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 Tree Edges Nodes Non - Tree
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
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
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
BINARY TREE REPRESENTATION A Binary Tree of Size 9 and Height 3, with a Root 2
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
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
Algebraic Expression E = (a – b) / ((c * d) + e) / - + c eab d *
Formula: Node K, 2*K, 2*K+1 and K/ Depth d n : n Nodes, D n = log 2 n +1
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
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
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
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
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
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.
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
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
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
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
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 ITEM = 20 Inserted
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
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
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
Case 1: Deletion with No Child Null Before DeletionAfter Deletion
Case 2: Deletion with One Child Before Deletion After Deletion To Be Deleted
Case 3: Deletion with Two Children Before DeletionAfter Deletion To Be Deleted Successor of 25
Case 3: Deletion with Successor Child Before DeletionAfter Deletion To Be Deleted Successor of 75 Successor Right Child
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
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
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.
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 ]
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
New Heap: 44, 30, 50, 22, 60, 55, 77,
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.
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
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
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.