Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic expression can be easily represented using Binary Tree. ( A + B ) + AB Operands Operator Traversals? InOrder PreOrder PostOrder Infix Prefix Postfix A + B + A B A B + Operators normally have maximum of 2 operands.
Types of Binary Trees Expression Tree: –A binary tree which stores an, Arithmetic expression where, Operands are stored as, –Leaves / Leaf nodes / External nodes of an expression tree and, Operators are stored as, –Internal nodes of an expression tree. –Traversals: InOrder: –Gives Infix notation of arithmetic expression. PreOrder: –Gives Prefix notation. PostOrder: –Gives Postfix notation of arithmetic expression.
Expression Tree Draw the expression tree for expression ‘(A + B)’. Infix Expression:( A + B ) Step-1 Postfix Expression: Postfix A B + 1) A A ) B A B 3) + A B 4000 (Operator)(Operand) Evaluate it for A = 5, B = 6.
Expression Tree Draw the expression tree for expression ‘(A + B)’ and evaluate it for A = 5, B = 6. Infix Expression:( A + B ) Step-1 Postfix Expression: Postfix A B + AAB+ AB Evaluation? ) A2) B3) +
Expression Tree ( ( A + B ) * ( C – D ) )A B + C D – * Postfix A B + C D – * A [A = 1, B = 2, C = 4, D = 2] AB+ AB C+ AB C+ AB D + AB - CD + AB - CD * + AB - CD * = AB+ CD -*
Expression Tree a * ( b + c ) / d ( ( a * ( b + c ) ) / d ) ( a * ( ( b + c ) / d ) ) a b c + * d / a b c + d / * a b c + * d / aab ab ab c c a+ + b c * * a+ b c d * a+ b c d / * a+ b c d / Postfix Evaluate: a = 2, b = 1, c = 1, d =
Expression Tree Draw the expression tree for following expression: ( a + b ) * c – d ^ e Evaluate the following expression tree for A = 4, B = 15, C = 5, D = 2, E = 3 + /* CBA^ DE
Types of Binary Trees A B C D EF G Main Disadvantage? 7 nodes 8 null links Linked Representation of a Binary Tree
Types of Binary Trees Threaded Binary Tree: –If there are n (n>0) nodes in a binary tree, No. of null link fields in linked representation would be: –n+1 –More than 50% of link fields are with null values and hence, Lot of memory space is wasted. –One way to use these null link fields has been given by Perlis & Thornton. Store pointers / links of some nodes in these null link fields. These extra pointers / links are called: –Threads
Threaded Binary Tree A B C D EF G NULL Inorder traversal: B A G E D F C Inorder Threading
Threaded Binary Tree A B C D EF G Preorder traversal: A B C D E G F Preorder Threading NULL
Types of Binary Trees Threaded Binary Tree: –3 ways to thread a binary tree: Inorder threading: –Left null link points to Inorder predecessor. –Right null link points to Inorder successor. Preorder threading: –Left null link points to Preorder predecessor. –Right null link points to Preorder successor. Postorder threading: –Left null link points to Postorder predecessor. –Right null link points to Postorder successor.
Types of Binary Trees Threaded Binary Tree: –Advantages: Traversal operation is faster. –Non-recursive traversal can be easily implemented and, –Non-recursive algorithm will work faster than recursive algorithm. Predecessor and Successor (Inorder / Preorder / Postorder) can be easily determined for any node.
Types of Binary Trees Threaded Binary Tree: –Disadvantage: How to distinguish / identify a link from a thread? –How to tell which is a link and which is a thread as both will contain pointers to nodes. 2 ways to overcome this disadvantage: –1] Node structure with 2 extra fields. »LTAG = 1: Pointer of LC is a Thread. »LTAG = 0: Pointer of LC is a Link. –Disadvantage: »2 extra bytes for TAG’s are required in each node. LTAGLCDATARCRTAG
Types of Binary Trees Threaded Binary Tree: –Disadvantage: How to distinguish / identify a link from a thread? –How to tell which is a link and which is a thread as both will contain pointers to nodes. 2 ways to overcome this disadvantage: –2] Store +ve value for Link and –ve value for Thread. »Get the actual pointer value by taking the magnitude / mod of the pointer. –Disadvantage: »Not possible in a programming language that does not allow –ve valued pointers.
Types of Binary Trees Tree T1 Tree T2 Tree T3 Heap Tree
Types of Binary Trees Heap Tree: –Suppose H is a complete binary tree. –It will be called a heap tree if it satisfies following property: For each and every node in H, –Value at N is greater than or equal to the value of each of children of N. OR For each and every node in H, –N has a value which is greater than or equal to the value of every successor of N.
Heap Tree Tree T1 Tree T2 Tree T3 No Yes Condition not satisfied for node 20. Not a complete binary tree. Which is a heap tree?
Heap Tree Tree T1 Yes Tree T2 Yes Max HeapMin Heap Max Value Min Value Which is a heap tree?
Types of Binary Trees Heap Tree: –Suppose H is a complete binary tree. –It will be called a heap tree if it satisfies following property: For each node in H, –Value at N is greater (or smaller) than or equal to the value of each of children of N. OR For each node in H, –N has a value which is greater (or smaller) than or equal to the value of every successor of N.
Heap Tree Representation of Heap Tree: –Any binary tree can be represented in the form of: Array (Sequential Representation) Linked List (Linked Representation) –Which representation is better for a Heap tree? ‘Array representation’ has certain advantages over ‘Linked representation’ in case of Heap tree. –There will be no / less memory wastage because, »A heap tree is a complete binary tree. –No empty space between 2 non-null entries. »If there are NULL entries, they will be at the tail of the array.
Heap Tree Max HeapMin Heap Index Value Array representation of Heap Tree
Heap Tree Max Heap Max Value Insert value Insertion into a Heap Tree
Heap Tree Max Heap Max Value Insert value Insertion into a Heap Tree
Heap Tree Max Heap Max Value Insert value Insertion into a Heap Tree
Heap Tree Insertion into a Heap Tree: –Exercise: Form / Build a Max Heap with the following data: –19, 55, 44, 98, 67, 48, 95, 66, 70, 69, 30, 24, 99, 82
Types of Binary Trees Max Heap Max Value Delete a value Any node can be deleted, but deleting root node has some special importance. Deletion from a Heap Tree
Types of Binary Trees Deletion from a Heap Tree: –Importance of deletion of root node: Deleting root node continuously from max heap gives: –Data sorted in descending order. Deleting root node continuously from min heap gives: –Data sorted in ascending order. –This sorting of data using a heap tree is called: Heap Sort
Heap Sort: –Works on the basic concept of: Heap Tree –It is always a complete binary tree. –Stored in the form of array. –2 types: »Max Heap »Min Heap –Basic steps in a heap sort (using max heap) are: Create heap from the array which is to be sorted. Repeat the following steps till heap tree is empty: –Delete root node from heap tree. –Rebuild heap after deletion. –Place the deleted node in output.
1. Create Heap Array A Array B Output Array A
2. Sort the Heap Heap Array A Array B Array B would not be required Directly swap the root with the last element in the heap tree.
2. Sort the Heap Heap Array A Iteration-1 Nij Iteration-2 ij
2. Sort the Heap Heap Array A Iteration-3 Nij Iteration-4 ij OUTPUT
Heap Sort BuildMaxHeap(A) For i = N down to 2 Swap( A[ 1 ], A[ i ] ) For j = 1 to i – 1 lchild = 2 * j rchild = 2 * j + 1 If( A[ j ] A[ rchild ] ) Swap(A[ j ], A[ lchild ]) j = lchild Else If( A[ j ] A[ lchild] ) Swap(A[ j ], A[ rchild ]) j = rchild Else Break; EndIf EndFor Create Max Heap 1 st iteration: Swap 1 st with last. 2 nd iteration: Swap 1 st with second last. And so on... Check the position of newly placed element with leftchild and rightchild. Rebuild the heap until the newly placed element comes to proper position.
Heap Sort Algorithm: –HeapSort Input: –Array A which is to be sorted. Output: –Array A with elements sorted in ascending order. Data Structure: –Array.
Heap Sort Steps BuildMaxHeap(A) For i = N down to 2 Swap( A[ 1 ], A[ i ] ) For j = 1 to i – 1 lchild = 2 * j rchild = 2 * j + 1 If( A[ j ] A[ rchild ] ) Swap(A[ j ], A[ lchild ]) j = lchild Else If( A[ j ] A[ lchild] ) Swap(A[ j ], A[ rchild ]) j = rchild Else Break; EndIf EndFor
Types of Binary Trees Binary Search Tree (BST) Heap Tree Tree T1Tree T2Tree T3
Types of Binary Trees Binary Search Tree (BST): –A binary tree is called a Binary Search Tree if, Each node N satisfies following property: The value at N is, –Greater than every value in the left sub-tree of N and is, –Less than every value in the right sub-tree of N.
Binary Search Tree (BST) Which is a BST? No Heap Tree (Min Heap) No Not satisfied for node with value 30. Tree T1Tree T2Tree T3 Yes
Binary Search Tree (BST) Which is a BST? Yes. Lexicographical ordering. Jan DecMar AugFeb JulNov Jun (Not numerical ordering)
Binary Search Tree (BST) Applications of BST Smallest / Minimum value?15 2. Largest / Maximum value?88 3. Search Value 54?Found. 4. Search Value 90?Not Found. 5. InOrder Traversal: Data sorted in Ascending Order. Hence also known as: Binary Sorted Tree.
Types of Binary Trees Creation of BST / Insertion into BST 20, 10, 30 20, 30, 10 10, 20, 30 10, 30, 20 30, 10, 20 30, 20, For a different sequence of the same set of data, there will be a different BST. Conclusion:
Binary Search Tree (BST) Applications of BST: –To find out the minimum / maximum value from a set of data. –To search a particular data from a set of data. –To sort the data and hence a BST is also called, Binary Sorted Tree. –To remove duplicate values from set of data.
Binary Search Tree (BST) Create a BST for data '57, 28, 25, 69, 88, 81, 74, 65, 19, 15'
Types of Binary Trees Binary Search Tree (BST): –Consider the following set of data: 10, 20, 30, 40, 50, 60 –No. of ways to arrange this data: 6! = 720 Some ways to arrange the data are: –10, 20, 30, 40, 50, 60 –60, 50, 40, 30, 20, 10 –30, 20, 40, 10, 50, 60 –20, 40, 30, 10, 50, 60 –So number of Binary Search Trees (BST) that can be constructed are: 720.
Binary Search Tree (BST) 10 10, 20, 30, 40, 50, , 50, 40, 30, 20,
Binary Search Tree (BST) 30 30, 20, 40, 10, 50, 6030, 20, 50, 10, 40,
Binary Search Tree (BST) Question: In which tree, searching operation will be faster? Looks more balanced.
Binary Search Tree (BST) When can we say that a tree is balanced or not? Depends on the balance factor of any node. What is this balance factor of a node? Balance Factor (bf) = Height of left sub-tree (h L ) – Height of right sub-tree (h R )
Types of Binary Trees Height Balanced Tree Calculating Balance Factor of every node h L = 2 h R = 2 2 – 2 = 0 0 h L = 1 h R = 0 1 – 0 = = – 0 =
Types of Binary Trees Height Balanced Tree: –A binary search tree is said to be a height balanced tree if, All its nodes have a balance factor of 1, 0 or -1. That is: –| bf | = | h L – h R | <= 1 for every node in the tree.
Types of Binary Trees Which is a Height Balanced Tree? T1 3-2 = 1 1-2= = = = = 0 Yes = T2 No Balance Factor not satisfied for root node (6)
Types of Binary Trees Height Balanced Tree: –Properties: Every Height Balanced Tree is always a Binary Search Tree (BST) whereas, –Every Binary Search Tree (BST) might not be a Height Balanced Tree. Complete Binary Search Tree (BST) is always a Height Balanced Tree. –Note: It is also called AVL tree after the name of the scientists: –Adelson-Velskii, Lendis.
Height Balanced Tree Creation of BST / Insertion into BST Create different BSTs for set of data 10, 20, 30. Arrangement of 3 elements can be done in: 1)20, 10, 30 2)20, 30, 10 3)10, 20, 30 4)30, 20, 10 5)10, 30, 20 6)30, 10, 20 3! = 6 ways
Height Balanced Tree 20, 10, Height Balanced Tree 0 0 0
20, 30, Height Balanced Tree 0 0 0
Left Rotation 10, 20, Not Height Balanced Left Rotation Height Balanced Tree
Right Rotation 30, 20, Right Rotation 2 Not Height Balanced Height Balanced Tree
10, 30, Not Height Balanced Left Rotation Balanced Not a Binary Search Tree Not a Height Balanced Tree
Height Balanced Tree Left-Right Rotation 10, 30, Not Height Balanced -2 Right Rotation Not Height Balanced Binary Search Tree Left Rotation Height Balanced Tree
Height Balanced Tree 30, 10, Right Rotation Balanced Not a Binary Search Tree Not a Height Balanced Tree 2 Not Height Balanced
Height Balanced Tree Right-Left Rotation 30, 10, Left Rotation Right Rotation 2 Not Height Balanced Binary Search Tree Height Balanced Tree
Height Balanced Tree AVL Rotations: –4 types: Left Rotation: –Also known as ‘LL Rotation’. –Involves only a single Left Rotation. Right Rotation: –Also known as ‘RR Rotation’. –Involves only a single Right Rotation. Left Right Rotation: –Also known as ‘LR Rotation’. –Involves 2 rotations: »First a Right, then a Left. Right Left Rotation: –Also known as ‘RL Rotation’. –Involves 2 rotations: »First a Left, then a Right.
Tree General Tree & Not a Binary Tree Mono DickSumoKapilRavi HariLiza Azu JohnMary Prem Tom KaranPeter
Tree Representation of Tree: –A tree can be represented in a computer using: Array Linked List
Tree Array representation of Tree Mono Sumo KapilRaviDickHariLiza--Azu Mono Sumo KapilRaviDickHariLizaAzuMaryJohnPrem Karan … Will not work Works
Tree Array representation of Tree Mono Sumo Kapil Ravi Dick Hari Liza Azu Mary John Prem Karan Peter Tom DATA Array LEFT CHILD Array SIBLING Array INDEX
Tree Linked representation of Tree Fixed Size Node (Version 1) Mono Sumo KapilRaviDick HariLizaAzuMaryJohn PremKaranPeterTom Advantage: Easy Algorithm Disadvantage: Null Links
Tree Linked representation of Tree Variable Size Node (Version 2) Mono4 Sumo2Kapil1Ravi2Dick0 Hari0Liza3Azu0Mary1John0 Prem0Peter0Karan0Tom0 Advantage: Memory Saved Disadvantage: Complex Algorithm
Tree General Tree using a Linked Representation of Binary Tree Mono Sumo Left Child Sibling KapilRaviDick HariLizaAzuMaryJohn Peter Prem Karan Tom
Tree Mono DickSumoKapilRavi HariLiza Azu JohnMary Prem Tom KaranPeter Binary Tree representation of Tree Mono SumoKapilRaviDick HariLizaAzuMaryJohn PremKaranPeterTom
Forest A DCB EF W ZYX P SRQ T1 T2 T3
Forest Forest: –Forest is a collection of disjoint trees. It can also be represented in the form of: –Linked Representation of Binary Tree.
Forest A DCB EF W ZYX P SRQ T1 T2 T3 Conversion of Forest to a Binary Tree A DCB EF T1’ W ZYX P SRQ T2’ T3’
Forest Conversion of Forest to a Binary Tree A DCB EF T1’ W ZYX P SRQ T2’ T3’
Trees FF F F F F F F F F F F F F F F Binary Search Trees / 2-way Search Trees Height Balanced Trees Failure Nodes are not on same level.All Failure nodes are on same level. B Tree of Order 2 B Tree
2040 F X FFFFFFF FF 3-way Search Tree. Not a B Tree. All Failure nodes are not on the same level. Is it a B Tree?
B Tree 30X F 20X X FFFFFFFFF 3-way Search Tree. YesIs it a B Tree? 25X35X4550 All Failure nodes are on the same level. B Tree of Order 3
B Tree General Node Structure of B Tree of Order m. P1P1 K1K1 P2P2 K2K2 P3P3 K3K3 P4P4 ….K m-1 PmPm Where: Ki: Key values in the node. Pi: Pointers to sub-trees.
B Tree B Tree: –A ‘B Tree’ of order m is an, m-way search tree where, All failure nodes are at the same level where, –A failure node is, »A node which can be reached during a search only if, »The value being searched for is not in the tree. –Applications: Used in index creation in database. –Note: Every B Tree will always be a Search Tree however, –Every Search Tree might not be a B Tree.
B Tree Construct a B Tree of order 3 for following set of data: 10, 20, 30, 40, 50, 60, 70 10X 20 X 30 10X30X 20X 40 10X3040 X 70 20X60X 10X30X50X70X