Advertisement
Gilgamesh858

esame-141008.cpp

Jun 12th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <class H> class MultiBST{
  6.     public:
  7.         virtual MultiBST<H>* ins(H x) = 0;
  8.         virtual int search(H x) = 0;
  9.         virtual void inorder() = 0;
  10. };
  11.  
  12. template <typename H> class Node{
  13.     private:
  14.         H key;
  15.         int mul;
  16.         Node<H> *parent, *left, *right;
  17.     public:
  18.         Node(H x){
  19.             key = x;
  20.             mul = 1;
  21.             parent = left = right = NULL;
  22.         }
  23.         H getKey() { return key; }
  24.         int getMul() { return mul; }
  25.         Node<H>* getParent() { return parent; }
  26.         Node<H>* getLeft() { return left; }
  27.         Node<H>* getRight() { return right; }
  28.         void setKey(H x){ key = x; }
  29.         void setParent(Node<H> *par){ parent = par; }
  30.         void setLeft(Node<H> *lef){ left = lef; }
  31.         void setRight(Node<H> *rig){ right = rig; }
  32.         void incMul(){ mul++; }
  33.         void decMul(){ mul--; }
  34.         int isLeaf() {
  35.             if( left == NULL && right == NULL)
  36.                 return 1;
  37.             return 0;
  38.         }
  39.         int onlyOneSon(){
  40.             if(left!=NULL && right==NULL)
  41.                 return 1;
  42.             else if(left==NULL && right!=NULL)
  43.                 return 2;
  44.             return 0;
  45.         }
  46. };
  47.  
  48. template <typename H> class MyMultiBST: public MultiBST<H>{
  49.     private:
  50.         Node<H> *root;
  51.     public:
  52.         MyMultiBST(){ root = NULL; }
  53.         int isEmpty(){
  54.             if(root==NULL)
  55.                 return 1;
  56.             return 0;
  57.         }
  58.         void _ins(Node<H> *aux, Node<H> *par, H x){
  59.             if(aux!=NULL && aux->getKey() == x){
  60.                 aux->incMul();
  61.                 return;
  62.             }
  63.             if(aux==NULL){
  64.                 Node<H> *temp = new Node<H>(x);
  65.                 if(par==NULL){
  66.                     root = temp;
  67.                     return;
  68.                 }
  69.                 if( x < par->getKey() ){
  70.                     par->setLeft(temp);
  71.                     temp->setParent(par);
  72.                     return;
  73.                 }
  74.                 else{
  75.                     par->setRight(temp);
  76.                     temp->setParent(par);
  77.                     return;
  78.                 }
  79.             }
  80.             par = aux;
  81.             if( x < aux->getKey() )
  82.                 aux = aux->getLeft();
  83.             else
  84.                 aux = aux->getRight();
  85.             _ins(aux, par, x);
  86.         }
  87.         MultiBST<H>* ins(H x){
  88.             _ins(root, NULL, x);
  89.             return this;
  90.         }
  91.         int search(H x){
  92.             return 0;
  93.         }
  94.         void _inorder(Node<H> *aux){
  95.             if(aux==NULL)
  96.                 return;
  97.             _inorder(aux->getLeft());
  98.             for(int i=0; i < aux->getMul(); i++) cout << aux->getKey() << " ";
  99.             _inorder(aux->getRight());
  100.         }
  101.         void _postorder(Node<H> *aux){
  102.             if(aux==NULL)
  103.                 return;
  104.             _postorder(aux->getLeft());
  105.             _postorder(aux->getRight());
  106.             for(int i=0; i < aux->getMul(); i++) cout << aux->getKey() << " ";
  107.         }
  108.         void _preorder(Node<H> *aux){
  109.             if(aux==NULL)
  110.                 return;
  111.             _preorder(aux->getLeft());
  112.             _preorder(aux->getRight());
  113.             for(int i=0; i < aux->getMul(); i++) cout << aux->getKey() << " ";
  114.         }
  115.         void inorder(){
  116.             _inorder(root);
  117.             cout << endl;
  118.         }
  119.         void postorder(){
  120.             _postorder(root);
  121.             cout << endl;
  122.         }
  123.         void preorder(){
  124.             _preorder(root);
  125.             cout << endl;
  126.         }
  127. };
  128. int main()
  129. {
  130.     MyMultiBST<int> *T = new MyMultiBST<int>();
  131.     T->ins(10)->ins(5)->ins(2)->ins(7)->ins(17)->ins(12)->ins(3)->ins(15);
  132.     T->inorder();
  133.     T->postorder();
  134.     T->preorder();
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement