• Tree Data Structures Objectives At the end of this class, you should: Know what a tree data structure is Be able to explain fundamental tree terminologies Know how to traverse a tree pre-order In-order post-order Objectives At the end of this class, you should: Know what a binary tree is Full/Strict Complete Perfect Objectives At the end of this class, you should: Understand Binary Search Trees How to retrieve from them How to insert into them How to delete from them Intro A B D C E H G I J Definition 0 1 2 4 7 5 8 Trees are non-linear Trees represent relationships 6 9 Each node may have zero or more successors Each node has exactly one predecessor except the starting node. Trees are hierarchical 3 A tree is a collection of nodes. Tree Terminologies 0 Root: - The starting node of a tree, i.e. the node without any predecessor Tree Terminologies 0 Parent: - 1 2 Child: - 3 4 5 6 8 9 Successor of a node Siblings: - 7 Predecessor of a node Nodes with the same parent Tree Terminologies 3 5 7 8 9 Leaf: - A node without any child - Shows the termination of a tree branch Tree Terminologies Branch: - The link from one node to another - Also called an Edge Tree Terminologies Subtree: - The subtree of a node is a tree whose root node is a child of that node - Left subtree of the root node Tree Terminologies Subtree: - The subtree of a node is a tree whose root node is a child of that node - Right subtree of the root node Tree Terminologies Depth: - Measure of the distance of a node from the root. - Depth 0 Tree Terminologies Depth: - Measure of the distance of a node from the root. - Depth 1 Tree Terminologies Depth: - Measure of the distance of a node from the root. - Depth 2 Tree Terminologies Depth: - Measure of the distance of a node from the root. - Depth 3 Tree Terminologies Depth: - Measure of the distance of a node from the root. - Also called the path-length - Depth 3 Tree Terminologies Depth: - Measure of the distance of a node from the root. - Also called the path-length - Depth 3 Nodes at the same depth form a level Tree Terminologies Height: - The maximum distance of any leaf node from the root node - Height = 3 Tree Examples 1 1 2 2 1 3 3 1 4 5 2 5 3 4 2 Binary Tree A tree in which no node can have more than two children Each node either has at most two non-empty subtrees or is a leaf node. Full Binary Tree A binary three in which each node has two or zero child nodes. There is no node with only one child Complete Binary Tree A binary tree in which every level, except possibly the deepest, is completely filled. At the height of the tree (the deepest level), all nodes (leaves) are as far left as possible 1 2 Where would the next node go? 3 4 5 6 Complete Binary Tree A binary tree in which every level, except possibly the deepest, is completely filled. At the height of the tree (the deepest level), all nodes (leaves) are as far left as possible 1 Where would the next node go? Perfect Binary Tree A binary tree with all leaf nodes at the same depth All internal nodes have exactly two children Has the max no of nodes for a given height. (2n+1 – 1) nodes n = height of the tree Binary Tree Traversal Traversal: Visiting the nodes of a tree Four traditional types grouped in two Deapth First Pre-order In-order Post-order Breadth First Level-order Pre-Order Traversal Visit a node (N) Pre-Order Traversal Visit a node (N) Visit its left subtree (L) Pre-Order Traversal Visit a node (N) Visit its left subtree (L) Visit its right subtree (R) Pre-Order Traversal Visit a node (N) Visit its left subtree (L) Visit its right subtree (R) Preoder ==> NLR Node – Left – Right Pre-Order Traversal Example 1. A B C Solution Condition: NLR Answer: D E H F I G J Pre-Order Traversal Example 1. A B C Solution Condition: NLR Answer: A, D E H F I G J Pre-Order Traversal Example 1. A B C Solution Condition: NLR Answer: A, B, D E H F I G J Pre-Order Traversal Example 1. A B C Solution Condition: NLR Answer: A, B, D, D E H F I G J Pre-Order Traversal Example 1. A B C Solution Condition: NLR Answer: A, B, D, E, D E H F I G J Pre-Order Traversal Example 1. A B C Solution Condition: NLR Answer: A, B, D, E, H, D E H F I G J Pre-Order Traversal Example 1. A B C Solution Condition: NLR Answer: A, B, D, E, H, I, D E H F I G J Pre-Order Traversal Example 1. A B C Solution Condition: NLR Answer: A, B, D, E, H, I, C, D E H F I G J Pre-Order Traversal Example 1. A B C Solution Condition: NLR Answer: A, B, D, E, H, I, C, F, D E H F I G J Pre-Order Traversal Example 1. A B C Solution Condition: NLR Answer: A, B, D, E, H, I, C, F, G, D E H F I G J Pre-Order Traversal Example 1. A B C Solution Condition: NLR Answer: A, B, D, E, H, I, C, F, G, J D E H F I G J Pre-Order Traversal Example 2. 3 7 9 Solution Condition: NLR Answer: 8 4 2 5 Pre-Order Traversal Example 2. 3 7 9 Solution Condition: NLR Answer: 3, 8 4 2 5 Pre-Order Traversal Example 2. 3 7 9 Solution Condition: NLR Answer: 3, 7, 8 4 2 5 Pre-Order Traversal Example 2. 3 7 9 Solution Condition: NLR Answer: 3, 7, 8, 8 4 2 5 Pre-Order Traversal Example 2. 3 7 9 Solution Condition: NLR Answer: 3, 7, 8, 4, 8 4 2 5 Pre-Order Traversal Example 2. 3 7 9 Solution Condition: NLR Answer: 3, 7, 8, 4, 9, 8 4 2 5 Pre-Order Traversal Example 2. 3 7 9 Solution Condition: NLR Answer: 3, 7, 8, 4, 9, 2, 8 4 2 5 Pre-Order Traversal Example 2. 3 7 9 Solution Condition: NLR Answer: 3, 7, 8, 4, 9, 2, 5 8 4 2 5 In-order Traversal Visit the left subtree (L) of a node In-order Traversal Visit the left subtree (L) of a node Visit the node (N) In-order Traversal Visit the left subtree (L) of a node Visit the node (N) Visit its right subtree (R) In-order Traversal Visit the left subtree (L) of a node Visit the node (N) Visit its right subtree (R) Inoder ==> LNR Left – Node – Right In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D E F G H I J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, I, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, I, A, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, I, A, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, I, A, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, I, A, F, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, I, A, F, C, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, I, A, F, C, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, I, A, F, C, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, I, A, F, C, J, D E H F I G J In-order Traversal Example 1. A B C Solution Condition: LNR Answer: D, B, H, E, I, A, F, C, J, G D E H F I G J In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8, 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8, 7, 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8, 7, 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8, 7, 4, 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8, 7, 4, 3, 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8, 7, 4, 3, 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8, 7, 4, 3, 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8, 7, 4, 3, 2, 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Order: L-M-R Answer: 8, 7, 4, 3, 2, 9, 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Order: L-M-R Answer: 8, 7, 4, 3, 2, 9, 8 4 2 5 In-order Traversal Example 2. 3 7 9 Solution Condition: LNR Answer: 8, 7, 4, 3, 2, 9, 5 8 4 2 5 Post-order Traversal Visit the right subtree (L) of a node Post-order Traversal Visit the left subtree (L) of a node Visit its right subtree (R) Post-order Traversal Visit the left subtree (L) of a node Visit its right subtree (R) Visit the node (M) Post-order Traversal Visit the left subtree (L) of a node Visit its right subtree (R) Visit the node (M) Inoder ==> LRN Left – Right – Node Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, I, D E H F I G J Post-order Traversal Example 1. A B C Solution Condtion: LRN Answer: D, H, I, E, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, I, E, B, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, I, E, B, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, I, E, B, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, I, E, B, F, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, I, E, B, F, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, I, E, B, F, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, I, E, B, F, J, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, I, E, B, F, J, G, D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, I, E, B, F, J, G, C D E H F I G J Post-order Traversal Example 1. A B C Solution Condition: LRN Answer: D, H, I, E, B, F, J, G, C, A D E H F I G J Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8, 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8, 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8, 4, 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8, 4, 7, 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8, 4, 7, 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8, 4, 7, 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8, 4, 7, 2, 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8, 4, 7, 2, 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8, 4, 7, 2, 5, 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8, 4, 7, 2, 5, 9, 8 4 2 5 Post-order Traversal Example 2. 3 7 9 Solution Condition: LRN Answer: 8, 4, 7, 2, 5, 9, 3 8 4 2 5 Binary Search Tree (BST) 10 3 2 16 7 6 12 9 22 17 Each node is either empty or has up to two subtrees Binary Search Tree (BST) 10 3 2 16 7 6 12 9 22 17 Each node is either empty or has up to two subtrees Value in each node > value in all of its left subtree nodes Binary Search Tree (BST) 10 3 2 16 7 6 12 9 22 17 Each node is either empty or has up to two subtrees Value in each node > value in all of its left subtree nodes Value in each node < value in all of its right subtree nodes Binary Search Tree (BST) 10 3 2 16 7 12 22 6 9 17 Each node is either empty or has up to two subtrees Value in each node > value in all of its left subtree nodes Value in each node < value in all of its right subtree nodes Left-most node == smallest value Binary Search Tree (BST) 10 3 16 2 7 6 12 9 22 17 Each node is either empty or has up to two subtrees Value in each node > value in all of its left subtree nodes Value in each node < value in all of its right subtree nodes Left-most node == smallest value Right-most node == largest value BST Insertion Start from the root node Move to the left child if the value is less, otherwise go to the right child. Repeat the comparision until a leaf node is reached Insert node as a left child if value is less than that of the current leaf, otherwise insert as a right child. BST Insertion Example 1 Before <10, move left 8 10 10 3 2 7 6 8 16 12 9 22 17 3 2 16 7 6 12 9 22 17 BST Insertion Example 1 Before >3, move right 8 10 10 3 3 16 16 8 2 7 6 12 9 22 17 2 7 6 12 9 22 17 BST Insertion Example 1 Before >7, move right 8 10 10 3 2 3 16 7 12 22 2 16 7 12 22 8 6 9 17 6 9 17 BST Insertion Example 1 Before <9, move left 8 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 8 22 17 BST Insertion Example 1 Before 9 is leaf, insert 8 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 8 22 17 BST Insertion Example 2 Before After 23 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 23 BST Deletion Traverse the tree, find the node and delete it. BST properties must be preserved!! Three cases: Deleting leaf node Deleting a node with one child Deleting a node with two children BST Deletion Deleting a leaf node Traverse to the node’s parent Remove link to the node BST Deletion Example 1 Before After 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 22 17 BST Deletion Example 2 Before After 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 22 9 17 BST Deletion Node with one child Traverse to the node Link child of the node to the parent of the node Remove the link between the node and its parent Remove the link between the node and its child BST Deletion Example 1 Before Link parent to child 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 1 Before Unlik from parent 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 1 Before Unlink from child 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 1 Before Complete 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 17 BST Deletion Node with two children Find the node to be deleted Let’s call it node x Find the rightmost node on its left subtree Let’s call this node z The leftmost node on the right subtree can be used instead Replace the node x data with node z data Delete node z BST Deletion Example 1: using the rightmost node in the left subtree Before Locate node z 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 1: using the rightmost node in the left subtree Before Locate node z 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 1: using the rightmost node in the left subtree Before Locate node z 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 1: using the rightmost node in the left subtree Before Locate node z 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 1: using the rightmost node in the left subtree Before Substitute node z data 9 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 1: using the rightmost node in the left subtree Before Delete node z 9 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 1: Using the rightmost node in the left subtree Before Complete 9 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 22 17 BST Deletion Example 2: using the leftmost node in the right subtree Before Locate node z 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 2: using the leftmost node in the right subtree Before Locate node z 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 2: using the leftmost node in the right subtree Before Locate node z 10 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 2: using the leftmost node in the right subtree Before Substitute node z data 12 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 2: using the leftmost node in the right subtree Before Delete node z 12 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 12 9 22 17 BST Deletion Example 2: using the leftmost node in the right subtree Before Complete 12 10 3 2 7 6 3 16 12 9 22 17 2 16 7 6 22 9 17 Closing Trees are heirachichal data structures are collection of nodes. show the relationship between their nodes are awesome