Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 Tree Edges Nodes Non - Tree

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 BC J GDE K H F A is the Root E is the right child of B D is the left child of B B is the parent of D and E The dashed line is a path F is the Root of a subtree Level 0 Level 1 Level 2 Level 3 J and K are siblings 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 75 4 926 85 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 1,3 and 4 are Similar. 1 and 3 are Copies – 2 and 4 Are Neither Similar Nor Copy. B CD E F GH A B CD E F GH 13 2 4

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

10 Formula: Node K, 2*K, 2*K+1 and K/2 1 2 3 8 745 9 6 Depth d n : n Nodes, D n = log 2 n +1

11 Representation in Memory By a Single Array (Formula Discussed) By Link List –RootLocation 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 C E B D

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 PreorderInorder Postorder A B C H GDE J F 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)] Preorder: * + a – b c / - d e - + f g h Postorder : a b c - + d e – f g + h - / * * + / - b a - cde - + f g h

15 Preorder Traversing Initially push NULL onto Stack and Set PTR:= Root (a)Process the Left Path, Push Right Child in Stack. Ends when Node has No Left Child (b)[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 B C HG D E K F

16 PREORDER (INFO, LEFT, RIGHT, ROOT) - Algorithm STACK is used to Temporarily hold the Addresses of Nodes 1.[Push NULL onto STACK and Initialize PTR] Set TOP : = 1, STACK[1] : = NULL and PTR : = ROOT 2.Repeat Step 3 to 5 while PTR ≠ NULL 3.Apply PROCESS to INFO[PTR] 4.[Right Child ?] If Right Child ≠ NULL then [Push on STACK] Set TOP := TOP + 1 and STACK[TOP] := RIGHT[PTR] [End of If Structure] 5.[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] 6.Exit.

17 Postorder Traversing Initially push NULL onto Stack and Set PTR:= Root (a)Process the Left Path, Push all Nodes N in Stack; and If N has a Right Child R(N) Push –R(N) (b)[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 B C H G D L M K E

18 POSTORDER (INFO, LEFT, RIGHT, ROOT) - Algorithm 1.[Push NULL onto STACK and Initialize PTR] Set TOP : = 1, STACK[1] : = NULL and PTR : = ROOT 2.[Push Left-most Path in Stack] Repeat Step 3 to 5 while PTR ≠ NULL 3.Set TOP := TOP + 1 and STACK[TOP] := PTR [Pushes PTR on Stack] 4.If Right [PTR] ≠ NULL then [Push on STACK] Set TOP := TOP + 1 and STACK[TOP] := - RIGHT[PTR] [End of If Structure] 5.Set PTR : = LEFT[PTR] [Updates Pointer PTR] [End of Step 2 loop] 6.Set PTR := STACK[TOP] and TOP := TOP – 1 [Pops node from STACK] 7.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] 8.If PTR < 0 then (a) Set PTR := - PTR (b) Go to Step 2 [End of If Structure] 9.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 70 82 8 2345 18

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

22 FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR) - Algorithm i.LOC = NULL and PAR = NULL :Tree is Empty ii.LOC ≠ NULL and PAR = NULL: ITEM is Root of Tree iii.LOC = NULL and PAR ≠ NULL: ITEM is not in Tree, Added using PAR 1.[Tree Empty ?] If Root = NULL; Set LOC := NULL and PAR := NULL. Return 2.[ITEM at Root?] If ITEM = INFO[Root]; Set LOC:=Root and PAR := NULL. Return 3.[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] 4.Repeat Steps 5 and 6 while PTR ≠ NULL 5.[ITEM Found?] If ITEM < INFO[PTR] then Set LOC :=PTR and PTR:=SAVE. Return 6.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] 7.[Search Unsuccessful] Set LOC := NULL and PAR := SAVE 8.Exit

23 INSBST(INFO, LEFT, RIGHT, ROOT, ITEM, LOC) 1.Call FIND (INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR) 2.If LOC ≠ NULL then Exit 3.[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] 4.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 10 5 37 5 37 Null Before DeletionAfter Deletion

26 Case 2: Deletion with One Child Before Deletion After Deletion 80 52 67 4871 63 To Be Deleted 80 52 67 4863

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

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

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 1.[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] 2.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] 3.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 1.[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 2.[Delete Inorder Successor using Previous Procedure] 3.[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] 4.Return

31 Algorithm: DEL (INFO,LEFT,RIGHT,ROOT,ITEM) This Algorithm Deletes the Given ITEM of Information from the Binary Search Tree T. 1.[Find the Location of ITEM and its Parent] Call FIND (INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR) 2.[ITEM in Tree?] If LOC = NULL then Write Item not in Tree and Exit. 3.[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] 4.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 ] 1 2 3456 78 91011121314151617 181920 97 88 95665595 4866 354855627725381840 302624

33 1 2 3456 78 91011121314151617 181920 97 88 95665595 4866 354855627725381840 302624 97 8895 55 48 24 66 35 66 26 30 40 18 48 38 25 95 77 62

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 77 60 45 44 55 30 50 22 44 30 50 30 44

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 70 65 30 85 33 55 15 22 15 20 85 70 65 30 55 33 22 15 20

38 DELHEAP (TREE,N,ITEM) 1.Set LAST := TREE[N] and N:= N-1 [remove the Last Node] 2.Set PTR := 1, LEFT := 2 and RIGHT := 3 [Initialize Pointers] 3.Repeat Step 4 to 7 while RIGHT < = N 4.If LAST >= TREE[LEFT] and LAST >= TREE[RIGHT] then: Set TREE [PTR] := LAST and Return [End of If Structure] 5.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] 6.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. 1.[Build a Heap H] Repeat for J = 1 to N – 1: Call INSHEAP(A, J, A[J+1]) [End of Loop] 2.[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.


Download ppt "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."

Similar presentations


Ads by Google