Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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.

2 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.

3 Expression Tree Draw the expression tree for expression ‘(A + B)’. Infix Expression:( A + B ) Step-1 Postfix Expression: Postfix A B + 1) A A 2000 2) B A 2000 4000 B 3) + A 2000 6000 B 4000 (Operator)(Operand) + 6000 56 11 Evaluate it for A = 5, B = 6.

4 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? 5 6 11 1) A2) B3) +

5 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 * 1 2 4 2 3 2 6 = AB+ CD -*

6 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 = 2 2 11 2 4 2 2

7 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

8 Types of Binary Trees A B C D EF G Main Disadvantage? 7 nodes 8 null links Linked Representation of a Binary Tree

9 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

10 Threaded Binary Tree A B C D EF G NULL Inorder traversal: B A G E D F C Inorder Threading

11 Threaded Binary Tree A B C D EF G Preorder traversal: A B C D E G F Preorder Threading NULL

12 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.

13 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.

14 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

15 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.

16 Types of Binary Trees 30 5040 2010 50 3040 2010 50 3040 2010 Tree T1 Tree T2 Tree T3 Heap Tree

17 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.

18 Heap Tree 50 2040 3010 50 3040 20 10 50 40 2010 Tree T1 Tree T2 Tree T3 No Yes Condition not satisfied for node 20. Not a complete binary tree. Which is a heap tree?

19 Heap Tree 50 3040 2010 Tree T1 Yes 10 3020 5040 Tree T2 Yes Max HeapMin Heap Max Value Min Value Which is a heap tree?

20 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.

21 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.

22 Heap Tree 50 3040 2010 3020 5040 Max HeapMin Heap 1234567 Index Value Array representation of Heap Tree 5030402010-- 1234567 30205040--

23 Heap Tree 95 8545 7525 Max Heap Max Value 3515 5565 Insert value 19 19 Insertion into a Heap Tree

24 Heap Tree 95 8545 7525 Max Heap Max Value 3515 5565 Insert value 111 19111 25 85 111 95 Insertion into a Heap Tree

25 Heap Tree 95 8545 7525 Max Heap Max Value 3515 5565 Insert value 111 19111 Insertion into a Heap Tree

26 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

27 Types of Binary Trees 99 4563 3529 Max Heap Max Value 5742 27122426 Delete a value Any node can be deleted, but deleting root node has some special importance. Deletion from a Heap Tree

28 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

29 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.

30 1. Create Heap Array A 12345 Array B 12345 33 14650276 12345 33 14 12345 33 14 65 12345 14 33 02 12345 65 14 33 02 76 Output 12345 7665330214 Array A

31 2. Sort the Heap 76 6533 0214 Heap 12345 7665330214 Array A 12345 Array B Array B would not be required Directly swap the root with the last element in the heap tree.

32 2. Sort the Heap 76 6533 0214 Heap 12345 7665330214 Array A 76 6533 0214 12345 7665330214 Iteration-1 Nij 65 1433 02 12345 6514330276 Iteration-2 ij

33 2. Sort the Heap 76 6533 0214 Heap 12345 7665330214 Array A 33 1402 12345 3314026576 Iteration-3 Nij 14 02 12345 1402336576 Iteration-4 ij OUTPUT

34 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.

35 Heap Sort Algorithm: –HeapSort Input: –Array A which is to be sorted. Output: –Array A with elements sorted in ascending order. Data Structure: –Array.

36 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

37 Types of Binary Trees 50 3040 2010 50 3060 1040 80 50 3060 1055 80 Binary Search Tree (BST) Heap Tree Tree T1Tree T2Tree T3

38 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.

39 Binary Search Tree (BST) 10 2030 5040 50 3060 1080 50 30 35 55 Which is a BST? No Heap Tree (Min Heap) No Not satisfied for node with value 30. Tree T1Tree T2Tree T3 Yes

40 Binary Search Tree (BST) Which is a BST? Yes. Lexicographical ordering. Jan DecMar AugFeb JulNov Jun (Not numerical ordering)

41 Binary Search Tree (BST) Applications of BST 65 1974 15882869 80 25 57 54 1. Smallest / Minimum value?15 2. Largest / Maximum value?88 3. Search Value 54?Found. 4. Search Value 90?Not Found. 5. InOrder Traversal: 15 19 25 28 54 57 65 69 74 80 88 Data sorted in Ascending Order. Hence also known as: Binary Sorted Tree.

42 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, 10 20 1030 20 1030 10 20 30 10 30 20 30 10 20 30 20 10 For a different sequence of the same set of data, there will be a different BST. Conclusion:

43 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.

44 Binary Search Tree (BST) Create a BST for data '57, 28, 25, 69, 88, 81, 74, 65, 19, 15'. 57 28 57 25 28 25 57 69 28 25 69

45 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.

46 Binary Search Tree (BST) 10 10, 20, 30, 40, 50, 60 20 30 40 50 60, 50, 40, 30, 20, 10 50 40 30 20 10 60

47 Binary Search Tree (BST) 30 30, 20, 40, 10, 50, 6030, 20, 50, 10, 40, 60 2040 10 50 30 2050 10 60 40

48 Binary Search Tree (BST) 10 20 30 40 50 40 30 20 10 60 30 2040 10 50 30 2050 10 60 40 1 2 3 4 Question: In which tree, searching operation will be faster? Looks more balanced.

49 Binary Search Tree (BST) 30 20 50 1060 40 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 )

50 Types of Binary Trees Height Balanced Tree Calculating Balance Factor of every node. 30 20 50 1060 40 h L = 2 h R = 2 2 – 2 = 0 0 h L = 1 h R = 0 1 – 0 = 1 1 1 -1 = 0 0 0 – 0 = 0 0 0 0

51 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.

52 Types of Binary Trees Which is a Height Balanced Tree? 6 2 8 1 3 74 T1 3-2 = 1 1-2= -1 0-0 = 0 1-0 = 0 1-0 = 1 0-0 = 0 Yes 6 2 8 1 35 4 3-1 = 2 0 0 00 0 T2 No Balance Factor not satisfied for root node (6)

53 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.

54 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

55 Height Balanced Tree 20, 10, 30 20 1030 Height Balanced Tree 0 0 0

56 20, 30, 10 20 1030 Height Balanced Tree 0 0 0

57 Left Rotation 10, 20, 30 10 20 30 Not Height Balanced -2 20 1030 Left Rotation Height Balanced Tree

58 Right Rotation 30, 20, 10 30 20 10 20 1030 Right Rotation 2 Not Height Balanced Height Balanced Tree

59 10, 30, 20 10 30 20 Not Height Balanced -2 30 1020 Left Rotation Balanced Not a Binary Search Tree Not a Height Balanced Tree

60 Height Balanced Tree Left-Right Rotation 10, 30, 20 10 30 20 Not Height Balanced -2 Right Rotation Not Height Balanced Binary Search Tree 10 20 30 -2 Left Rotation 20 1030 Height Balanced Tree

61 Height Balanced Tree 30, 10, 20 30 10 20 10 2030 Right Rotation Balanced Not a Binary Search Tree Not a Height Balanced Tree 2 Not Height Balanced

62 Height Balanced Tree Right-Left Rotation 30, 10, 20 30 10 20 2 Left Rotation 30 20 10 20 1030 Right Rotation 2 Not Height Balanced Binary Search Tree Height Balanced Tree

63 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.

64 Tree General Tree & Not a Binary Tree Mono DickSumoKapilRavi HariLiza Azu JohnMary Prem Tom KaranPeter

65 Tree Representation of Tree: –A tree can be represented in a computer using: Array Linked List

66 Tree Array representation of Tree Mono Sumo KapilRaviDickHariLiza--Azu--- 1 2 345678910111213 Mono Sumo KapilRaviDickHariLizaAzuMaryJohnPrem Karan ….. 1 2 345678910111213 Will not work Works

67 Tree Array representation of Tree Mono Sumo Kapil Ravi Dick Hari Liza Azu Mary John Prem Karan Peter Tom DATA Array 2 6 8 9 0 0 11 0 14 0 0 0 0 0 LEFT CHILD Array 0 3 4 5 0 7 0 0 10 0 12 13 0 0 SIBLING Array 1 2 3 4 5 6 7 8 9 10 11 12 13 14 INDEX

68 Tree Linked representation of Tree Fixed Size Node (Version 1) Mono Sumo KapilRaviDick HariLizaAzuMaryJohn PremKaranPeterTom Advantage: Easy Algorithm Disadvantage: Null Links

69 Tree Linked representation of Tree Variable Size Node (Version 2) Mono4 Sumo2Kapil1Ravi2Dick0 Hari0Liza3Azu0Mary1John0 Prem0Peter0Karan0Tom0 Advantage: Memory Saved Disadvantage: Complex Algorithm

70 Tree General Tree using a Linked Representation of Binary Tree Mono Sumo Left Child Sibling KapilRaviDick HariLizaAzuMaryJohn Peter Prem Karan Tom

71 Tree Mono DickSumoKapilRavi HariLiza Azu JohnMary Prem Tom KaranPeter Binary Tree representation of Tree Mono SumoKapilRaviDick HariLizaAzuMaryJohn PremKaranPeterTom

72 Forest A DCB EF W ZYX P SRQ T1 T2 T3

73 Forest Forest: –Forest is a collection of disjoint trees. It can also be represented in the form of: –Linked Representation of Binary Tree.

74 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’

75 Forest Conversion of Forest to a Binary Tree A DCB EF T1’ W ZYX P SRQ T2’ T3’

76 Trees 30 15 40 10 45 25 10 40 05 451530 25 05 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

77 2040 F 1015 2530 4550 35X FFFFFFF FF 3-way Search Tree. Not a B Tree. All Failure nodes are not on the same level. Is it a B Tree?

78 B Tree 30X F 20X 1015 40X FFFFFFFFF 3-way Search Tree. YesIs it a B Tree? 25X35X4550 All Failure nodes are on the same level. B Tree of Order 3

79 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.

80 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.

81 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


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

Similar presentations


Ads by Google