Binary Search Trees One of the tree applications
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags. This presentation illustrates how another data type called a dictionary is implemented with binary search trees.
Binary Search Trees (BST) A data structure for efficient searching, insertion and deletion Binary search tree property For every node x All the keys in its left subtree are smaller than the key value in x All the keys in its right subtree are larger than the key value in x
Binary Search Trees A binary search tree Not a binary search tree
Binary Search Trees The same set of keys may have different BSTs Average depth of a BST is O(log n) Maximum depth of a BST is O(n)
A Binary Search Tree of States rida Flo Oklahoma Colorado Mass. Arizona Arkansas West Virginia Washington New Hampshire Storage rules: Every key to the left of a node is alphabetically before the key of the node.
A Binary Search Tree of States rida Flo Mass. Washington Arkansas West Virginia Example: “Massachusetts” and “ New Hampshire” are alphabetically before “Oklahoma” Oklahoma New Hampshire Storage rules: Every key to the left of Colorado a node is alphabetically before the key of the node. Arizona
A Binary Search Tree of States rida Flo Oklahoma Mass. West Virginia Washington New Hampshire Storage rules: Every key to the left of Colorado a node is alphabetically before the key of the node. Arizona Every key to the right of a node is alphabetically after Arkansas the key of the node.
A Binary Search Tree of States rida Flo Oklahoma Mass. West Virginia Washington New Hampshire Storage rules: Every key to the left of Colorado a node is alphabetically before the key of the node. Arizona Every key to the right of a node is alphabetically after Arkansas the key of the node.
Searching BST If we are searching for 15, then we are done. If we are searching for a key < 15, then we should search in the left subtree. If we are searching for a key > 15, then we should search in the right subtree.
Searching (Find) Find X: return a pointer to the node that has key X, or NULL if there is no such node Time complexity: O(depth of the tree)
Retrieving Data rida Flo Oklahoma Colorado Mass. Arizona Arkansas West Virginia Washington New Hampshire Start at the root. q If the current node has the key, then stop and retrieve the data. q If the current node's key is too large, move left and repeat 1 -3. q If the current node's key is too small, move right and repeat 1 -3.
Retrieve " New Hampshire" rida Flo Oklahoma Colorado Mass. Arizona Arkansas West Virginia Washington New Hampshire Start at the root. q If the current node has the key, then stop and retrieve the data. q If the current node's key is too large, move left and repeat 1 -3. q If the current node's key is too small, move right and repeat 1 -3.
Inorder Traversal of BST Inorder traversal of BST prints out all the keys in sorted order Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
find. Min/ find. Max Goal: return the node containing the smallest (largest) key in the tree Algorithm: Start at the root and go left (right) as long as there is a left (right) child. The stopping point is the smallest (largest) element Time complexity = O(depth of the tree)
Insertion Proceed down the tree as you would with a find If x is found, do nothing (or update something) Otherwise, insert x at the last spot on the path traversed Time complexity = O(depth of the tree)
rida Flo Adding a New Item with a Given Key Oklahoma Colorado Mass. Arizona Washington Arkansas West Virginia q Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. New Hampshire q
Adding Iowa rida Flo Oklahoma Colorado Mass. Arizona Washington Arkansas West Virginia q Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. New Hampshire q
Adding Iowa rida Flo Oklahoma Colorado Mass. Arizona Washington Arkansas West Virginia q Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. New Hampshire q
Adding Iowa rida Flo Oklahoma Colorado Mass. Arizona Washington Arkansas West Virginia q Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. New Hampshire q
Adding Iowa rida Flo Oklahoma Colorado Mass. Arizona Washington Arkansas West Virginia q Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. New Hampshire q
Adding Iowa rida Flo Oklahoma Colorado Mass. Arizona Washington Arkansas West Virginia q Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. New Hampshire q
Adding rida Flo Oklahoma Colorado Mass. Arizona Washington Iowa Arkansas West Virginia q Pretend that you are trying to find the key, but stop when there is no node to move to. Add the new node at the spot where you would have moved to if there had been a node. New Hampshire q
Adding Ka zak hst an rida Flo Oklahoma Colorado Mass. Arizona Iowa Arkansas West Virginia Washington New Hampshire Where would you add this state?
Adding rida Flo Oklahoma Colorado Mass. Arizona Iowa Arkansas Ka zak hst an West Virginia Washington New Hampshire Kazakhstan is the new right child of Iowa?
Deletion When we delete a node, we need to consider how we take care of the children of the deleted node. This has to be done such that the property of the search tree is maintained.
Deletion under Different Cases Case 1: the node is a leaf Delete it immediately Case 2: the node has one child Adjust a pointer from the parent to bypass that node
Deletion Case 3: the node has 2 children Replace the key of that node with the minimum element at the right subtree Delete that minimum element Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2. Time complexity = O(depth of the tree)
Given rida Flo Removing an Item with a Key q Oklahoma Colorado Mass. Arizona Washington Iowa Arkansas Ka zak hst an West Virginia q Find the item. If necessary, swap the item with one that is easier to remove. Remove the item. New Hampshire q
Removing "Florida" rida Flo Find the item. Oklahoma Colorado Mass. Arizona Iowa Arkansas Ka zak hst an West Virginia Washington New Hampshire q
Removing "Florida" rida Flo Oklahoma Colorado Mass. Arizona Florida cannot be removed at the moment. . . Arkansas New Hampshire Iowa Ka zak hst an West Virginia Washington
Removing "Florida" Oklahoma Colorado Mass. Iowa Arkansas Ka zak hst an West Virginia Washington New Hampshire . . . because removing Florida would break the tree into two pieces. Arizona
Removing "Florida" rida Flo q If necessary, do some rearranging. Oklahoma Colorado Mass. Iowa Arkansas Ka zak hst an West Virginia Washington New Hampshire The problem of breaking the tree happens because Florida has 2 children. Arizona
Removing "Florida" rida Flo If necessary, do some rearranging. Oklahoma Colorado Mass. Arizona For the rearranging, take the smallest item in the right subtree. . . Iowa Arkansas Ka zak hst an West Virginia Washington New Hampshire q
Removing "Florida" Iowa If necessary, do some rearranging. Oklahoma Colorado Mass. Arizona . . . copy that smallest item onto the item that we're removing. . . Iowa Arkansas Ka zak hst an West Virginia Washington New Hampshire q
Removing "Florida" Iowa If necessary, do some rearranging. Oklahoma Colorado Mass. Arizona . . . and then remove the extra copy of the item we copied. . . Arkansas Ka zak hst an West Virginia Washington New Hampshire q
Removing "Florida" Iowa If necessary, do some rearranging. Oklahoma Colorado Mass. Arizona Washington Ka . . . and reconnect the tree Arkansas sta n West Virginia zak h New Hampshire q
Removing "Florida" rida Flo Oklahoma Colorado Arizona Arkansas Ka zak hst an West Virginia Washington New Hampshire Why did I choose the smallest item in the right subtree? Mass.
Removing "Florida" Iowa Oklahoma Colorado Mass. Washington Ka Arkansas sta n West Virginia zak h New Hampshire Because every key must be smaller than the keys in its right subtree Arizona
Removing an Item with a Key q q Given Find the item. If the item has a right child, rearrange the tree: Find smallest item in the right subtree Copy that smallest item onto the one that you want to remove Remove the extra copy of the smallest item (making sure that you keep the tree connected) else just remove the item.
Summary Binary search trees are a good implementation of data types such as sets, bags, and dictionaries. Searching for an item is generally quick since you move from the root to the item, without looking at many other items. Adding and deleting items is also quick.
- Slides: 41