Implementation of the base class of the Binary Search Tree (final)

Source: Internet
Author: User

The following is an example of using the data structure.

It seems troublesome and resource-consuming, but it does offer the benefits of polymorphism.

Main. cpp File

#include <iostream>#include "BSTree.h"#include "IntClass.h"#include "StringClass.h"using namespace std;int main(){BSTree bstree;BSTree bstreeStr;bstree.insert(new IntClass(10),new StringClass("Tao",3));bstree.insert(new IntClass(5),new StringClass("Tao",3));bstree.insert(new IntClass(15),new StringClass("Lily",4));bstree.insert(new IntClass(3),new StringClass("Tom",3));bstree.insert(new IntClass(7),new StringClass("John",4));bstree.insert(new IntClass(13),new StringClass("Peter",5));bstree.insert(new IntClass(17),new StringClass("Joson",5));bstree.insert(new IntClass(2),new StringClass("Tao",3));bstree.insert(new IntClass(4),new StringClass("Tao",3));bstree.insert(new IntClass(6),new StringClass("Tao",3));bstree.insert(new IntClass(8),new StringClass("Lucy",4));bstree.insert(new IntClass(11),new StringClass("Jim",3));bstree.insert(new IntClass(14),new StringClass("Brown",5));bstree.insert(new IntClass(16),new StringClass("Tao",3));bstree.insert(new IntClass(18),new StringClass("Tao",3));bstree.insert(new IntClass(1),new StringClass("Tao",3));bstree.insert(new IntClass(9),new StringClass("Tao",3));bstree.insert(new IntClass(12),new StringClass("Tao",3));cout<<"-------------------------------------------------"<<endl;cout<<"The total tree is like this:"<<endl;bstree.outPut();cout<<"-------------------------------------------------"<<endl;cout<<"The sub-tree is like this:"<<endl;bstree.ascend(bstree.get(new IntClass(15)));cout<<"-------------------------------------------------"<<endl;bstreeStr.insert(new StringClass("Jim",3),new StringClass("Hello, I'm a student",20));bstreeStr.insert(new StringClass("Lucy",4),new StringClass("Hello, I'm a teacher",20));bstreeStr.insert(new StringClass("Brown",5),new StringClass("Hello, I'm a doctor",19));bstreeStr.insert(new StringClass("Lily",4),new StringClass("Hello, I'm a actor",18));bstreeStr.insert(new StringClass("Tao",3),new StringClass("Hello, I'm a student",20));bstreeStr.insert(new StringClass("Peter",5),new StringClass("Hello, I'm a teacher",20));bstreeStr.insert(new StringClass("John",4),new StringClass("Hello, I'm a doctor",19));bstreeStr.insert(new StringClass("Tony",4),new StringClass("Hello, I'm a actor",18));bstreeStr.insert(new StringClass("Linda",5),new StringClass("Hello, I'm a student",20));bstreeStr.insert(new StringClass("Jurcy",5),new StringClass("Hello, I'm a teacher",20));bstreeStr.insert(new StringClass("Chern",5),new StringClass("Hello, I'm a doctor",19));bstreeStr.insert(new StringClass("Rone",4),new StringClass("Hello, I'm a actor",18));bstreeStr.outPut();cout<<"-------------------------------------------------"<<endl;}
The output result is as follows:

Appendix: we need to use a queue to traverse the hierarchy of the tree. Here we will show it. This is a template class.

#ifndef QUEUE_H#define QUEUE_H#define MAXLEN 20#include <assert.h>template<class T>class Queue{private:T data[MAXLEN];int head,end;public:Queue();bool EnQueue(T next);T DeQueue();bool isFull();bool isEmpty();};#endiftemplate<class T>Queue<T>::Queue(){head = 0;end = 0;}template<class T>bool Queue<T>::EnQueue(T next){if(isFull())return false;data[end] = next;end=end+1;if(end >= MAXLEN)end = 0;return true;}template<class T>T Queue<T>::DeQueue(){assert(!isEmpty());int temp=head;head=head+1;if(head>=MAXLEN)head=0;return data[temp];}template<class T>bool Queue<T>::isFull(){if(end+1==head || (head==0 && end == MAXLEN-1)) return true;return false;}template<class T>bool Queue<T>::isEmpty(){if(head == end)return true;return false;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.