Upload
buithu
View
218
Download
0
Embed Size (px)
Citation preview
Binary Search Trees 1
Binary Search Trees
class node int key;node *left, *right, *parent;
;
• A Binary Search Tree is a binary tree with thefollowing properties: Given a nodex in the tree
– if y is a node in the left subtree ofx, theny->key ≤ x->key.
– if y is a node in the right subtree ofx, thenx->key ≤ y->key.
23
17
21
13
10
9
8
7
5
1
3
4
• We assume that all keys are distinct.
Binary Search Trees 2
Binary Search Tree Operations
• Given a binary search tree, there are severaloperations we want to perform.
– Insert an element
– Deletean element
– Searchfor an element
– Find theminimum/maximum element
– Find thesuccessor/predecessorof a node.
• Once we see how these are done, it will beapparent that the complexity of each of these isΘ(h), whereh is the height of the tree.
• The insert and delete operations are the hardest toimplement.
• Finding the minimum/maximum and searchingare the easiest, so we will start with these.
Binary Search Trees 3
BST: Minimum/Maximum
• The minimum element is the left-most node, themaximum element is the right-most.
• The following functions find the min/max elementin the BST rooted atx.
node *Find_Min(x) node *Find_Max(x) while(x->left!=NULL) while(x->right!=NULL)x=x->left; x=x->right;
return x; return x;
23
17
21
13
10
9
8
7
5
1
3
4
Minimum
Maximum
Binary Search Trees 4
BST: Searching
• Searching for an element in a binary search tree isvery straightforward:
• The following function searches for the valuek inthe tree rooted atx.
node *Search(node *x, int k) while(x != NULL && k != x->key)
if(k < x->key)x = x->left;
elsex = x->right;
return x;
23
17
21
13
10
9
8
7
5
1
3
4
x
NIL
Search(x,16)Search(x,10)
Binary Search Trees 5
BST: Successor/Predecessor
• Finding the Successor/Predecessor of a node is alittle harder.
• To find the successory of a nodex (if it exists)
– If x has a nonempty right subtree, theny is thesmallest element in the tree rooted atx->right. Why?
– If x has an empty right subtree, theny is thelowest ancestor ofx whose left child is also anancestora of x. (Of course!)
node *Successor(node *x) if(x->right != NULL)
return Find_Min(x->right);y = x->parent;while(y != NULL && x == y->right)
x = y;y = y->parent;
return y;
• The predecessor operation is symmetric tosuccessor.
ax is one of its own ancestors
Binary Search Trees 6
BST: Successor Argument
• So, why is it that ifx has an empty right subtree,theny is the lowest ancestor ofx whose left childis also an ancestor ofx?
• Let’s look at it the other way.
• Let y be the lowest ancestor ofx whose left childis also an ancestor ofx.
• What is the predecessor ofy?
• Sincey has a left child, it must be the largestelement in the tree rooted aty->left
• If x is not the largest element in the subtree rootedaty->left, then some ancestor ofx (in thesubtree) is the left child of its parent.
• But y, which is not in this subtree, is the lowestsuch node.
• Thusx is the predecessor ofy, andy is thesuccessor ofx.
Binary Search Trees 7
BST: Successor Examples
23
17
21
13
10
9
8
7
5
1
3
4
23
17
21
13
10
9
8
7
5
1
3
4
Find_Min
y
x1
1
2
2
Successor(7)=8
Successor(10)=13
Binary Search Trees 8
BST: Insertion
• To insert a node into a binary tree, we search thetree until we find a node whose appropriate childis NULL. We insert the new node there.
• T is the tree, andz the node we wish to insert.Insert(T,z)
node *y=NULL;node *x=T.root;while(x != NULL)
y = x;if(z->key < x->key)x = x->left;
elsex = x->right;
z->parent = y;if(y == NULL)
T.root = z;else
if(z->key < y->key)y->left = z;
elsey->right = z;
Binary Search Trees 9
BST: Insertion Example
178
9
10
178
9
10
23
21
13
7
5
1
3
4
6 22
23
21
13
7
5
1
3
4
22186
y
y
Insert(T,z)
Insert(T,z)
z
z
Binary Search Trees 10
BST: Deletion
• Deleting a nodez is by far the most difficultoperation.
• There are 3 cases to consider:
– If z has no children, just delete it.
– If z has one child, splice outz. That is, linkz’s parent and child.
– If z has two children, splice outz’s successory, and replace the contents ofz with thecontents ofy.
• The last case works because ifz has 2 children,then its successor has no left child. Why?
• Deletion is made worse by the fact that we have toworry about boundary conditions
• To simplify things, we will first define a functioncalledSpliceOut.
Binary Search Trees 11
BST: Splice Out
• Any node with≤ 1 child can be “spliced out”.
• Splicing out a node involves linking the parentand child of a node.
• The algorithm:SpliceOut(tree T, node *y) if(y->left != NULL && y->right != NULL)
return; //Two children; can’t splice out
if(y->left != NULL) //Locate child of yx = y->left;
else if (y->right != NULL)x = y->right;
elsex = NULL;
if(x != NULL) //If y has child, set parentx->parent = y->parent;
//Set y’s parent’s child to y’s childif(y->parent == NULL)
T.root = x;else
if(y == y->parent->left)y->parent->left = x;
elsey->parent->right = x;
Binary Search Trees 12
BST: SpliceOut Examples
10
8
7
3
4
23
17
21
13
10
9
8
7
5
1
3
4
z
23
17
21
13
10
9
8
7
1
3
4
23
17
21
13
7
5
1
3
4 10
9
23
17
21
13
10
9
8
7
5
1
3
4z
13
10
8
7
3
4
z
SpliceOut(T,z)
SpliceOut(T,z)
SpliceOut(T,z)
Binary Search Trees 13
BST: Deletion Algorithm
• Once we have defined the function SpliceOut,deletion looks simple.
• Here is the algorithm to deletez from treeT.Delete(tree T, node *z)
if(z->left == NULL || z->right == NULL)SpliceOut(T,z);
else y = Successor(z);z->key = y->key;SpliceOut(T,y);
Binary Search Trees 14
BST: Deletion Examples
23
17
21
13
10
8
7
5
1
3
4
23
17
21
13
10
9
8
7
5
1
3
4
z
23
17
21
13
10
9
8
7
5
1
3
4
23
17
21
13
8
7
5
1
3
4
zz 9
23
17
21
13
7
1
3
4
z
8
9
11
12
10
y
23
17
21
13
1
3
4
9
11
12
10
8
Delete(T,z)
Delete(T,z)
Delete(T,z)
Binary Search Trees 15
BST: Time Complexity
• We stated earlier, and have now seen, that all ofthe BST operations have time complexityΘ(h),whereh is the height of the tree.
• However, in the worst case, the height of a BST isΩ(n), wheren is the number of nodes.
• In this case, the BST has gained us nothing.
• To prevent this worst-case behavior, we need todevelop a method which ensures that the height ofa BST is kept to a minimum.
• Red-Black Treesare binary search trees whichhave heightΘ(log n).
Binary Search Trees 16
Red-Black Trees
• A red-black tree is a binary search tree with thefollowing properties:
– Each node is colored eitherred or black.
– Every leaf (NULL) is black.
– If a node is red, both its children are black.
– Every simple path from a node to a descendentleaf has the same number of black nodes.
1
83
7 415
119
27
23
13
30
4
6
10
• The leaf nodes are all empty and black, so we willomit them in the figures.
• When we talk about the nodes in a red-black tree,we will mean the internal nodes.
Binary Search Trees 17
Red-Black Trees Fact and Terms
• Theblack-height of a nodex is the number ofblack nodes, not includingx, on a path to any leaf.
• A red-black tree withn nodes has height at most2 log(n + 1).
• Sincered-black treesare binary search trees, allof the operations that can be performed on binarysearch trees can be performed on them.
• Furthermore, the time complexity will be thesame–O(h)–whereh is the height.
• Unfortunately, insertion and deletion as definedfor regular binary search trees will not work forred-black trees. Why not?
• Fortunately, insertion and deletion can both bemodified so that they work, and still have timecomplexityO(h).
Binary Search Trees 18
Insert and Delete in RB Trees
• Here are a few examples of why inserting a nodeinto a red-black tree is not trivial.
Insert(9)13
83
11
13
83
11
13
83
11
13
83
11
Insert(10)
10
??
1
7
1 4 9
24
7
41 9
10
24
1 4
7
9
24
7
24
4
• Similar things happen when we try to deletenodes.
• We will not discuss in depth these operations.
• We will discuss some of the concepts, however.
Binary Search Trees 19
Red-Black Tree Insertion: Method
• To insert a nodex into a red-black tree, we do thefollowing:
– Insertx with the standard BSTInsert.
– Colorx red.
– If x’s parent is red, fix the tree.
• Notice thatx’s children,NULL, are black.
• Since we coloredx red, we have not changed theblack height.
• The only problem we have is (possibly) having ared node with a red child.
• Fixing the tree involves re-coloring some of thenodes and performing rotations.
Binary Search Trees 20
Left- and Right-Rotations
• Rotations are best defined by an illustration:
x
yA
C
x
y
B
C
A BLeft-Rotate(T,x)
Right-Rotate(T,y)
• Here, the lettersA, B, andC represent arbitrarysubtrees. They could even be empty.
• It is not too hard to see that the binary search treeproperty will still hold after a rotation.
Binary Search Trees 21
Rotation Example
23
17
13
23
17
13
10
9
8
7
53
4
x
Right-Rotate(T,x)
7
4
10
9
8
3
5
Binary Search Trees 22
Rotation Example
23
17
13
7
4
10
9
8
3
5
23
17
13
4
3
7
5
10
8
9
z
Left-Rotate(T,z)
Binary
Search
Trees
23
Insertion Example
13
83
11
13
83
11
x
Recolor
Insert(T,10)
Left−Rotation(T,x)
13
3
11
9
13
3
11
8
7
4 91
24
7
24
10
94
1 4
7
8 10
24
1 4
7
9
10
24
1
Binary Search Trees 24
Red Black Tree Summary
• Red-black trees are binary search trees whichhave heightΘ(log n) guaranteed.
• The basic operations can all be implemented intimeO(log n).
• Although inserting and deleting nodes onlyrequires timeO(log n), they are nonetheless nottrivial to implement.
• A regular binary search tree does not guaranteetime complexity ofO(log n), only O(h), whereh
can be as large asn.
• Thus red-black trees are useful if one wants toguarantee that the basic operations will takeO(log n) time.