Presentation is loading. Please wait.

Presentation is loading. Please wait.

Balanced binary search trees

Similar presentations


Presentation on theme: "Balanced binary search trees"— Presentation transcript:

1 Balanced binary search trees
Red Black Trees Balanced binary search trees

2 Binary Trees Tree may be empty Each node may have 0, 1, or two children The time it takes to search the tree is related to the number of nodes {O(n) worst case} How can that be???? We can search an ordered array in O(lg n), but a binary tree has no order.

3 Binary Search Trees Same rules as a binary tree …PLUS for any given node: The key values are comparable All nodes in the left child tree have values less than the node All nodes in the right child tree have values greater than the node Duplicates Not allowed (all duplicate key values are ignored) May be in the left subtree (all left node key values are strictly not greater than a node’s key value) May be in the right subtree (all left node key values are strictly less than a node’s key value) Handled by increasing the count of duplicates for a given node. This is not really useful without additional programming

4 BST of Pool Balls

5 How do we delete 16? 11? 4?

6 Insertion of Data In-order is Problematic
When the data is presented in order we get a right-leaning tree of depth n When the data is presented in reverse order we get a left-leaning tree of depth n Both have a search time on the order of O(n) which is not close to O(log n) How can we balance the BST? When we have no control over the data?

7 Search is Dependent Upon Tree Depth
It takes O(depth) to search for a value (worst case) It should be possible to achieve a depth of log n This is a problem if the input data is presented in some order In order creates a strongly right-leaning tree with a depth of n Reverse order creates a strongly left-leaning tree with a depth of n Randomizing the input should give better results, but may not You cannot randomize the input if it is received one value at a time…oops.

8 Red Black Trees Adjustments to BST Every node is either red or black
The root node is black (some implementations allow the root to be red) Every leaf node (nil) is black If a node is red, its children are black For each node, all simple paths from the node to descendant leaves contain the same number of black nodes

9 Effects of These Rules These simple rules have important side effects:
Adding a node may require recoloring all ancestors up to, and including, the root Adding a node may require restructuring the tree Add a node and then do an addition fixup routine Deleting a node may require recoloring all ancestors Deleting a node may require restructuring the tree Delete a node and then do a deletion fixup routine All of these can be done in O(log n) time After all the recoloring and restructuring, the red-black properties are preserved A red black tree has a depth of no more than 2 (log n+1) no matter how the key values are input

10 A Graphical Demo of a Red Black Tree
Watch for: Does the order the key values are added change the resulting tree? Is there a fix up phase after each add? Does the order the key values are deleted change the resulting tree? Is there a fix up phase after each delete If a key value is deleted and then immediately re-added, is the same tree re-constructed? After each add or delete Is the tree correctly rebalanced (red-black properties preserved)? Is the tree perfectly rebalanced?

11 DEMO!!! Things to explore: Does the input order matter?
What happens if I delete a node? Do I get the same tree if I delete a node and then insert it back?

12 What Good Are Red Black Trees?
Provides a balanced search tree in reasonable time Searchable in at worst O( 2 lg n+1) Can add and delete keys (nodes) in a reasonable time A reasonable method to sort data that is presented one value at a time Sorts in close to O(n log n) Beats Insertion Sort if data is in reverse order Can insure a tree that can be searched close to the optimal time Can be used to build a priority tree by always deleting the lowest (highest) key value and allowing the tree to rebalance Might be useful as a data structure any time a search or sort is needed.

13 In Closing…. Binary Search Trees can greatly speed searching only if the BSTs are balanced Balanced BST can be searched in O(tree-depth) BST have an optimal depth of lg n+1, which requires a specific input order Red-Black Trees use color and simple rules to achieve a relatively balanced BST regardless of the input order The final Red-Black Tree depends heavily upon the input order Updates to a Red-Black Tree (including recoloring and restructuring) are relatively fast On the order of O(lg n) Usually no worse than O(depth) if done from the leaves to the root

14 Questions?


Download ppt "Balanced binary search trees"

Similar presentations


Ads by Google