Advertisement
Gilgamesh858

esame-140624.cpp

Jun 14th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename H> class BST{
  6.     public:
  7.         virtual BST<H>* ins(H *x) = 0;
  8.         virtual BST<H>* del(H *x) = 0;
  9.         virtual int search(H *x) = 0;
  10.         virtual void naturalFill(H* v) = 0;
  11.         virtual void postorderPrint() = 0;
  12.         virtual void printLevel(int l) = 0;
  13. };
  14.  
  15. template <typename H> class Node{
  16.     private:
  17.         H key;
  18.         Node<H> *parent, *left, *right;
  19.     public:
  20.         Node(H x){
  21.             key = x;
  22.             parent = left = right = NULL;
  23.         }
  24.         void setKey(){ return key; }
  25.         void setParent(){ return parent; }
  26.         void setLeft(){ return left; }
  27.         void setRight(){ return right; }
  28.         H getKey() {return key; }
  29.         H* getPrtKey() { return &key; }
  30.         Node<H>* getParent(Node<H>* x) { parent = x; }
  31.         Node<H>* getLeft(Node<H>* x) { left = x; }
  32.         Node<H>* getRight(Node<H>* x) { right = x; }
  33.         bool isLeaf(){ return (left==NULL && right==NULL)? true:false; }
  34. };
  35.  
  36. template <typename H> class MyBST: public BST<H>{
  37.     private:
  38.         Node<H> *root;
  39.         int size;
  40.     public:
  41.         MyBST() { root = NULL; }
  42.         bool isEmpty(){  return (root==NULL? true:false);}
  43.         BST<H>* ins(H *x){
  44.             Node<H> *temp = new Node<H>(*x);
  45.             if( isEmpty() ){
  46.                 root == temp;
  47.                 return this;
  48.             }
  49.             Node<H> *aux = root;
  50.             Node<H> *par = NULL;
  51.             while(aux){
  52.                 par = aux;
  53.                 if( aux->getKey() > *x )
  54.                     aux = aux->getLeft();
  55.                 else
  56.                     aux = aux->getRight();
  57.             }
  58.             if(par > *x)
  59.                 par->setLeft(temp);
  60.             else
  61.                 par->setRight(temp);
  62.             temp->setParent(par);
  63.             return this;
  64.         }
  65.         BST<H>* del(H *x){ return this; }
  66.         int search(H *x){ return 0; }
  67.         void naturalFill(H* v){ return; }
  68.         void _postorder(Node<H> *aux){
  69.             if(aux==NULL)
  70.                 return;
  71.             _postorder(aux->getLeft());
  72.             _postorder(aux->getRight());
  73.             cout << aux->getKey() << " ";
  74.         }
  75.         void postorderPrint(){
  76.             _postorder(root);
  77.             cout << endl;
  78.         }
  79.         void printLevel(int l){ return; }
  80. };
  81.  
  82. int main()
  83. {
  84.     MyBST<int>* T = new MyBST<int>();
  85.     int array[13] = {23,4,6,8,12,21,5,9,7,3,16,2,24};
  86.     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]);
  87.     T->postorderPrint();
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement