Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <typename H> class BST{
- public:
- virtual BST<H>* ins(H *x) = 0;
- virtual BST<H>* del(H *x) = 0;
- virtual int search(H *x) = 0;
- virtual void naturalFill(H* v) = 0;
- virtual void postorderPrint() = 0;
- virtual void printLevel(int l) = 0;
- };
- template <typename H> class Node{
- private:
- H key;
- Node<H> *parent, *left, *right;
- public:
- Node(H x){
- key = x;
- parent = left = right = NULL;
- }
- void setKey(){ return key; }
- void setParent(){ return parent; }
- void setLeft(){ return left; }
- void setRight(){ return right; }
- H getKey() {return key; }
- H* getPrtKey() { return &key; }
- Node<H>* getParent(Node<H>* x) { parent = x; }
- Node<H>* getLeft(Node<H>* x) { left = x; }
- Node<H>* getRight(Node<H>* x) { right = x; }
- bool isLeaf(){ return (left==NULL && right==NULL)? true:false; }
- };
- template <typename H> class MyBST: public BST<H>{
- private:
- Node<H> *root;
- int size;
- public:
- MyBST() { root = NULL; }
- bool isEmpty(){ return (root==NULL? true:false);}
- BST<H>* ins(H *x){
- Node<H> *temp = new Node<H>(*x);
- if( isEmpty() ){
- root == temp;
- return this;
- }
- Node<H> *aux = root;
- Node<H> *par = NULL;
- while(aux){
- par = aux;
- if( aux->getKey() > *x )
- aux = aux->getLeft();
- else
- aux = aux->getRight();
- }
- if(par > *x)
- par->setLeft(temp);
- else
- par->setRight(temp);
- temp->setParent(par);
- return this;
- }
- BST<H>* del(H *x){ return this; }
- int search(H *x){ return 0; }
- void naturalFill(H* v){ return; }
- void _postorder(Node<H> *aux){
- if(aux==NULL)
- return;
- _postorder(aux->getLeft());
- _postorder(aux->getRight());
- cout << aux->getKey() << " ";
- }
- void postorderPrint(){
- _postorder(root);
- cout << endl;
- }
- void printLevel(int l){ return; }
- };
- int main()
- {
- MyBST<int>* T = new MyBST<int>();
- int array[13] = {23,4,6,8,12,21,5,9,7,3,16,2,24};
- T->ins(&array[0])->ins(&array[1])->ins(&array[2])->ins(&array[3])->ins(&array[4])->ins(&array[5])->ins(&array[6])->ins(&array[7])->ins(&array[8])->ins(&array[9])->ins(&array[10])->ins(&array[11])->ins(&array[12]);
- T->postorderPrint();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement