560 likes | 592 Views
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).
E N D
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 • Average depth of a BST is O(log n) • Maximum depth of a BST is O(n) The same set of keys may have different BSTs
The Dictionary Data Type • A dictionary is a collection of items, similar to a bag. • But unlike a bag, each item has a string attached to it, called the item's key.
The Dictionary Data Type • A dictionary is a collection of items, similar to a bag. • But unlike a bag, each item has a string attached to it, called the item's key. Example: The itemsI am storing are records containing data about a state.
The Dictionary Data Type • A dictionary is a collection of items, similar to a bag. • But unlike a bag, each item has a string attached to it, called the item's key. Example: The keyfor each record is the name of the state. Washington
void dictionary::insert(The key for the new item, The new item); The Dictionary Data Type • The insertion procedure for a dictionary has two parameters. Washington
Item dictionary::retrieve("Washington"); The Dictionary Data Type • When you want to retrieve an item, you specify the key...
When you want to retrieve an item, you specify the key... ... and the retrieval procedure returns the item. Item dictionary::retrieve("Washington"); The Dictionary Data Type
The Dictionary Data Type • We'll look at how a binary tree can be used as the internal storage mechanism for the dictionary.
Arizona Arkansas A Binary Search Tree of States Florida The data in the dictionary will be stored in a binary tree, with each node containing an item and a key. Oklahoma Colorado Mass. Washington New Hampshire West Virginia
Colorado Arizona Arkansas A Binary Search Tree of States Florida Storage rules: • Every key to the left of a node is alphabetically before the key of the node. Oklahoma Colorado Mass. Washington New Hampshire West Virginia
Arizona A Binary Search Tree of States Florida Storage rules: • Every key to the left of a node is alphabetically before the key of the node. Oklahoma Colorado Mass. Washington Example: “Massachusetts” and “ New Hampshire” are alphabetically before “Oklahoma” New Hampshire West Virginia Arkansas
Arizona A Binary Search Tree of States Florida Storage rules: • Every key to the left of a node is alphabetically before the key of the node. • Every key to the right of a node is alphabetically after the key of the node. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Arizona A Binary Search Tree of States Florida Storage rules: • Every key to the left of a node is alphabetically before the key of the node. • Every key to the right of a node is alphabetically after the key of the node. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
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)
Arizona Retrieving Data Florida Start at the root. • If the current node has the key, then stop and retrieve the data. • If the current node's key is too large, move left and repeat 1-3. • If the current node's key is too small, move right and repeat 1-3. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Arizona Retrieve " New Hampshire" Florida Start at the root. • If the current node has the key, then stop and retrieve the data. • If the current node's key is too large, move left and repeat 1-3. • If the current node's key is too small, move right and repeat 1-3. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Arizona Retrieve "New Hampshire" Florida Start at the root. • If the current node has the key, then stop and retrieve the data. • If the current node's key is too large, move left and repeat 1-3. • If the current node's key is too small, move right and repeat 1-3. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Arizona Retrieve "New Hampshire" Florida Start at the root. • If the current node has the key, then stop and retrieve the data. • If the current node's key is too large, move left and repeat 1-3. • If the current node's key is too small, move right and repeat 1-3. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Arizona Retrieve "New Hampshire" Florida Start at the root. • If the current node has the key, then stop and retrieve the data. • If the current node's key is too large, move left and repeat 1-3. • If the current node's key is too small, move right and repeat 1-3. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Inorder Traversal of BST • Inorder traversal of BSTprints out all the keys in sorted order Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
findMin/ findMax • 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 downthe 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)
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
Arizona Adding a New Item with aGiven Key Florida • 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. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Arizona Adding Iowa Florida • 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. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Arizona Adding Iowa Florida • 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. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Arizona Adding Iowa Florida • 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. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Arizona Adding Iowa Florida • 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. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Arizona Adding Iowa Florida • 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. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas
Arizona Adding Florida • 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. Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas
Where would you add this state? Arizona Adding Kazakhstan Florida Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas
Kazakhstan is the new right child of Iowa? Arizona Adding Florida Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas Kazakhstan
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 • Case 3: the node has 2 children • Replace the key of that node with the minimum element at theright 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)
Arizona Removing an Item with a Given Key Florida • Find the item. • If necessary, swap the item with one that is easier to remove. • Remove the item. Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas Kazakhstan
Arizona Removing "Florida" Florida • Find the item. Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas Kazakhstan
Arizona Florida cannot be removed at the moment... Removing "Florida" Florida Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas Kazakhstan
Arizona ... because removing Florida would break the tree into two pieces. Removing "Florida" Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas Kazakhstan
Arizona The problem of breaking the tree happens because Florida has 2 children. Removing "Florida" Florida • If necessary, do some rearranging. Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas Kazakhstan
Arizona For the rearranging, take the smallest item in the right subtree... Removing "Florida" Florida • If necessary, do some rearranging. Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas Kazakhstan
Iowa Arizona ...copy that smallest item onto the item that we're removing... Removing "Florida" • If necessary, do some rearranging. Oklahoma Colorado Mass. Washington Iowa New Hampshire West Virginia Arkansas Kazakhstan
Iowa Arizona ... and then remove the extra copy of the item we copied... Removing "Florida" • If necessary, do some rearranging. Oklahoma Colorado Mass. Washington New Hampshire West Virginia Arkansas Kazakhstan
Iowa Arizona Removing "Florida" • If necessary, do some rearranging. Oklahoma Colorado Mass. Washington ... and reconnect the tree Kazakhstan New Hampshire West Virginia Arkansas