1 / 39

Trees

Trees. Binary Trees Extended Binary Trees. Tree. A Tree consist of Nodes connected by Edges. Node is represented by Circle Edges as Lines or Arrows A Tree can have only One Root There Must be One and Only One Path from the Root to Any Other Node. Nodes. Tree. Non - Tree. Edges.

tanaya
Télécharger la présentation

Trees

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. Trees Binary Trees Extended Binary Trees

  2. Tree A Tree consist of Nodes connected by Edges. Node is represented by Circle Edges as Lines or Arrows A Tree can have only One Root There Must be One and Only One Path from the Root to Any Other Node Nodes Tree Non - Tree Edges

  3. Terminology Path: Nodes connected by Edges form a Path Root: Top node of the Tree Parent: The Above Node of any Node Child: Downward Node of any Node Leaf: Node with No Child Node Level: How many Generation the Node is from the Root Key: The value used to find any Node in a tree

  4. A B F J G D E K H A is the Root B is the parent of D and E Level 0 The dashed line is a path D is the left child of B C E is the right child of B Level 1 Level 2 Level 3 J and K are siblings F is the Root of a subtree H, E, J, K and G are leaf nodes

  5. BINARY TREES Binary Tree ‘T’ is define as a Finite set of Elements called Nodes T is Empty – Null or Empty Tree T has a Root Node ‘R’ T1 and T2 will be the Left and Right Subtrees If T1 and T2 are Nonempty, their Roots will called Left and Right Successors of R Any Node can have 0, 1 or 2 Successors. Node with No Successors are called Terminal Nodes

  6. BINARY TREE REPRESENTATION 2 7 5 4 9 2 6 8 5 A Binary Tree of Size 9 and Height 3, with a Root 2

  7. Definitions An Arrow (Edge) is Refers to the link from the Parent to the Child. Sequence of Edges forms a Path. The Root Node of a Tree is the Node with No Parents A Leaf or Terminal Node has No Children The Depth of a Node N is the Length of the Path from the Root to the Node. The set of all Nodes at a Given Depth is sometimes called a Level of the Tree. The Root Node is at Depth Zero. The Height of a Tree is the Depth of its Furthest Leaf A Tree with only a Root node has a Height of Zero Siblings are Nodes that Share the same Parent Node The size of a Node is the Number of Descendants it has Including Itself Root is the only Node in the Tree with in-Degree = 0

  8. A A 1 3 B B C D C D E E F F 4 2 G H G H 1,3 and 4 are Similar. 1 and 3 are Copies – 2 and 4 Are Neither Similar Nor Copy.

  9. Algebraic Expression E = (a – b) / ((c * d) + e) a b - / + * e c d

  10. Formula: Node K, 2*K, 2*K+1 and K/2 4 5 2 1 3 6 7 8 9 Depth dn: n Nodes, Dn = log2 n +1

  11. Representation in Memory By a Single Array (Formula Discussed) By Link List Root Location of the Root INFO[K] Data of the Node N LEFT[K] Left Child of Node N RIGHT[K] Right Child of Node N Root A B C D E

  12. Traversing Binary Trees Preorder: Process the Root R Traverse the Left Subtree of R Traverse the Right Subtree of R Inorder: Traverse the Left Subtree of R Process the Root R Traverse the Right Subtree of R Postorder: Traverse the Left Subtree of R Traverse the Right Subtree of R Process the Root R

  13. Preorder Inorder Postorder A C B D E F G H J Preorder : A B D E C F H J G Inorder : D B E A H F J C G Postorder : D E B H J F G C A

  14. E = [a + (b – c)] * [(d – e) / (f + g – h)] * + / - a - - c d e b h + g f Preorder : * + a – b c / - d e - + f g h Postorder : a b c - + d e – f g + h - / *

  15. Preorder Traversing Initially push NULL onto Stack and Set PTR:= Root Process the Left Path, Push Right Child in Stack. Ends when Node has No Left Child [Backtracking] Pop and Assign PTR to the TOP Element, If PTR ≠ NULL then Return to Step (a) otherwise Exit. Initially push NULL onto Stack (Stack= 0) Set PTR = A Root of T Process A; Push C in Stack (Stack=0,C) Process B; No Right Child Process D; Push H in Stack (Stack=0,C,H) Process G; No Left, Right Child Pop H from Stack Set PTR := H (Stack=0,C) Return to Step (a) Process H; Push K in Stack (Stack=0,C,K) Pop K from Stack Continues………. Output: A, B, D, G, H, K, C, E, F A C B E D F G H K

  16. PREORDER (INFO, LEFT, RIGHT, ROOT) - Algorithm STACK is used to Temporarily hold the Addresses of Nodes [Push NULL onto STACK and Initialize PTR] Set TOP : = 1, STACK[1] : = NULL and PTR : = ROOT Repeat Step 3 to 5 while PTR ≠ NULL Apply PROCESS to INFO[PTR] [Right Child ?] If Right Child ≠ NULL then [Push on STACK] Set TOP := TOP + 1 and STACK[TOP] := RIGHT[PTR] [End of If Structure] [Left Child ?] If LEFT [PTR] ≠NULL then: Set PTR := LEFT [PTR] Else [Pop from STACK] Set PTR := STACK [TOP] and TOP := TOP - 1 [End of If Structure] [End of Step 2 loop] Exit.

  17. Postorder Traversing Initially push NULL onto Stack and Set PTR:= Root Process the Left Path, Push all Nodes N in Stack; and If N has a Right Child R(N) Push –R(N) [Backtracking] Pop and Process Positive Nodes. Assign PTR to the TOP Element, If PTR = -VE, Set PTR +VE then Return to Step (a) otherwise Exit. Initially push NULL onto Stack (Stack= 0) Set PTR = A Root of T, Push A, -C, B, D, -H, G, K Stack = 0, A, -C, B, D, -H, G, K Pop and Process K then G, Only Pop –H PTR = - H, Reset PTR = H and Push it in Stack Step (a) Stack = 0, A, -C, B, D, H, -M, L PTR = - M, Reset PTR = M Stack = 0, A, -C, B, D, H, M Continues………. Output: K, G, L, M, H, D, B, E, C, A A C B E D H G L M K

  18. POSTORDER (INFO, LEFT, RIGHT, ROOT) - Algorithm [Push NULL onto STACK and Initialize PTR] Set TOP : = 1, STACK[1] : = NULL and PTR : = ROOT [Push Left-most Path in Stack] Repeat Step 3 to 5 while PTR ≠ NULL Set TOP := TOP + 1 and STACK[TOP] := PTR [Pushes PTR on Stack] If Right [PTR] ≠ NULL then [Push on STACK] Set TOP := TOP + 1 and STACK[TOP] := - RIGHT[PTR] [End of If Structure] Set PTR : = LEFT[PTR] [Updates Pointer PTR] [End of Step 2 loop] Set PTR := STACK[TOP] and TOP := TOP – 1 [Pops node from STACK] Repeat while PTR > 0 (a) Apply Process to INFO[PTR] (b) Set PTR := STACK[ TOP] and TOP := TOP – 1 [Pops node from STACK] [End of Loop] If PTR < 0 then (a) Set PTR := - PTR (b) Go to Step 2 [End of If Structure] Exit

  19. Binary Search Tree Why Use Binary Search Tree Combines the advantages of Ordered Array and Linked List Searching Equals Compared to Ordered Array Fast Compared to Linked List Insert and Delete Item Fast Compared to Ordered Array Equals Compared to Linked List

  20. Binary Search Tree Properties The value of Node ‘N’ is Greater then every value in the Left Subtree The value of Node ‘N’ is Less then every value in the Right Subtree Inorder Traversing Produce a Sorted List 38 14 56 23 45 82 8 18 70

  21. Searching and Insertion Compare ITEM with the Root Node ‘N’ If ITEM < N, Proceed to the Left Child of N If ITEM > N, Proceed to the Right Child of N Repeat (a) until one of the following occurs ITEM found ITEM not found, Insert ITEM 38 ITEM = 20 Inserted 14 56 23 45 82 8 18 70 20

  22. FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR) - Algorithm LOC = NULL and PAR = NULL :Tree is Empty LOC ≠ NULL and PAR = NULL: ITEM is Root of Tree LOC = NULL and PAR ≠ NULL: ITEM is not in Tree, Added using PAR [Tree Empty ?] If Root = NULL; Set LOC := NULL and PAR := NULL. Return [ITEM at Root?] If ITEM = INFO[Root]; Set LOC:=Root and PAR := NULL. Return [Initialize ITEM and SAVE] If ITEM < INFO[Root] then Set PTR := LEFT[Root] and SAVE := Root Else Set PTR := RIGHT[Root] and SAVE := Root [End of If Structure] Repeat Steps 5 and 6 while PTR ≠ NULL [ITEM Found?] If ITEM < INFO[PTR] then Set LOC :=PTR and PTR:=SAVE. Return If ITEM < INFO [PTR] then Set SAVE:=PTR and PTR:= LEFT[PTR] Else Set SAVE := PTR and PTR:= RIGHT[PTR] [End of If Structure] [End of Step 4 Loop] [Search Unsuccessful] Set LOC := NULL and PAR := SAVE Exit

  23. INSBST(INFO, LEFT, RIGHT, ROOT, ITEM, LOC) Call FIND (INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR) If LOC ≠ NULL then Exit [Add ITEM to Tree] Set New := Create Node Set INFO[New] := ITEM Set RIGHT[New] := NULL and LEFT[New] := NULL If PAR = NULL then Set Root := New Else if ITEM < INFO[PAR] then Set LEFT[PAR] := NEW Else Set RIGHT[PAR] := NEW [End of If Structure] Exit

  24. Deletion in a Binary Tree • Case 1: Node N has No Children • Replace the Pointer in Parent Node by NULL • Case 2: Node N has Exactly One Child • Replace the Pointer in Parent Node by its Only Child • Case 3: Node N has Two Children • Find the Inorder Successor • Remove the Inorder Successor by using Case 1 or Case 2 • Replace the Node N by the Inorder Successor

  25. Case 1: Deletion with No Child 3 7 3 7 5 5 10 10 Null Before Deletion After Deletion

  26. Case 2: Deletion with One Child 48 48 71 63 52 52 80 80 To Be Deleted 67 63 67 After Deletion Before Deletion

  27. Case 3: Deletion with Two Children 15 15 35 35 25 30 50 50 To Be Deleted 40 40 30 20 20 5 5 Successor of 25 Before Deletion After Deletion

  28. Case 3: Deletion with Successor Child 62 62 87 87 75 77 50 50 To Be Deleted 93 93 77 79 Successor Right Child Successor of 75 79 Before Deletion After Deletion

  29. Procedure: CASEA (INFO,LEFT,RIGHT,ROOT,LOC,PAR) Deletes N at LOC where N has No 2 Children, PAR gives the Parent of N or PAR = NULL Shows N is Root. CHILD Shows the Only Child of N or CHILD = NULL [Initialize CHILD] If LEFT[LOC] = NUL and RIGHT[LOC] = NULL then: Set CHILD := NULL Else if LEFT[LOC] ≠, then: Set CHILD := LEFT[LOC] Else Set CHILD := RIGHT[LOC]. [End of If Structure] If PAR ≠ NULL, then: If LOC = LEFT[PAR], then: Set LEFT[PAR] := CHILD Else Set RIGHT[PAR] := CHILD. [End of If Structure] Else: Set ROOT : = CHILD [End of If Structure] Return

  30. Procedure:CASEB (INFO,LEFT,RIGHT,ROOT,LOC,PAR) Deletes N at LOC where N has 2 Children, PAR gives the Parent of N or PAR = NULL Shows N is Root. SUC Shows the Inorder Successor and PARSUC Shows the Parent of Successor [Find SUC and PARSUC] (a) Set PTR := RIGHT[LOC] and SAVE := LOC. (b) Repeat while LEFT[PTR] ≠ NULL: Set SAVE := PTR and PTR:=LEFT[PTR] [End of Loop] (c) Set SUC := PTR and PTR := SAVE [Delete Inorder Successor using Previous Procedure] [Replace Node N by its Inorder Successor] (a) If PAR ≠ NULL, then: If LOC = LEFT[PAR], then: Set LEFT[PAR : =SUC Else: Set RIGHT[PAR] := SUC [End of If Structure] Else: Set ROOT := SUC [End of If Structure] (b) Set LEFT [SUC] := LEFT[LOC] and RIGHT[SUC] := RIGHT[LOC] Return

  31. Algorithm: DEL (INFO,LEFT,RIGHT,ROOT,ITEM) This Algorithm Deletes the Given ITEM of Information from the Binary Search Tree T. [Find the Location of ITEM and its Parent] Call FIND (INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR) [ITEM in Tree?] If LOC = NULL then Write Item not in Tree and Exit. [Delete Node Containing ITEM] If RIGHT[LOC] ≠ NULL and LEFT[LOC] ≠ NULL, then: Call CASEB (INFO,LEFT,RIGHT,ROOT,LOC,PAR). Else call CASEA (INFO,LEFT,RIGHT,ROOT,LOC,PAR). [End of If Structure] Exit.

  32. HEAP – HEAP SORT • A Complete Binary Tree H can be maxheap or minheap If: • The Value of Node N is Greater then or Equal to the Value of Each of its Children (MaxHeap) • The Value of Node N is Less then or Equal to the Value of Each of its Children (MinHeap) • H[ 1 ] is the root , H[ 2k ], H[ 2k + 1 ] • Parent of Any Non root Node j: H[ j / 2 ] 97 95 66 55 95 35 48 55 62 77 25 38 18 40 88 48 66 30 26 24 1 3 4 5 6 9 10 11 12 13 14 15 16 17 2 7 8 18 19 20

  33. 97 95 66 55 95 35 48 55 62 77 25 38 18 40 88 48 66 30 26 24 1 3 4 5 6 9 10 11 12 13 14 15 16 17 2 7 8 18 19 20 97 88 95 66 95 55 48 66 48 62 25 35 55 77 38 18 30 40 26 24

  34. Insertion in a Heap • First Set the New ITEM on the Available Location i.e. If ITEM = 70 its Location is H[21] • Then Compare the ITEM with its Parents • If Parent is Greater Then ITEM Leave it • Else Swap Them • 70 is the Right Child of H[10] = 48 • Compare 70 with 48 and Interchange • Compare 70 with 55 and Interchange • Compare 70 with 88 and Leave

  35. New Heap: 44, 30, 50, 22, 60, 55, 77, 55 44 30 77 50 55 60 44 30 50 44 30 45 22

  36. INSHEAP( TREE, N, ITEM) H is a Heap with N items in which ITEM will be inserted. PTR will Find the Location of ITEM and PAR will be the Parent Location 1. [Add New Node to H and Initialize PTR] Set N := N + 1 and PTR := N 2. [Find Location to Insert ITEM] Repeat Steps 3 to 6 while PTR > 1 3. Set PAR = [PAR / 2] [Location of Parent Node] 4. If ITEM <= TREE [PAR] then: Set TREE[PAR] := ITEM and Return [End of If Structure] 5. Set TREE[PTR] := TREE[PAR] [Move Node Down] 6. Set PTR := PAR [Update PTR] [End of Step 2 Loop] 7. [Assign ITEM as the ROOT of H] Set TREE[1] := ITEM 8. Return.

  37. Deletion in a Heap • Deletion of the Root • Replace the Root with the Last Value L of the Heap • L will Sink to its Proper Position 95 85 85 55 70 70 55 30 22 30 33 65 33 65 15 15 15 15 20 22 20

  38. DELHEAP (TREE,N,ITEM) • Set LAST := TREE[N] and N:= N-1 [remove the Last Node] • Set PTR := 1, LEFT := 2 and RIGHT := 3 [Initialize Pointers] • Repeat Step 4 to 7 while RIGHT < = N • If LAST >= TREE[LEFT] and LAST >= TREE[RIGHT] then: Set TREE [PTR] := LAST and Return [End of If Structure] • If TREE[RIGHT] <= TREE[LEFT] then: Set TREE[PTR] := TREE[LEFT] and PTR := LEFT Else: Set TREE[PTR] := TREE[RIGHT] and PTR := RIGHT [End of If Structure] • Set LEFT := 2 * PTR and RIGHT := LEFT + 1 [End of Step 4 Loop] 7. If LEFT = N and If LAST < TREE[LEFT], then Set PTR:= LEFT 8. Set TREE[PTR] := LAST 9. Return

  39. HEAPSORT (A, N) An array A with N Elements is given. • [Build a Heap H] Repeat for J = 1 to N – 1: Call INSHEAP(A, J, A[J+1]) [End of Loop] • [Sort A by repeatedly deleting the Root of H] Repeat while N > 1: a) Call DELHEAP(A, N, ITEM) b) Set A[N +1] := ITEM [End of Loop] 3. Exit.

More Related