• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this document? Why not share!

computer notes - Binary tree

on

  • 660 views

If every non-leaf node in a binary tree has non-empty left and right subtrees, the tree is termed a strictly binary tree. There are a number of operations that can be defined for a binary tree.

If every non-leaf node in a binary tree has non-empty left and right subtrees, the tree is termed a strictly binary tree. There are a number of operations that can be defined for a binary tree.

Statistics

Views

Total Views
660
Views on SlideShare
660
Embed Views
0

Actions

Likes
1
Downloads
10
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    computer notes - Binary tree computer notes - Binary tree Document Transcript

    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________Data StructuresLecture No. 12Operations on Binary TreeIn the last lecture, we talked about the uses of binary tree, which is an abstract data type.We discussed about the functions to find the information inside a node, the parent, thesiblings(brothers), the left and right children of a node. In this lecture, we will talk aboutthe algorithms and implementation of those functions.When we discuss about an abstract data type, firstly we focus what it can do for us anddon t bother about the how part. The how part or implementation is thought out later.While implementing these abstract data types, the implementation is hidden in a class soit is abstract to the user. The user only needs to be aware of the interface. But there can besituations when the users may like to know about the implementation detail, for example,when a data type is performing quite slower than promised.For now, we start our discussion from the methods of tree data type. Consider a tree hasbeen formed already, following methods will be used to perform different operations on anode of this tree: Operation Description left(p) Returns a pointer to the left sub-tree right(p) Returns a pointer to the right sub-tree parent(p) Returns the father node of p brother(p) Returns the brother node of p info(p) Returns the contents of node pThese methods have already been discussed at the end of the previous lecture, however,few more methods are required to construct a binary tree: Operation Description setLeft(p, x) Creates the left child node of p and set the value x into it. setRight(p, x) Creates the right child node of p, the child node contains the info x.All these methods are required to build and to retrieve values from a tree. Page 1 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________Applications of Binary TreeLet s take few examples to understand how the tree data type is used and what are itsbenefits. We will also develop some algorithms that may by useful in future whileworking with this data type.Binary tree is useful structure when two-way decisions are made at each point. Supposewe want to find all duplicates in a list of the following numbers:14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5This list may comprise numbers of any nature. For example, roll numbers, telephonenumbers or voter s list. In addition to the presence of duplicate number, we may alsorequire the frequency of numbers in the list. As it is a small list, so only a cursory viewmay reveal that there are some duplicate numbers present in this list. Practically, this listcan be of very huge size ranging to thousands or millions.Searching for DuplicatesOne way of finding duplicates is to compare each number with all those that precede it.Let s see it in detail. 14 15 4, 9, 7, 18 3, 5, 16 4, 20 17 9, 14 5 4, , , , , , , , 14 15 4, 9, 7, 18 3, 5, 16 4, 20 17 9, 14 5 4, , , , , , , , Fig 12.1: Search for DuplicatesSuppose, we are looking for duplicates for the number 4, we will start scanning from thefirst number 14. While scanning, whenever we find the number 4 inside, we rememberthe position and increment its frequency counter by 1. This comparison will go on till theend of the list to get the duplicates or fequence of the number 4. You might haveunderstood already that we will have to perform this whole scanning of list every time foreach number to find duplicates. This is a long and time consuming process.So this procedure involves a large number of comparisons, if the list of numbers is largeand is growing.A linked list can handle the growth. But it can be used where a programmer has no ideaabout the size of the data before hand. The number of comparisons may still be large. Thecomparisons are not reduced after using linked list as it is a linear data structure. Tosearch a number in a linked list, we have to begin from the start of the list to the end inthe linear fashion, traversing each node in it. For optimizing search operation in a list,there is no real benefit of using linked list. On the contrary, the search operation becomesslower even while searching in an array because the linked list is not contiguous like anarray and traversing is done in the linked list with the help of pointers.So, the solution lies in reducing the number of comparisons. The number of comparisonscan be drastically reduced with the help of a binary tree. The benefits of linked list are Page 2 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________there, also the tree grows dynamically like the linked list.The binary tree is built in a special way. The first number in the list is placed in a node,designated as the root of the binary tree. Initially, both left and right sub-trees of the rootare empty. We take the next number and compare it with the number placed in the root. Ifit is the same, this means the presence of a duplicate. Otherwise, we create a new treenode and put the new number in it. The new node is turned into the left child of the rootnode if the second number is less than the one in the root. The new node is turned into theright child if the number is greater than the one in the root. 14 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 Fig 12.2: First number in the list became the rootIn the above figure, the first number in the list 14 is placed in a node , making it the rootof the binary tree. You can see that it is not pointing to any further node. Therefore, itsleft and right pointers are NULL at this point of tree construction time. Now, let s see,how do we insert the next element into it. 15 14 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 Fig 12.3: A new node is created to insert it in the binary treeAs a first step to add a new node in the tree, we take the next number 15 in the list andcompare it with 14, the number in the root node. As they are different, so a new node iscreated and the number 15 is set into it. 14 15 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 Fig 12.4: The second node is added into the tree Page 3 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________The next step is to add this node in the tree. We compare 15, the number in the new nodewith 14, the number in the root node. As number 15 is greater than number 14, therefore,it is placed as right child of the root node.The next number in the list i.e. 4, is compared with 14, the number in the root node. Asthe number 4 is less than number 14, so we see if there is a left child of the root node tocompare the number 4 with that. At this point of time in the tree, there is no further leftchild of the root node. Therefore, a new node is created and the number 4 is put into it.The below figure shows the newly created node. 4 14 15 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 Fig 12.5: A new node is created and number 4 put into itNext, the newly created node is added as the left child of the root node. It is shown in thefigure below. Page 4 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________ 14 4 15 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 Fig 12.6: The node is added as the left child of the root nodeThe next number in the list is 9. To add this number in the tree, we will follow thealready defined and experimented procedure. We compare this number first with thenumber in the root node of the tree. This number is found to be smaller than the numberin the root node. Therefore, left sub-tree of the root node is sought. The left child of theroot node is the one with number 4. On comparison, number 9 is found greater than thenumber 4. Therefore, we go to the right child of the node with number 4. At the moment,there is no further node to the right of it, necessitating the need of creating a new node.The number 9 is put into it and the new node is added as the right child of the node withnumber 4. The same is shown in the figure given below. 14 4 15 9 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 Fig 12.7: A new node is added in the treeWe keep on adding new nodes in the tree in line with the above practiced rule andeventually, we have the tree shown in the below figure. Page 5 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________ 14 4 15 3 9 18 7 16 20 5 1714, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 Fig 12.8: Binary tree of whole list of numbersIt is pertinent to note that this is a binary tree with two sub-nodes or children of eachnode. We have not seen the advantage of binary tree, the one we were earlier talkingabout i.e. it will reduce the number of comparisons. Previously, we found that searchoperation becomes troublesome and slower as the size of list grows. We will see thebenefit of using binary tree over linked list later. Firstly, we will see how the tree isimplemented.C++ Implementation of Binary TreeSee the code below for the file treenode.cpp. /* This file contains the TreeNode class declaration. TreeNode contains the functionality for a binary tree node */ 1. #include <stdlib.h> 2. 3. template <class Object> 4. 5. class TreeNode 6. { 7. public: 8. // constructors 9. TreeNode() 10. { 11. this->object = NULL; 12. this->left = this->right = NULL; 13. }; Page 6 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________ 14. 15. TreeNode( Object * object ) 16. { 17. this->object = object; 18. this->left = this->right = NULL; 19. }; 20. 21. Object * getInfo() 22. { 23. return this->object; 24. }; 25. 26. void setInfo(Object * object) 27. { 28. this->object = object; 29. }; 30. 31. TreeNode * getLeft() 32. { 33. return left; 34. }; 35. 36. void setLeft(TreeNode * left) 37. { 38. this->left = left; 39. }; 40. 41 TreeNode * getRight() 42. { 43. return right; 44. }; 45. 46. void setRight(TreeNode * right) 47. { 48. this->right = right; 49. }; 50. 51. int isLeaf( ) 52. { 53. if( this->left == NULL && this->right == NULL ) 54. return 1; 55. return 0; 56. }; 57. 58. private: 59. Object * object; Page 7 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________ 60. TreeNode * left; 61. TreeNode * right; 62. }; // end class TreeNodeFor implementation, we normally write a class that becomes a factory for objects of thattype. In this case too, we have created a class TreeNode to be used to create nodes of thebinary tree. As we want to use this class for different data types, therefore, we will makeit a template class, the line 2 is doing the same thing. Inside the class body, we start withthe private data members given at the bottom of the class declaration. At line 59, theobject is a private data element of Object *, used to store the tree element (value) insidethe node of the tree. left is a private data member of type TreeNode*, it is used to store apointer to the left sub-tree. right is a private data member of type TreeNode*, employedto store a pointer to the right sub-tree.Now, we go to the top of the class declaration to see the public functions. At line 9, apublic parameter-less constructor is declared. The data members have been initialized inthe constructor. At line 11, the object data member is initialized to NULL. Similarly leftand right data members are initialized to NULL at line 12.There is another constructor declared at line 15- that takes object value as a parameter toconstruct a TreeNode object with that object value. While the pointers for right and leftsub-trees are pointing to NULL.At line 21, there is method getInfo(), which returns the object i.e. the element of theTreeNode object.At line 26, the method setInfo(Object * ) sets the value of the object data member to thevalue passed to it as the argument.The method getLeft() returns the pointer to the left sub-tree. Similarly, the getRight()returns the right sub-tree. Note that both of these methods return a pointer to the object oftype TreeNode.The setLeft(TreeNode *) method is used to set the pointer left to left sub-tree. Similarly,setRight(TreeNode *) is used to set the pointer right to right sub-tree. Both of thesemethods accept a pointer of type TreeNode.The isLeaf() method at line 51, is to see whether the current node is a leaf node or not.The method returns 1 if it is leaf node. Otherwise, it returns 0.Using the above TreeNode, nodes for the binary tree can be created and linked uptogether to form a binary tree. We can write a separate method or a class to carry out thenode creation and insertion into tree.Let s use this class by writing couple of functions. Below is the code of main programfile containing the main() and insert() functions. 1. #include <iostream> 2. #include <stdlib.h> 3. #include "TreeNode.cpp" 4. 5. int main(int argc, char * argv[]) 6. { Page 8 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________ 7. int x[] = {14,15,4,9,7,18,3,5,16,4,20,17,9,14,5,-1}; 8. TreeNode <int> * root = new TreeNode<int>(); 9. root->setInfo( &x[0] ); 10. for(int i = 1; x[i] > 0; i++ ) 11. { 12. insert( root, &x[i] ); 13. } 14. } 15. 16. void insert (TreeNode <int> * root, int * info) 17. { 18. TreeNode <int> * node = new TreeNode <int> (info); 19. TreeNode <int> * p, * q; 20. p = q = root; 21. while( *info != *(p->getInfo()) && q != NULL ) 22. { 23. p = q; 24. if( *info < *(p->getInfo()) ) 25. q = p->getLeft(); 26. else 27. q = p->getRight(); 28. } 29. 30. if( *info == *( p->getInfo() ) ) 31. { 32. cout << "attempt to insert duplicate: " << *info << endl; 33. delete node; 34. } 35. else if( *info < *(p->getInfo()) ) 36. p->setLeft( node ); 37. else 38. p->setRight( node ); 39. } // end of insertWe have used the same list of numbers for discussion in this lecture. It is given at line 7in the code. It is the same, only the last number is 1. This is used as delimiter or markerto indicate that the list has finished.At line 8, we are creating a new TreeNode object i.e. a root node as the name implies.This node will contain an int type element as evident from the syntax.At line 9, the first number in the list is set into the root node. At line 10, the for loop isstarted, which is inserting all the elements of the list one by one in the tree with the use ofthe insert() function. Most of the time, this loop will be reading the input numbers fromthe users interactively or from a file. It it is Windows application then a form can by usedto take input from the user. In this implementation, our objective is to insert numbers inthe tree and see the duplicates, so hard coding the numbers within our program will servethe purpose. Page 9 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________The insert() method starts from line 16. It is accepting two parameters. The firstparameter is pointer to a TreeNode object, containing a value of int type while second isthe info that is an int *.In the first line of the function, line 18, a new node has been by calling the parameterizedconstructor of the TreeNode class.Then at line 19, two pointer variables p and q are declared.In line 20, we are initializing variables p and q to the root node.In line 21, the while loop is being started, inside the while loop we are checking theequality of the number inside the node (being pointed to by pointer p) with the numberbeing passed. The control is entered into the loop, if both the numbers are not equal and qis not pointing to NULL. This while loop will be terminated if the numbers in the twonodes are equal or end of a sub-tree is reached.At line 23 inside the loop, p is assigned the value of q.At line 24 inside the loop, if the number inside the node is smaller than the number in thenode pointed to by the pointer p. Then at line 25, we are getting the left sub-tree address(left) and assigning it to the pointer q.Otherwise, if this not the case, means the number in the node to be inserted is not smallerthan the number in the node pointed to by the pointer p. Then at line 27, the right sub-treeaddress is retrieved and assigned to pointer q.At line 30, the comparison is made to see if the both the values are equal (number in thenode to be inserted and the number inside the node pointed to by the pointer p). In casethey are equal, it displays a message with the number informing that a duplicate numberhas been found. This also means that the upper while loop was terminated because ofequality of the two numbers. In the line 33, the newly created node is deleted afterwards.At line 35, we are checking if the number in the newly constructed node is less than thenumber in the node at the end of a sub-tree. If this is so then the newly constructed nodeis inserted to the left of the tree node. The insertion code is at line 36.If the number in the newly constructed node is greater than the number in the tree node,the newly constructed node will be inserted to the right of the tree node as shown on line38. To make it clearer, let s see the same thing, the insert() method pictorially.Trace of insertWe will take the tree and the figure, we constructed above. At this time, we want to insertsome new numbers in the tree as given in the figure below: p 17 q 14 4 15 3 9 18 7 16 20 5 17, 9, 14, 5 Fig 12.9: Start of insertion of new node in the tree Page 10 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________Initially the pointers p and q are pointing to the start (root) of the tree. We want to insertthe number 17 in the tree. We compare the number in the root node (14) with number 17.Because number 17 is greater than the number 14, so as we did in the while loop in thefunction above, we will move toward the right sub-tree. In the next picture below, we cansee that the pointer q has moved to the right sub-tree of the root. p 17 14 4 q 15 3 9 18 7 16 20 5 17, 9, 14, 5 Fig 12.10: Insertion of a new node in progressAfter moving the pointer q forward, we make the pointer p point to the same node. We dothis operation as the first step inside the while loop. It can be seen at line 23 above. Sofollowing will be the latest position of pointers in the tree. 17 14 4 p 15 q 3 9 18 7 16 20 5 17, 9, 14, 5 Fig 12.11: Insertion of a new node in progress Page 11 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________Now, the number 15 in the tree node is compared with new number 17. Therefore, thepointer q is again moved forward to the right child node i.e. the node with number 18. Inthe next step, we move forward the p pointer also. The following figure depicts thecurrent picture of the tree: 17 14 4 15 3 9 p 18 q 7 20 16 5 17, 9, 14, 5 Fig 12.12: Insertion of a new node in progressThe previous process is repeated again (while loop is the repeating construct) that thenumber 17 is compared with the number 18. This time the left child node is traversed andq pointer starts pointing the node with number 16 inside. In the next step, p is movedforward to point to the same node as q.The same comparison process starts again, number 17 is found to be greater than thenumber 16, therefore, the right child is seek. But we see that the current tree node doesnot have right child node, so it returns NULL. Following figure depicts it. 17 14 4 15 3 9 18 7 20 p 16 5 q 17, 9, 14, 5 Fig 12.13: Insertion of a new node in progress Page 12 of 13
    • ecomputernotes.com Data Structures Lecture No. 12___________________________________________________________________Above shown (q is pointing to NULL) is the condition that causes the while loop in thecode above to terminate. Later we insert the new node as the right child of the currentnode. 14 4 15 3 9 18 7 20 p 16 5 node 17 17, 9, 14, 5 p->setRight( node ) ; Fig 12.14: A new node is inserted in the treeIt is recommended to execute this algorithm manually to insert the remaining numbers inthe tree. This will make the understanding more clear. This tree algorithm is going to beused rigorously in the future and complete understanding of it will make the complexfuture implementations easier to comprehend. The specific advantages of using binarytree over linked list will be discussed in more detail in the next lecture. Page 13 of 13