Binary tree

5,024 views

Published on

Published in: Technology, Business
3 Comments
4 Likes
Statistics
Notes
No Downloads
Views
Total views
5,024
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
383
Comments
3
Likes
4
Embeds 0
No embeds

No notes for slide

Binary tree

  1. 1. TreesNon-Linear Data Structure
  2. 2. Non-Linear Data Structure• These data structures donot have their elements in a sequence.• Trees is an example.• Trees are mainly used to represent data containing a hierarchical relationship between elements, ex : records, family trees and table of contents.
  3. 3. Terminology of Trees• The boxes on the tree are called nodes• The nodes immediately below (to the left and right of) a given node are called its children• The node immediately above a given node is called its parent• The (unique) node without a parent is called the root• A node with no children is called a leaf• Two children of the same parent are said to be siblings• A node can be an ancestor (e.g. grandparent) or a descendant (e.g. great-grandchild)
  4. 4. Some tree terminology• root: node with no parent – A non-empty tree has exactly one root• leaf: node with no children• siblings: two nodes with the same parent.• path: a sequence of nodes n1, n2, … , nk such that ni is the parent of ni+1 for 1 ≤ i < k – in other words, a sequence of hops to get from one node to another – the length of a path is the number of edges in the path, or 1 less than the number of nodes in it
  5. 5. Depth and height• depth or level: length of the path from root to the current node (depth of root = 0)• height: length of the longest path from root to any leaf – empty (null) trees height: -1 – single-element trees height: 0 – tree with a root with children: 1
  6. 6. Tree example
  7. 7. Trees• Tree nodes contain two or more links – All other data structures we have discussed only contain one• Binary trees – All nodes contain two links • None, one, or both of which may be NULL – The root node is the first node in a tree. – Each link in the root node refers to a child – A node with no children is called a leaf node
  8. 8. Binary Trees• Special type of tree in which every node or vertex has either no children, one child or two children.• Characteristics : • Every binary tree has a root pointer which points to the start of the tree. • A binary tree can be empty. • It consists of a node called root, a left subtree and right subtree both of which are binary trees themselves.
  9. 9. Examples : Binary Trees (1) (2) (3) (4) X X X X Y Z Y ZRoot of tree is node having info as X.(1) Only node is root.(2) Root has left child Y. A(3) Root X has right child Z.(4) Root X has left child Y and right child Z which is again a binary tree with its parent as Z and B C left child of Z is A, which in turn is parent for left child B and right child C.
  10. 10. Properties of Binary Tree• A tree with n nodes has exactly (n-1) edges or branches.• In a tree every node except the root has exactly one parent (and the root node does not have a parent).• There is exactly one path connecting any two nodes in a tree.• The maximum number of nodes in a binary tree of height K is 2K+1 -1 where K>=0.
  11. 11. Representation of Binary Tree• Array representation X – The root of the tree is stored in position 0. Y Z – The node in position p, is the implicit father of nodes 2p+1 and 2p+2. A – Left child is at 2p+1 and right at 2p+2. B C 0 1 2 3 4 5 6 7 8 9 10 11 12X Y Z A B C
  12. 12. Representation of Binary Tree• Linked List • Every node will consists of information, and two pointers left and right pointing to the left and right child nodes. struct node { int data; struct node *left; struct node *right; }; • The topmost node or first node is pointed by a root pointer which will inform the start of the tree. Rest of the nodes are attached either to left if less than parent or right if more or equal to parent.
  13. 13. • Diagram of a binary tree B A D C
  14. 14. Operations on Binary Tree• Searching an existing node.• Inserting a new node.• Deleting an existing node.• Traversing the tree. • Preorder • Inorder • Postorder
  15. 15. Search Process• Initialize a search pointer as the root pointer.• All the data is compared with the data stored in each node of the tree starting from root node.• If the data to be searched is equal to the data of the node then print “successful” search.• Else if the data is less than node’s data move the pointer to left subtree. Else data is more than node’s data move the pointer to right subtree.• Keep moving in the tree until the data is found or search pointer comes to NULL in which case it is an “unsuccessful” search.
  16. 16. Example used for Search 21 Root 18 7 19 6 9 8 11 14 13
  17. 17. Example : Search• Initialize temp as root pointer which is node having 21.• Loop until temp!=NULL or ITEM found • Compare item=9 with the root 21 of the tree, since 9<21, proceed temp to the left child of the tree. • Compare ITEM=9 with 18 since 9<18 proceed temp to the left subtree of 18, which is 7. • Compare ITEM=9 with 7 since 9>7 proceed temp to right subtree of the node which holds a value 7. • Now ITEM=9 is compared with the node which holds value 9 and since 9 is found the searching process ens here.
  18. 18. Insert Process• For node insertion in a binary search tree, initially the data that is to be inserted is compared with the data of the root data.• If the data is found to be greater than or equal to the data of the root node then the new node is inserted in the right subtree of the root node, else left subtree.• Now the parent of the right or left subtree is considered and its data is compared with the data of the new node and the same procedure is repeated again until a NULL is found which will indicate a space when the new node has to be attached. Thus finally the new node is made the appropriate child of this current node.
  19. 19. Example used for Insert 38 Root 14 56 23 45 82 8 18 70 20
  20. 20. Example : Insert• Suppose the ITEM=20 is the data part of the new node to be inserted in the tree and the root is pointing to the start of the tree.• Compare ITEM=20 with the root 38 of the tree. Since 20 < 38 proceed to the left child of 38, which is 14.• Compare ITEM=20 with 14, since 20 > 14 proceed to the right child of 14, which is 23.• Compare ITEM=20 with 23 since 20 < 23 proceed to the left child of 23 which is 18.• Compare ITEM=20 with 18 since 20 > 18 and 18 does not have right child, 10 is inserted as the right child of 18.
  21. 21. • Tree traversals: – Inorder traversal – prints the node values in ascending order 1. Traverse the left subtree with an inorder traversal 2. Process the value in the node (i.e., print the node value) 3. Traverse the right subtree with an inorder traversal – Preorder traversal 1. Process the value in the node 2. Traverse the left subtree with a preorder traversal 3. Traverse the right subtree with a preorder traversal – Postorder traversal 1. Traverse the left subtree with a postorder traversal 2. Traverse the right subtree with a postorder traversal 3. Process the value in the node
  22. 22. Binary tree traversals• three common binary tree traversal orderings (each one begins at the root): – preorder traversal: the current node is processed, then the nodes left subtree is traversed, then the nodes right subtree is traversed (CURRENT-LEFT-RIGHT) – in-order traversal: the nodes left subtree is traversed, then the current node itself is processed, then the nodes right subtree is traversed (LEFT-CURRENT-RIGHT) – postorder traversal: the nodes left subtree is traversed, then the nodes right subtree is traversed, and lastly the current node is processed (LEFT-RIGHT-CURRENT)
  23. 23. Binary tree preorder traversal• order: C F T B R K G – The trick: Walk around the outside of the tree and emit a nodes value when you touch the left side of the node. root "C" "F" "G" "T" "K" "B" "R"
  24. 24. Binary tree in-order traversal• order: B T R F K C G – The trick: Walk around the outside of the tree and emit a nodes value when you touch the bottom side of the node. root "C" "F" "G" "T" "K" "B" "R"
  25. 25. Binary tree postorder traversal• order: B R T K F G C – The trick: Walk around the outside of the tree and emit a nodes value when you touch the right side of the node root "C" "F" "G" "T" "K" "B" "R"
  26. 26. Delete Process• There are four possible conditions are to be taken into account : i. No node in the tree holds the specified data. ii. The node containing the data has no children. iii. The node containing the data has exactly one child. iv. The node containing data has two children.• Condition (i) In this case we simply print the message that the data item is not present in the tree.• Condition (ii) In this case since the node to be deleted has no children the memory occupied by this should be freed and either the left link or the right link of the parent of this node should be set to NULL. Which of these is to be set to NULL depends upon whether the node being deleted is a left child or right child of its parent.
  27. 27. Condition (iii) • In this case since the node to be deleted has one child the solution is to adjust the pointer of the parent of the node to be deleted such that after deletion it points to the child of the node being deleted as in the diagram. Root Root 21 21 18 24 18 24 7 19 26 7 19 266 9 25 30 6 11 25 30 11 10 10
  28. 28. Condition (iv) In this case the node to be deleted has two children. Consider node 9 before deletion. The inorder successor of the node 9 is node 10. The data of this inorder successor should now be copied into the node to be deleted and a pointer should be set up pointing to the inorder successor (node 10). This inorder successor would always have one or zero child. It should then be deleted using the same procedure as for deleting one child or a zero child node. Thus, the whole logic of deleting a node with two children is to locate the inorder successor, copy its data and reduce the problem to a simple deletion of a node with one or zero child. This is shown in the example. Root Root 21 21 18 24 18 24 7 19 26 7 19 266 9 25 30 6 10 25 30 8 11 8 11 10 Inorder successor of node 9
  29. 29. Deletion• Condition (i) Print message data not found.• Condition (ii) Since no children so free the node & make either parent->left=NULL or parent- >right=NULL. Parent A A Parent left right X B C X In the example1, In the example2, parent->left==x so free(x) and parent->right==x so free(x) and make parent->left=NULL. make parent->right=NULL.
  30. 30. Deletion• Condition (iii) Adjust parent to point to the child of the deleted node.I. Node deleted(X) has only left child : 1) If(parent->left==x) parent->left=x->left. 2) If(parent->right==x) parent->right=x->left. Parent A A Parent left right X B B X left right C C
  31. 31. Deletion• Condition (iii) Adjust parent to point to the child of the deleted node.II. Node deleted(X) has only right child : 1) If(parent->left==x) parent->left=x->right. 2) If(parent->right==x) parent->right=x->right. Parent A A Parent left right X B B X right right C C
  32. 32. Deletion• Condition (iv) solution more complex as X has two children,• Find inorder successor of the node X. Inorder successor of any node will be first go to right of the node and then from that node keep going left until NULL encountered, that will be the inorder successor.• Copy data of inorder successor to node X’s data.• Place pointer at the inorder successor node. Now the inorder succesor will have zero or one child only (so the complex problem is reduced to condition (iii)).• Delete the inorder successor node by using logic of condition (ii) if no children or condition (iiii) if one right child. Parent A Parent A left left X B E X left right left right C D C D left left Here inorder successor E has E no children condition (ii) E
  33. 33. DeletionParent A Parent A Parent A left left leftX B E Eleft right left right left right C D C D C D left left left E Inorder X E Inorder Successor Successor F right right left right F F G H left right left right Here inorder successor E has G H G H one right child condition (iii) If(parent->left==x) parent->left=x->right.
  34. 34. Application of Tree• Expression Tree• Game Tree

×