Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Similar presentations


Presentation on theme: "Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:"— Presentation transcript:

1 Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public: //The data section BTNode data T data; left right //The links BTNode * left; BTNode * right; }; 6 Copyright © William C. Cheng

2 7 Copyright © William C. Cheng G D H E I B Data Structures - CSCI 102 Binary Tree A C F

3 Trees grown downward in computer science 8 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Tree Terminology Each BTNode has 0, 1 or 2 Child Nodes that are below it in the tree A BTNode that has a child is called that child’s Parent Node Parent nodes point to child nodes via a branch (child nodes don’t need to point to parents, but they can if they want)

4 9 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Trees A is the Parent of B and C B is the Left Child of A C is the Right Child of A A Branch B Branch C

5 The Root Node is the only BTNode in the tree that has no parent node 10 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Tree Terminology A BTNode is a Leaf Node if it has no children The Level of a BTNode in the tree is the number of branches from the root to the node The Height of the tree is the number of nodes on the longest path from root to leaf

6 11 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Trees B A C DE A is the Root Node C, D, E are Leaf Nodes Level(A) = 0 Level(B) = 1 Level(D) = 2 The Height of the tree is 3

7 What kind of operations do we need to perform on a binary tree? 12 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Trees height() size() search() insert() delete() print() NOTE: Pretty much every operation requires a way to traverse the tree

8 14 Copyright © William C. Cheng E HI D G BC F Data Structures - CSCI 102 Binary Tree Height If you had to calculate the height of a binary tree, how would you do it? Root A

9 15 Copyright © William C. Cheng E HI D G BC F Data Structures - CSCI 102 Binary Tree Height Height = 1 + max(Height(Root->left),Height(Root->right)) Root A Height of Root->left Root->right 1 Height of 1

10 18 Copyright © William C. Cheng E HI D G BC F Data Structures - CSCI 102 Binary Tree Traversal If you had to print out all the nodes in a binary tree, how would you do it? Root A

11 If you had to print out all the nodes in a binary tree, how would you do it? 19 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Tree Traversal At each node in the tree, you have three choices: These choices leads to different kinds of traversals Print the current node Go to the left child Go to the right child In Order: print left, print current, print right Pre Order: print current, print left, print right Post Order: print left, print right, print current

12 20 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Tree Traversal In Order Traversal: Traverse A’s left subtree Print A Traverse A’s right subtree Order of output: GDBHEIACF E I D G BHBH A C F Root

13 Pre Order Traversal: Print A Traverse A’s left subtree Traverse A’s right subtree Order of output: ABDGEHICF E I D G BHBH A C F Root 22 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Tree Traversal

14 Post Order Traversal: Traverse A’s left subtree Traverse A’s right subtree Print A Order of output: GDHIEBFCA E I D G BHBH A C F Root 24 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Tree Traversal

15 26 Copyright © William C. Cheng E HI D G BC F Data Structures - CSCI 102 Binary Tree Search If you had to find a particular value in a binary tree, how would you do it? Root A

16 Search still requires us to potentially look at every single node in the tree Still O(n) So far binary trees are interesting, but they don’t provide any extra benefit 27 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Tree Issues If you’re going to insert a new Node, where should you put it in the tree? What’s missing here is some extra organization that makes it easier to find things

17 template class BTNode { public: T key;//The key data U data; //The data left right //The links BTNode * left; BTNode * right; }; 8 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Tree BTNode can also have a separate key and data key is used to order the nodes in the tree data actually holds the stuff we care about BTNode key

18 To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) 9 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Trees Binary Search Trees have one major organizational property (the binary search tree property): Let X be a Node in a binary search tree Every Node X is comparable to every other Node via a field we call the key (for now, "data" is the key) If Y is a Node in the left subtree of X: key[Y] <= key[X] key[Y] > key[X] If keys must be unique, then we have key[Y] < key[X] You can implement your BST to enforce this We will assume that this is the case here If Y is a Node in the right subtree of X:

19 11 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Search 58 7746 30 If you had to find a particular value in a binary search tree, how would you do it? Ex: find 58 50 60 70 80 Root

20 If you had to find a particular value in a binary search tree, how would you do it? 12 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Search 5830 50 46 60 70 77 80 Root Ex: find 58 Start at root 58 == 60? No 58 < 60? Yes, go left Current

21 13 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Search 58 7746 30 50 60 70 80 Root If you had to find a particular value in a binary search tree, how would you do it? Ex: find 58 Start at root 58 == 60? No 58 < 60? Yes, go left Current 58 == 50? No 58 < 50? No 58 > 50? Yes, go right

22 14 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Search 58 7746 30 50 60 70 80 Root If you had to find a particular value in a binary search tree, how would you do it? Ex: find 58 Start at root 58 == 60? No 58 < 60? Yes, go left 58 == 50? No 58 < 50? No Current 58 > 50? Yes, go right 58 == 58? Yes

23 Start at the root What will our search algorithm be? If the key we want is less than the key of the current node, search the left subtree 15 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Search Is the current node what we want? Otherwise, search the right subtree If yes, we’re done If no... Should our solution be recursive or iterative?

24 18 Copyright © William C. Cheng 58 7746 30 5070 80 Data Structures - CSCI 102 Binary Search Tree Min/Max How would you find the minimum/maximum values in a binary search tree? Root 60

25 Start at the root What will our findMin algorithm be? 19 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Min/Max Does the current Node have a left child? If yes, go to the left child If no, we’re at the minimum Start at the root What will our findMax algorithm be? Does the current Node have a right child? If yes, go to the right child If no, we’re at the maximum

26 Order of output: 30 46 50 58 60 70 77 80 discussed? In Order Traversal Visits all the nodes in the tree in order 21 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Traversal Now that we have an implicit ordering to our tree, what happens with the traversal algorithms we previously 5830 50 46 60 70 77 80 Root

27 22 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Traversal Traverse the binary search tree with an in-order traversal Tree Sort Given an unordered list of elements, add them all to a binary search tree All the nodes come back out sorted!

28 Binary Search Tree Insertion 24 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Insertion New nodes are always added in the place of an existing NULL...they never reposition other nodes 1) Create the new node with its data and set both of its pointers to NULL 2) If the tree is empty, make the new node the root 3) Walk down the tree node by node like you were doing a search If new value is less than current node, move to left subtree, otherwise move to right subtree Potentially reject duplicates

29 25 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? 58 7746 30 50 60 70 80 Root Ex: insert 59

30 Make new node 26 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? 58 7746 30 50 60 70 80 Root 59 New Node

31 27 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? 58 7746 30 50 60 70 80 Root 59 New Node Current Ex: insert 59 Make new node Start at root 59 < 60; go left

32 59 > 50; go right 28 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? 58 7746 30 50 60 70 80 Root 59 New Node Current Ex: insert 59 Make new node Start at root 59 < 60; go left

33 29 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? 7746 30 50 60 70 80 Root 59 New Node Current 58 Ex: insert 59 Make new node Start at root 59 < 60; go left 59 > 50; go right 59 > 58; go right

34 30 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? 58 7746 30 50 60 70 80 Root 59 Ex: insert 59 Make new node Start at root 59 < 60; go left 59 > 50; go right 59 > 58; go right Right is NULL Insert new Node

35 31 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? 58 7746 30 50 60 70 80 Root 59 Ex: insert 59 Make new node Start at root 59 < 60; go left 59 > 50; go right 59 > 58; go right Right is NULL Insert new Node

36 Binary Search Tree Removal 32 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Remove Search the tree for the node that you want to remove When node is found, removal breaks down into 3 distinct cases 1) Remove a node with no children 2) Remove a node with one child 3) Remove a node with two children

37 Find the node and its parent Remove a node with no children (case #1) 33 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Remove Set the parent pointer to the node to NULL Delete the node

38 Case #1 Ex: remove 46 34 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? 58 7746 30 50 60 70 80 Root

39 35 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? 58 7746 30 50 60 70 80 Root To remove Case #1 Ex: remove 46 Search for the value

40 36 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? 58 77 46 30 50 60 70 80 Root To remove Case #1 Ex: remove 46 Search for the value Set parent’s pointer to NULL

41 37 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? 58 77 46 30 50 60 70 80 Root To remove Case #1 Ex: remove 46 Search for the value Set parent’s pointer to NULL Delete the child

42 Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? 58 77 38 Copyright © William C. Cheng 30 50 60 70 80 Root Case #1 Ex: remove 46 Search for the value Set parent’s pointer to NULL Delete the child

43 Find the node and its parent Remove a node with one child (case #2) Set the parent pointer to the node’s only child Delete the node 39 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Remove

44 58 7746 30 50 60 70 80 Root 40 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30

45 58 7746 30 50 60 70 80 Root 41 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? To remove Case #2 Ex: remove 30 Search for the value

46 58 7746 30 50 60 70 80 Root 42 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? To remove Case #2 Ex: remove 30 Search for the value Pointer value’s parent to value’s only child

47 58 7746 30 50 60 70 80 Root 43 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? To remove Case #2 Ex: remove 30 Search for the value Pointer value’s parent to value’s only child Delete the node

48 58 50 46 60 70 77 80 Root 44 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30 Search for the value Pointer value’s parent to value’s only child Delete the node

49 Find the node Remove a node with two children (case #3) 45 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Remove Remove node’s successor or predecessor Replace node’s data with successor/predecessor data The node in the tree with the biggest value less than x Predecessor(x) The node in the tree with the smallest value greater than x Successor(x)

50 58 7746 30 50 60 70 80 Root Case #3 Ex: remove 50 46 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree?

51 58 7746 30 50 60 70 80 Root Case #3 Ex: remove 50 47 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Search for the value To remove

52 5830 50 46 60 70 77 80 Root Case #3 Ex: remove 50 48 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Search for the value Find predecessor Rightmost value in To remove left subtree of node Predecessor

53 58 77 30 60 70 80 Root Case #3 Ex: remove 50 49 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Predecessor 46 To remove Search for the value Find predecessor Rightmost value in left subtree of node Swap values of node and its predecessor Swap 50

54 58 7750 30 46 60 70 80 Root 50 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Case #3 Ex: remove 50 Search for the value Find predecessor Rightmost value in To remove left subtree of node Swap values of node and its predecessor Delete the node via case #1 or case #2

55 5830 46 60 70 80 Root Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Case #3 Ex: remove 50 Search for the value Find predecessor Rightmost value in left subtree of node Swap values of node and its predecessor Delete the node via case #1 or case #2 77 51 Copyright © William C. Cheng

56 60, 46, 77, 45, 50, 63, 91 Insert the following values in an empty tree (in this order) 52 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Construction What does the resulting tree look like?

57 53 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (in this order) 60, 46, 77, 45, 50, 63, 91 506345 What does the resulting tree look like? Tree is perfectly balanced! Height = log(n+1) 46 60 91 Root 77

58 91, 77, 63, 60, 50, 46, 45 Insert the following values in an empty tree (same values as last time, but different order) 54 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Construction What does the resulting tree look like?

59 91 Root Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? Insert 91 55 Copyright © William C. Cheng

60 91 Root 77 Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? Insert 91 Insert 77 56 Copyright © William C. Cheng

61 91 Root 77 Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? Insert 91 63 Insert 77 Insert 63 57 Copyright © William C. Cheng

62 58 Copyright © William C. Cheng 91 Root 77 Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? Insert 91 63 Insert 77 Insert 63 60 Insert 60

63 91 Root 77 60 50 46 45 59 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? Tree is completely unbalanced 63 Height = n


Download ppt "Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:"

Similar presentations


Ads by Google