raxbg

BinaryTree.h

May 9th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #ifndef BINARY_TREE
  2. #define BINARY_TREE
  3. #include "SimpleQueue.h"
  4. using namespace std;
  5.  
  6. class BinaryTree
  7. {
  8.     private:
  9.         short int state; //0 - empty, 1 - not empty
  10.         int rowsCount;
  11.         Node *root;
  12.         SimpleQueue *nodesToWalk, *tmpNodes;
  13.  
  14.         int Power(int n,int power)
  15.         {
  16.             if (power == 0) return 1;
  17.             for (int i=1;i<power;i++)
  18.                 n *= n;
  19.             return n;
  20.         }
  21.  
  22.     public:
  23.         BinaryTree()
  24.         {
  25.             rowsCount = 0;
  26.             state = 0;
  27.             root = 0;
  28.         }
  29.  
  30.         BinaryTree(int v)
  31.         {
  32.             root = new Node(v);
  33.             rowsCount = 1;
  34.             state = 1;
  35.         }
  36.  
  37.         bool init(int v)
  38.         {
  39.             root = new Node(v);
  40.             if (root != 0)
  41.             {
  42.                 state = 1;
  43.                 rowsCount = 1;
  44.                 return true;
  45.             }
  46.             return false;
  47.         }
  48.  
  49.         void Build(int arr[], int elementsCount)
  50.         {
  51.             if (state == 0)
  52.             {
  53.                 root = new Node();
  54.                 state = 1;
  55.                 rowsCount = 1;
  56.             }
  57.             if (elementsCount > 0)
  58.             {
  59.                 Node *tmpNode, *tmpChild;
  60.                 root->SetVal(arr[0]);
  61.                 nodesToWalk = new SimpleQueue(1);
  62.                 nodesToWalk->Push(root);
  63.                 int i=1;
  64.                 int rowNum = 1;
  65.                 while (i < elementsCount)
  66.                 {
  67.                     tmpNodes = new SimpleQueue(Power(2,rowNum));
  68.                     while (nodesToWalk->GetState() != 3)
  69.                     {
  70.                         tmpNode = nodesToWalk->Pop();
  71.                         if (i < elementsCount)
  72.                         {
  73.                             tmpNode->SetLeftChild(new Node(arr[i++]));
  74.                             tmpChild = tmpNode->GetLeftChild();
  75.                             tmpNodes->Push(tmpChild);
  76.  
  77.                             if (i < elementsCount)
  78.                             {
  79.                                 tmpNode->SetRightChild(new Node(arr[i++]));
  80.                                 tmpChild = tmpNode->GetRightChild();
  81.                                 tmpNodes->Push(tmpChild);
  82.                             }
  83.                         }
  84.                     }
  85.                     SimpleQueue *delNodes;
  86.                     delNodes = nodesToWalk;
  87.                     nodesToWalk = tmpNodes;
  88.                     delete delNodes;
  89.                     rowNum++;
  90.                 }
  91.                 rowsCount = rowNum;
  92.             }
  93.         }
  94.  
  95.         void Walk(int rowNum = 1)
  96.         {
  97.             Node *tmpNode;
  98.             nodesToWalk = new SimpleQueue(1);
  99.             nodesToWalk->Push(root);
  100.             if (rowNum < 1) rowNum = 1;
  101.             else if (rowNum > rowsCount) rowNum = rowsCount;
  102.  
  103.             for (int i = 1; i<= rowsCount; i++)
  104.             {
  105.                 tmpNodes = new SimpleQueue(Power(2,i));
  106.                 while (nodesToWalk->GetState() != 3)
  107.                 {
  108.                     tmpNode = nodesToWalk->Pop();
  109.                     if (tmpNode->GetLeftChild() != 0) tmpNodes->Push(tmpNode->GetLeftChild());
  110.                     if (tmpNode->GetRightChild() != 0) tmpNodes->Push(tmpNode->GetRightChild());
  111.                     if (i >= rowNum) cout << tmpNode->GetVal() << ' ';
  112.                 }
  113.                 SimpleQueue * delNodes = nodesToWalk;
  114.                 nodesToWalk = tmpNodes;
  115.                 delete delNodes;
  116.                 cout << endl;
  117.             }
  118.         }
  119.  
  120.         int GetRowsCount() {return rowsCount;}
  121.  
  122.         Node * GetRoot() {return root;}
  123. };
  124. #endif
Advertisement
Add Comment
Please, Sign In to add comment