1 / 46

Operations on Binary Tree

Operations on Binary Tree. There are a number of operations that can be defined for a binary tree. If p is pointing to a node in an existing tree then left( p ) returns pointer to the left subtree right( p ) returns pointer to right subtree parent( p ) returns the father of p

jhampton
Download Presentation

Operations on Binary Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operations on Binary Tree • There are a number of operations that can be defined for a binary tree. • If p is pointing to a node in an existing tree then • left(p) returns pointer to the left subtree • right(p) returns pointer to right subtree • parent(p) returns the father of p • brother(p) returns brother of p. • info(p) returns content of the node.

  2. Operations on Binary Tree • In order to construct a binary tree, the following can be useful: • setLeft(p,x) creates the left child node of p. The child node contains the info ‘x’. • setRight(p,x) creates the right child node of p. The child node contains the info ‘x’.

  3. Applications of Binary Trees • A binary tree is a useful data structure when two-way decisions must be made at each point in a process. • For example, suppose we wanted to find all duplicates in a list of numbers:14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  4. Applications of Binary Trees • One way of finding duplicates is to compare each number with all those that precede it. 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  5. Searching for Duplicates • If the list of numbers is large and is growing, this procedure involves a large number of comparisons. • A linked list could handle the growth but the comparisons would still be large. • The number of comparisons can be drastically reduced by using a binary tree. • The tree grows dynamically like the linked list.

  6. Searching for Duplicates • The binary tree is built in a special way. • The first number in the list is placed in a node that is designated as the root of a binary tree. • Initially, both left and right subtrees of the root are empty. • We take the next number and compare it with the number placed in the root. • If it is the same then we have a duplicate.

  7. Searching for Duplicates • Otherwise, we create a new tree node and put the new number in it. • The new node is made the left child of the root node if the second number is less than the one in the root. • The new node is made the right child if the number is greater than the one in the root.

  8. 14 Searching for Duplicates 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  9. 14 15 Searching for Duplicates 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  10. 14 15 Searching for Duplicates 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  11. 14 15 4 Searching for Duplicates 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  12. 14 15 4 Searching for Duplicates 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  13. 14 15 4 9 Searching for Duplicates 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  14. 14 15 4 9 Searching for Duplicates 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  15. 14 15 4 9 7 Searching for Duplicates 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  16. 14 15 4 9 7 Searching for Duplicates 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  17. 14 15 4 9 7 18 Searching for Duplicates 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  18. 14 15 4 9 7 18 Searching for Duplicates 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

  19. 14 15 4 9 7 18 3 Searching for Duplicates 3, 5, 16, 4, 20, 17, 9, 14, 5

  20. 14 15 4 9 7 18 3 Searching for Duplicates 3, 5, 16, 4, 20, 17, 9, 14, 5

  21. 14 15 4 9 7 18 3 5 Searching for Duplicates 5, 16, 4, 20, 17, 9, 14, 5

  22. 14 15 4 9 7 18 3 5 Searching for Duplicates 5, 16, 4, 20, 17, 9, 14, 5

  23. 14 15 4 9 7 18 3 5 16 Searching for Duplicates 16, 4, 20, 17, 9, 14, 5

  24. 14 15 4 9 7 18 3 5 16 Searching for Duplicates 16, 4, 20, 17, 9, 14, 5

  25. 14 15 4 9 7 18 3 5 16 4 Searching for Duplicates 4, 20, 17, 9, 14, 5

  26. 14 15 4 9 7 18 3 5 16 20 Searching for Duplicates 20, 17, 9, 14, 5

  27. 14 15 4 9 7 18 3 5 16 20 Searching for Duplicates 20, 17, 9, 14, 5

  28. 14 15 4 9 7 18 3 5 16 20 17 Searching for Duplicates 17, 9, 14, 5

  29. 14 15 4 9 7 18 3 5 16 20 17 Searching for Duplicates 17, 9, 14, 5

  30. 14 15 4 9 7 18 3 5 16 20 17 Searching for Duplicates 9, 14, 5

  31. C++ Implementation #include <stdlib.h> template <class Object> class TreeNode { public: // constructors TreeNode() { this->object = NULL; this->left = this->right = NULL; }; TreeNode( Object* object ) { this->object = object; this->left = this->right = NULL; };

  32. C++ Implementation Object* getInfo() { return this->object; }; void setInfo(Object* object) { this->object = object; }; TreeNode* getLeft() { return left; }; void setLeft(TreeNode *left) { this->left = left; };

  33. C++ Implementation TreeNode *getRight() { return right; }; void setRight(TreeNode *right) { this->right = right; }; int isLeaf( ) { if( this->left == NULL && this->right == NULL ) return 1; return 0; };

  34. C++ Implementation private: Object* object; TreeNode* left; TreeNode* right; }; // end class TreeNode

  35. C++ Implementation #include <iostream> #include <stdlib.h> #include "TreeNode.cpp" int main(int argc, char *argv[]) { int x[] = { 14, 15, 4, 9, 7, 18, 3, 5, 16,4, 20, 17, 9, 14,5, -1}; TreeNode<int>* root = new TreeNode<int>(); root->setInfo( &x[0] ); for(int i=1; x[i] > 0; i++ ) { insert(root, &x[i] ); } }

  36. C++ Implementation void insert(TreeNode<int>* root, int* info) { TreeNode<int>* node = new TreeNode<int>(info); TreeNode<int> *p, *q; p = q = root; while( *info != *(p->getInfo()) && q != NULL ) { p = q; if( *info < *(p->getInfo()) ) q = p->getLeft(); else q = p->getRight(); }

  37. C++ Implementation if( *info == *(p->getInfo()) ){ cout << "attempt to insert duplicate: " << *info << endl; delete node; } else if( *info < *(p->getInfo()) ) p->setLeft( node ); else p->setRight( node ); } // end of insert

  38. 14 15 4 9 7 18 3 5 16 20 17 p q Trace of insert 17, 9, 14, 5

  39. 14 15 4 9 7 18 3 5 16 20 17 p q Trace of insert 17, 9, 14, 5

  40. 14 15 4 9 7 18 3 5 16 20 17 p q Trace of insert 17, 9, 14, 5

  41. 14 15 4 9 7 18 3 5 16 20 17 p q Trace of insert 17, 9, 14, 5

  42. 14 15 4 9 7 18 3 5 16 20 17 p q Trace of insert 17, 9, 14, 5

  43. 14 15 4 9 7 18 3 5 16 20 17 p q Trace of insert 17, 9, 14, 5

  44. 14 15 4 9 7 18 3 5 16 20 17 p q Trace of insert 17, 9, 14, 5

  45. 14 15 4 9 7 18 3 5 16 20 17 p q Trace of insert 17, 9, 14, 5

  46. 14 15 4 9 7 18 3 5 16 20 17 p Trace of insert 17, 9, 14, 5 node p->setRight( node );

More Related