Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class Tree
- {
- public:
- Tree();
- bool insert(int value);
- bool lookup(int target); {return lookup(target,mRoot);}
- private:
- Class Node
- {
- Node(int value){mValue = Value; mLeft = NULL, mRight = NULL, mParrent = NULL;}
- mValue = value;
- Node *mLeft;
- Node *mRight;
- Node *mParrent;
- };
- Node *mRoot;
- bool lookup(int target, Node *root)
- bool insert(int value, Node *root)
- };
- bool Tree:: lookup (int target, Node *root)//Private lookup;
- {
- if(!root)
- return false;
- if(root->mValue == target)
- return true;
- if(target < root->mValue)
- return lookup(target, root->mLeft);
- else return lookup(target, root->mRight);
- }
- bool Tree:: insert(int value)//Public insert
- {
- if(!mRoot)
- {
- mRoot == new Node(value);
- return true;
- }
- return insert(value,mRoot);
- }
- bool Tree:: insert (int value, Node* root) // private insert (recurssions)
- {
- if(value == root->mValue)
- return false;
- else if(value < root->mValue) //handles the left side of the tree
- {
- if(root->mLeft == NULL)
- {
- root->mLeft = new Node(value);
- return true;
- }
- else return inest(value, root->mLeft)
- }
- else // handles the right side of the tree
- {
- if(root->mRight == NULL)
- {
- root->mRight = new Node(value);
- return true;
- }
- else return insert(value, root->mRight)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment