tikimyster

notes

May 8th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. Class Tree
  2. {
  3.     public:
  4.         Tree();
  5.         bool insert(int value);
  6.         bool lookup(int target); {return lookup(target,mRoot);}
  7.     private:
  8.         Class Node
  9.         {
  10.             Node(int value){mValue = Value; mLeft = NULL, mRight = NULL, mParrent = NULL;}
  11.             mValue = value;
  12.             Node *mLeft;
  13.             Node *mRight;
  14.             Node *mParrent;
  15.         };
  16.         Node *mRoot;
  17.         bool lookup(int target, Node *root)
  18.         bool insert(int value, Node *root)
  19. };
  20.  
  21. bool Tree:: lookup (int target, Node *root)//Private lookup;
  22. {
  23.     if(!root)
  24.         return false;
  25.     if(root->mValue == target)
  26.         return true;
  27.     if(target < root->mValue)
  28.         return lookup(target, root->mLeft);
  29.     else return lookup(target, root->mRight);
  30. }
  31.  
  32. bool Tree:: insert(int value)//Public insert
  33. {
  34.     if(!mRoot)
  35.     {
  36.         mRoot == new Node(value);
  37.         return true;
  38.     }
  39.     return insert(value,mRoot);
  40. }
  41.  
  42. bool Tree:: insert (int value, Node* root) // private insert (recurssions)
  43. {
  44.     if(value == root->mValue)
  45.         return false;
  46.     else if(value < root->mValue) //handles the left side of the tree
  47.     {
  48.         if(root->mLeft == NULL)
  49.         {
  50.             root->mLeft = new Node(value);
  51.             return true;
  52.         }
  53.         else return inest(value, root->mLeft)
  54.     }
  55.     else // handles the right side of the tree
  56.     {
  57.         if(root->mRight == NULL)
  58.         {
  59.             root->mRight = new Node(value);
  60.             return true;
  61.         }
  62.         else return insert(value, root->mRight)
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment