200 likes | 288 Views
Cs212: Data Structures. Trees Exercise. Trees, Binary Trees, and Binary Search Trees. Answer the following questions for the tree shown below. What is the path length of the path from node 20 to node 12?. Trees, Binary Trees, and Binary Search Trees.
E N D
Cs212: Data Structures Computer Science Department Trees Exercise
Trees, Binary Trees, and Binary Search Trees • Answer the following questions for the tree shown below. • What is the path length of the path from node 20 to node 12?
Trees, Binary Trees, and Binary Search Trees What is the path length of the path from node 20 to node 12? 3
Trees, Binary Trees, and Binary Search Trees • What is the height of the tree?
Trees, Binary Trees, and Binary Search Trees • What is the height of the tree? 4
Trees, Binary Trees, and Binary Search Trees • Show what would this tree look like after: • Adding 18
Trees, Binary Trees, and Binary Search Trees • Show what would this tree look like after: • Adding 18 18<20 18>16 18
Trees, Binary Trees, and Binary Search Trees • Show what would this tree look like after: • Delete 35 18
Trees, Binary Trees, and Binary Search Trees • Show what would this tree look like after: • Delete 35 18
Trees, Binary Trees, and Binary Search Trees • Show what would this tree look like after: • Delete 16 18
Trees, Binary Trees, and Binary Search Trees • Show what would this tree look like after: • Delete 16 , After delete node 16 we replace it by predecessor(largest node of left sub tree) . 12 18
Trees, Binary Trees, and Binary Search Trees • Show what would this tree look like after: • Delete 16 , After delete node 16 we replace it by successor(smallest node of right sub tree) . 18
Trees, Binary Trees, and Binary Search Trees • Show what would this tree look like after: • Delete 20 18
Trees, Binary Trees, and Binary Search Trees • Show what would this tree look like after: • Delete 20, After delete node 20 we replace it by predecessor(largest node of left sub tree) . 18
Trees, Binary Trees, and Binary Search Trees • Show what would this tree look like after: • Delete 20,After delete node 20 we replace it by successor(smallest node of right sub tree) .
Trees, Binary Trees, and Binary Search Trees • EX2:Traverse the tree shown below in: • Preorder travers VLR: • In order travers LVR: • Post order travers LRV:
Trees, Binary Trees, and Binary Search Trees • EX2:Traverse the tree shown below in: • Preorder travers VLR: • 20,16,11,10,12,43,35,48,45,65 • In order travers LVR: • 10,11,12,16,20,35,43,45,48,65. • Post order travers LRV: • 10,12,11,16,35,45,65,48,43,20
AVL Tree • EX3: test if tree is AVL tree or not, if not convert it to AVL tree. 20 10 30 5 1
AVL Tree • EX3: test if tree is AVL tree or not, if not convert it to AVL tree. Not AVL tree because node 10 out of balance. 20 20 Out of balance LH LH 10 30 5 30 LH EH EH EH 5 1 10 LH EH EH 1 Left to left imbalance case AVL tree EH