Upcoming SlideShare
×

# Computer notes - Operations on Binary Tree

4,765 views

Published on

There are a number of operations that can be defined for a binary tree. A binary tree is a useful data structure when two-way decisions must be made at each point in a process. One way of finding duplicates is to compare each number with all those that precede it.

Published in: Education, Technology, Design
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
4,765
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
162
0
Likes
3
Embeds 0
No embeds

No notes for slide
• End of lecture 11
• End of lecture 12
• ### Computer notes - Operations on Binary Tree

1. 1. Class No.12 Data Structures http://ecomputernotes.com
2. 2. Operations on Binary Tree <ul><li>There are a number of operations that can be defined for a binary tree. </li></ul><ul><li>If p is pointing to a node in an existing tree then </li></ul><ul><ul><li>left( p ) returns pointer to the left subtree </li></ul></ul><ul><ul><li>right( p ) returns pointer to right subtree </li></ul></ul><ul><ul><li>parent( p ) returns the father of p </li></ul></ul><ul><ul><li>brother( p ) returns brother of p . </li></ul></ul><ul><ul><li>info(p) returns content of the node. </li></ul></ul>http://ecomputernotes.com
3. 3. Operations on Binary Tree <ul><li>In order to construct a binary tree, the following can be useful: </li></ul><ul><li>setLeft(p,x) creates the left child node of p. The child node contains the info ‘x’. </li></ul><ul><li>setRight(p,x) creates the right child node of p. The child node contains the info ‘x’. </li></ul>http://ecomputernotes.com
4. 4. Applications of Binary Trees <ul><li>A binary tree is a useful data structure when two-way decisions must be made at each point in a process. </li></ul><ul><li>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 </li></ul>http://ecomputernotes.com
5. 5. Applications of Binary Trees <ul><li>One way of finding duplicates is to compare each number with all those that precede it. </li></ul>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 http://ecomputernotes.com
6. 6. Searching for Duplicates <ul><li>If the list of numbers is large and is growing, this procedure involves a large number of comparisons. </li></ul><ul><li>A linked list could handle the growth but the comparisons would still be large. </li></ul><ul><li>The number of comparisons can be drastically reduced by using a binary tree. </li></ul><ul><li>The tree grows dynamically like the linked list. </li></ul>http://ecomputernotes.com
7. 7. Searching for Duplicates <ul><li>The binary tree is built in a special way. </li></ul><ul><li>The first number in the list is placed in a node that is designated as the root of a binary tree. </li></ul><ul><li>Initially, both left and right subtrees of the root are empty. </li></ul><ul><li>We take the next number and compare it with the number placed in the root. </li></ul><ul><li>If it is the same then we have a duplicate. </li></ul>http://ecomputernotes.com
8. 8. Searching for Duplicates <ul><li>Otherwise, we create a new tree node and put the new number in it. </li></ul><ul><li>The new node is made the left child of the root node if the second number is less than the one in the root. </li></ul><ul><li>The new node is made the right child if the number is greater than the one in the root. </li></ul>http://ecomputernotes.com
9. 9. Searching for Duplicates <ul><li>14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14
10. 10. Searching for Duplicates <ul><li>15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15
11. 11. Searching for Duplicates <ul><li>15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15
12. 12. Searching for Duplicates <ul><li>4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4
13. 13. Searching for Duplicates <ul><li>4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4
14. 14. Searching for Duplicates <ul><li>9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9
15. 15. Searching for Duplicates <ul><li>9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9
16. 16. Searching for Duplicates <ul><li>7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7
17. 17. Searching for Duplicates <ul><li>7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7
18. 18. Searching for Duplicates <ul><li>18, 3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18
19. 19. Searching for Duplicates <ul><li>18, 3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18
20. 20. Searching for Duplicates <ul><li>3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3
21. 21. Searching for Duplicates <ul><li>3, 5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3
22. 22. Searching for Duplicates <ul><li>5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5
23. 23. Searching for Duplicates <ul><li>5, 16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5
24. 24. Searching for Duplicates <ul><li>16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16
25. 25. Searching for Duplicates <ul><li>16, 4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16
26. 26. Searching for Duplicates <ul><li>4, 20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 4
27. 27. Searching for Duplicates <ul><li>20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20
28. 28. Searching for Duplicates <ul><li>20, 17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20
29. 29. Searching for Duplicates <ul><li>17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17
30. 30. Searching for Duplicates <ul><li>17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17
31. 31. Searching for Duplicates <ul><li>9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17
32. 32. C++ Implementation <ul><li>#include <stdlib.h> </li></ul><ul><li>template <class Object> </li></ul><ul><li>class TreeNode { </li></ul><ul><li>public: </li></ul><ul><li>// constructors </li></ul><ul><li>TreeNode() </li></ul><ul><li>{ </li></ul><ul><li>this->object = NULL; </li></ul><ul><li>this->left = this->right = NULL; </li></ul><ul><li>}; </li></ul><ul><li> TreeNode( Object* object ) </li></ul><ul><li>{ </li></ul><ul><li>this->object = object; </li></ul><ul><li>this->left = this->right = NULL; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
33. 33. C++ Implementation <ul><ul><li>Object* getInfo() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>return this->object; </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>void setInfo(Object* object) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>this->object = object; </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>TreeNode* getLeft() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>return left; </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>void setLeft(TreeNode *left) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>this->left = left; </li></ul></ul><ul><ul><li>}; </li></ul></ul>http://ecomputernotes.com
34. 34. C++ Implementation <ul><ul><li>TreeNode *getRight() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>return right; </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>void setRight(TreeNode *right) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>this->right = right; </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>int isLeaf( ) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>if( this->left == NULL && this->right == NULL ) </li></ul></ul><ul><ul><li> return 1; </li></ul></ul><ul><ul><li>return 0; </li></ul></ul><ul><ul><li>}; </li></ul></ul>http://ecomputernotes.com
35. 35. C++ Implementation <ul><li>private: </li></ul><ul><li>Object* object; </li></ul><ul><li>TreeNode* left; </li></ul><ul><li>TreeNode* right; </li></ul><ul><li>}; // end class TreeNode </li></ul>http://ecomputernotes.com
36. 36. C++ Implementation <ul><li>#include <iostream> </li></ul><ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;TreeNode.cpp&quot; </li></ul><ul><li>int main(int argc, char *argv[]) </li></ul><ul><li>{ </li></ul><ul><li>int x[] = { 14, 15, 4, 9, 7, 18, 3, 5, 16,4, 20, 17, </li></ul><ul><li> 9, 14,5, -1}; </li></ul><ul><li>TreeNode<int>* root = new TreeNode<int>(); </li></ul><ul><li>root->setInfo( &x[0] ); </li></ul><ul><li>for(int i=1; x[i] > 0; i++ ) </li></ul><ul><li>{ </li></ul><ul><li>insert(root, &x[i] ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
37. 37. C++ Implementation <ul><li>void insert(TreeNode<int>* root, int* info) </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<int>* node = new TreeNode<int>(info); </li></ul><ul><li>TreeNode<int> *p, *q; </li></ul><ul><li>p = q = root; </li></ul><ul><li>while( *info != *(p->getInfo()) && q != NULL ) </li></ul><ul><li>{ </li></ul><ul><li>p = q; </li></ul><ul><li>if( *info < *(p->getInfo()) ) </li></ul><ul><li>q = p->getLeft(); </li></ul><ul><li>else </li></ul><ul><li>q = p->getRight(); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
38. 38. C++ Implementation <ul><li> if( *info == *(p->getInfo()) ){ </li></ul><ul><li>cout << &quot;attempt to insert duplicate: &quot; </li></ul><ul><li> << *info << endl; </li></ul><ul><li>delete node; </li></ul><ul><li>} </li></ul><ul><li>else if( *info < *(p->getInfo()) ) </li></ul><ul><li>p->setLeft( node ); </li></ul><ul><li>else </li></ul><ul><li>p->setRight( node ); </li></ul><ul><li>} // end of insert </li></ul>http://ecomputernotes.com
39. 39. Trace of insert <ul><li>17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17 p q
40. 40. Trace of insert <ul><li>17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17 p q
41. 41. Trace of insert <ul><li>17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17 p q
42. 42. Trace of insert <ul><li>17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17 p q
43. 43. Trace of insert <ul><li>17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17 p q
44. 44. Trace of insert <ul><li>17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17 p q
45. 45. Trace of insert <ul><li>17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17 p q
46. 46. Trace of insert <ul><li>17, 9, 14, 5 </li></ul>http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17 p q
47. 47. Trace of insert <ul><li>17, 9, 14, 5 </li></ul>p->setRight( node ); node http://ecomputernotes.com 14 15 4 9 7 18 3 5 16 20 17 p