CS102 Binary Search Trees Prof Tejada 1
To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) 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] 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: key[y] > key[x] 9
Binary Search Trees If Y is a Node in the left subtree of X: key[y] < key[x] If Y is a Node in the right subtree of X: key[y] > key[x] 50 60 70 Root 30 58 80 46 77 10
Binary Search Tree Search If you had to find a particular value in a binary search tree, how would you do it? Ex: find 58 60 Root 50 70 30 58 80 46 77 11
Binary Search Tree Search 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 50 60 70 Root 30 58 80 46 77 12
Binary Search Tree Search If you had to find a particular value in a binary search tree, how would you do it? Ex: find 58 Start at root 60 Root 58 == 60? No 58 < 60? Yes, go left Current 50 70 58 == 50? No 58 < 50? No 58 > 50? Yes, go right 30 58 80 46 77 13
Binary Search Tree Search If you had to find a particular value in a binary search tree, how would you do it? Root Ex: find 58 Start at root 60 58 == 60? No 58 < 60? Yes, go left 58 == 50? No 58 < 50? No 58 > 50? Yes, go right 58 == 58? Yes 30 46 50 Current 58 70 77 80 14
Binary Search Tree Search What will our search algorithm be? Start at the root Is the current node what we want? If yes, we re done If no... If the key we want is less than the key of the current node, search the left subtree Otherwise, search the right subtree Should our solution be recursive or iterative? 15
Binary Search Tree Search Iterative search bool BinaryTree<T>::search(const T& key) { //start at the root BTNode<T>* current = this->root; while(current!= NULL) { if (current->key == key) { return true; //found the value } else if (key < current->key) { //search the left subtree current = current->left; } else { //search the right subtree current = current->right; } } return false; //could not find the value } 16
} Binary Search Tree Search Recursive search //public helper function bool BinaryTree<T>::search(const T& key) { return search(root, key); //start at the root } //private recursive function bool BinaryTree<T>::search(BTNode<T>* node, const T& key) { if (node == NULL current->key == key) { //found or couldn t find value return (node!= NULL); } else if (key < node->key) { //search the left subtree return search(node->left, key); } else { //search the right subtree return search(node->right, key); } Base Case Recursive Case 1 Recursive Case 2 17
Binary Search Tree Min/Max How would you find the minimum/maximum keys in a binary search tree? Root 60 50 70 30 58 80 46 77 18
Binary Search Tree Min/Max What will our findmin algorithm be? Start at the root 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 19
Binary Search Tree Min/Max T& BinaryTree<T>::getMinimum() { if (root == NULL) { throw runtime_error("tree is empty!"); } //keep going left until we can t anymore BTNode<T> *current = this->root; while(current->left!= NULL) { current = current->left; } return current->key; } 20
Binary Search Tree Traversal Now that we have an implicit ordering to our tree, what happens with the traversal algorithms we previously discussed? In Order Traversal Visits all the nodes in the tree in order 60 Root Order of output: 30 46 50 58 60 70 77 80 50 70 30 58 80 46 77 21
Binary Search Tree Traversal Tree Sort Given an unordered list of elements, add them all to a binary search tree Traverse the binary search tree with an in-order traversal All the nodes come back out sorted! 22
Binary Search Tree Insertion Binary Search Tree Insertion 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 New nodes are always added in the place of an existing NULL...they never reposition other nodes 24
Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Root 60 50 70 30 58 80 46 77 25
Ex: insert 59 Make new node Binary Search Tree Insertion How do you insert a new value into a binary search tree? 60 Root 50 70 30 58 80 New Node 59 46 77 26
Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Make new node Start at root 59 < 60; go left Current 60 Root 50 70 30 58 80 New Node 59 46 77 27
59 > 50; go right Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Make new node Start at root 59 < 60; go left Current 50 60 70 Root 30 58 80 New Node 59 46 77 28
Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Make new node Start at root 59 < 60; go left 60 Root 59 > 50; go right 59 > 58; go right 50 Current 70 30 58 80 New Node 59 46 77 29
Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Make new node Start at root 59 < 60; go left 60 Root 59 > 50; go right 59 > 58; go right Right is NULL 50 70 Insert new Node 30 58 80 46 59 77 30
Binary Search Tree Insertion How do you insert a new value into a binary search tree? Ex: insert 59 Make new node Start at root 59 < 60; go left 60 Root 59 > 50; go right 59 > 58; go right Right is NULL 50 70 Insert new Node 30 58 80 46 59 77 31
Binary Search Tree Remove Binary Search Tree Removal 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 32
Binary Search Tree Remove Remove a node with no children (case #1) Find the node and its parent Set the parent pointer to the node to NULL Delete the node 33
Binary Search Tree Remove How do you remove a value from a binary search tree? Case #1 Ex: remove 46 Root 60 50 70 30 58 80 46 77 34
Binary Search Tree Remove How do you remove a value from a binary search tree? Case #1 Ex: remove 46 Search for the value 60 Root 50 70 30 58 80 To remove 46 77 35
Binary Search Tree Remove How do you remove a value from a binary search tree? Case #1 Ex: remove 46 Search for the value Set parent s pointer to NULL 60 Root 50 70 30 58 80 To remove 46 77 36
Binary Search Tree Remove How do you remove a value from a binary search tree? Case #1 Ex: remove 46 Search for the value Set parent s pointer to NULL Delete the child 60 Root 50 70 30 58 80 To remove 46 77 37
Binary Search Tree Remove How do you remove a value from a binary search tree? Case #1 Ex: remove 46 Search for the value Set parent s pointer to NULL Delete the child 60 Root 50 70 30 58 80 77 38
Binary Search Tree Remove Remove a node with one child (case #2) Find the node and its parent Set the parent pointer to the node s only child Delete the node 39
Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30 Root 60 50 70 30 58 80 46 77 40
Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30 Search for the value 60 Root 50 70 To remove 30 58 80 46 77 41
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 60 Root 50 70 To remove 30 58 80 46 77 42
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 60 Root Delete the node 50 70 To remove 30 58 80 46 77 43
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 50 60 70 Root 58 80 46 77 44
Binary Search Tree Remove Remove a node with two children (case #3) Find the node Remove node s successor or predecessor Replace node s data with successor/predecessor data Predecessor(x) The node in the tree with the biggest value less than x Successor(x) The node in the tree with the smallest value greater than x 45
Binary Search Tree Remove How do you remove a value from a binary search tree? Case #3 Ex: remove 50 Root 60 50 70 30 58 80 46 77 46
Case #3 Ex: remove 50 Search for the value Binary Search Tree Remove How do you remove a value from a binary search tree? To remove Root 60 50 70 30 58 80 46 77 47
Case #3 Ex: remove 50 Search for the value Find predecessor Rightmost value in Binary Search Tree Remove How do you remove a value from a binary search tree? left subtree of node To remove 50 60 70 Root 30 58 80 Predecessor 46 77 48
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 Binary Search Tree Remove How do you remove a value from a binary search tree? Predecessor 30 46 Swap 58 60 70 Root 80 To remove 50 77 49
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 60 Root left subtree of node Swap values of node and its predecessor 46 70 Delete the node via case #1 or case #2 30 58 80 To remove 50 77 50
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 60 Root left subtree of node Swap values of node and its predecessor 46 70 Delete the node via case #1 or case #2 30 58 80 77 51
Binary Search Tree Construction Insert the following values in an empty tree (in this order) 60, 46, 77, 45, 50, 63, 91 What does the resulting tree look like? 52
Binary Search Tree Construction Insert the following values in an empty tree (in this order) 60, 46, 77, 45, 50, 63, 91 What does the resulting tree look like? Tree is perfectly balanced! Height = log(n+1) 60 Root 46 77 45 50 63 91 53
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? 54
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 Root 91 What does the resulting tree look like? Insert 91 55
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 Root 91 What does the resulting tree look like? Insert 91 Insert 77 77 56
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 Root 91 What does the resulting tree look like? Insert 91 Insert 77 Insert 63 63 77 57
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 Insert 63 60 Insert 60 63 Root 77 91 58
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 Root 91 What does the resulting tree look like? Tree is completely unbalanced Height = n 50 60 63 77 46 45 59
Binary Search Tree Efficiency Binary Search Tree (search, add, remove) What are the best case scenarios? What s the Big O for this case? Tree is balanced: O(log(n)) What are the worst case scenarios? What s the Big O for this case? Tree is linear: O(n) 60
CS102 Balanced Binary Search Trees Prof Tejada 1
Balanced Binary Trees Can we ever guarantee a balanced binary tree? 3
Balanced Binary Trees Randomize insertion order If there is no particular order of element insertion that gives us the worst case, then the Big O trends toward O(log(n)) The same argument as randomized quicksort Unfortunately, this is not always possible 4
Balanced Binary Trees Global balance: all leaf nodes are at the maximum depth or at a depth one less than the maximum Local balance: difference between the maximum depth of the left and right subtrees of nonleaf node t is at most 1 Ex: AVL trees There are other types of balanced binary search trees Ex: Red-black trees, Treaps 5
Binary Search Tree Rotation A binary search tree can be balanced There is a single primitive called rotation If rotation is performed at the right place, it can bring a tree into a more balanced state If rotation is performed at the wrong place, it can make a tree even more unbalanced There is basically only one type of rotation Known as a single rotation "single rotation to the left" "single rotation to the right" There is somethingn called "double rotation" It s just two single rotations in a row 6
Binary Search Tree Rotation 7 4 11 x 3 6 9 18 y 2 14 19 12 17 22 Left-Rotate(T, x) 20 7 4 18 y 3 6 x 11 19 2 9 14 22 12 17 20 7
Binary Search Tree Rotation Rotation: (within a subtree) Single right rotation around node a b a C single rotation A b a A B B C Single left rotation around node b b a C single rotation A b a A B B C 8
Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: 5 4 3 2 1 9
Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 4 5 3 2 Single right rotation around node a 1 b a C single rotation A b a A B B C 10
Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 a is 3, b is 2 b 2 a 3 4 5 Single right rotation around node a 1 b a C single rotation A b a A B B C 11
Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 a is 3, b is 2 2 b 4 5 1 3 a Single right rotation around node a b a C single rotation A b a A B B C 12
Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 a is 3, b is 2 Single right rotation around node 5 2 4 1 3 5 Single right rotation around node a b a C single rotation A b a A B B C 13
Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 a is 3, b is 2 Single right rotation around node 5 a is 5, b is 4 1 2 b 4 3 a 5 Single right rotation around node a b a C single rotation A b a A B B C 14
Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree Example: Single right rotation around node 3 a is 3, b is 2 Single right rotation around node 5 a is 5, b is 4 2 1 3 4 b 5 a Single right rotation around node a b a C single rotation A b a A B B C 15
Binary Search Tree Rotation With BST rotation, you can, in theory, balance any tree In practice, you do not want to scan the whole tree to look for a good place to balance For an operation with a BST (such as insert and remove), you must not visit/inspect more than O(log(N)) nodes You need to use the structure of a particular BST to make sure that you stay within this limit AVL trees, Red-black trees, Treaps have different structures 16
AVL Trees AVL Trees Each node knows its height in the tree (not a requirement) Each node calculates a balance factor Balance Factor Height of its left subtree minus the height of its right subtree (or vice-versa) A node with balance factor 1, 0, or -1 is considered balanced and need not be changed A node with any other balance factor is considered unacceptable and must be rebalanced Search/Insert/Remove become O(log(n)) 17
AVL Trees AVL tree example: +1 +1-1 -1-1 +1 +1 +1 +1 0-1 0 0 0 0 +1 0 0 0 0 0 18
AVL Trees AVL tree insertion: Only need to balance at once place (but where?) +1 +1-1 -1-1 +1 +1 +1 +1 0-1 0 0 0 0 +1 0 0 0 0 0 0 19
AVL Trees AVL tree insertion: Only need to balance at once place (but where?) +1 +1-1 -1-1 +1 +1 +1 +1 0-2 0 0 0 0 +1 0-1 0 0 0 0 20
AVL Trees AVL tree insertion: Only need to balance at once place (but where?) +1 +1-1 -1-1 +1 +1 +1 +1 0 0 0 0 0 0 +1 0 0 0 0 0 0 21
AVL Trees AVL tree deletion is more complicated +1 +1-1 -1-1 +1 +1 +1 +1 0-1 0 0 0 0 +1 0 0 0 0 0 22
Red-Black Trees Red/Black Trees Each node stores one extra piece of information: it is colored red or black A binary search tree is a Red-Black Tree if it satisfies the following red/black properties: 1) Every node is red or black 2) The root is black 3) Every leaf (NULL) is black 4) If a node is red, then both of its children are black 5) Every simple paths from a node to any descendant leave node contain the same number of black nodes (4) and (5) imply that the tree is relatively balanced If deepest path from root to a leaf has length X, shortest path from root to a leaf has length X/2 23
Red-Black Trees 13 8 17 1 11 15 25 NIL 6 NIL NIL NIL NIL 22 27 NIL NIL NIL NIL NIL NIL 24
Red-Black Trees Red/Black Trees Properties are maintained via rotations Search/Insert/Remove become O(log(n)) Algorithm is bit complicated 25
Treaps Treaps Merge two of our favorite data structures Binary Search Trees Heaps Add some randomization to insertion Each element in the tree is given a random numeric priority Treap Properties If v is a left child of u, then v < u (BST property) If v is a right child of u, then v > u (BST property) If v is a child of u, the priority(v) < priority(u) (Max-heap property) Search/Insert/Remove become O(log(n)) 26
Treaps example: Letters are node values Numbers are priorities Treaps 4 c 9 h 7 j 2 a 0 e 27
Treaps example: Letters are node values Numbers are priorities Ex: Insert node f Ex: random priority 6 Treaps 2 a 4 c 0 e 9 h 7 j 28
Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap 2 a 4 c 0 e 7 j 6 f 29
Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e 2 a 4 c 0 e 7 j 6 f 30
Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e 2 a 4 c 6 f 7 j 0 e 31
Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e 2 a 4 c 6 f 7 j Not a heap 0 e 32
Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e Not a heap Single left rotation around c 2 a 4 c 0 e 6 f 7 j 33
Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e Not a heap 4 c 6 f 7 j Single left rotation around c 2 a 0 e 34
Treaps Treaps example: Letters are node values 9 h Numbers are priorities Ex: Insert node f Ex: random priority 6 Not a heap Single left rotation around e Not a heap 4 c 6 f 7 j Single left rotation around c It s a heap! Done. 2 a 0 e 35
Other Types of Trees Trees don t have to be binary B-Trees (Trees with N children per node) 2,3,4-Trees (B-Trees with 2-4 children per node) B+-Trees (B-Trees with data only at the leaf) Filesystems look like trees Process hierarchies look like trees Lots of AI and search algorithms use trees to represent complex state transitions A* Search 36