Design and Analysis of Algorithms Binary search trees

Post on 23-Feb-2016

38 views 0 download

description

Design and Analysis of Algorithms Binary search trees. Haidong Xue Summer 2012, at GSU. Operations on a binary search tree. SEARCH(S, k) MINIMUM(S) MAXIMUM(S) SUCCESSOR(S, x) PREDECESSOR(S, x) INSERT(S, x) DELETE(S, x). O(h). O(h). h is the height of the tree. O(h). O(h). O(h) . - PowerPoint PPT Presentation

transcript

Design and Analysis of AlgorithmsBinary search trees

Haidong XueSummer 2012, at GSU

Operations on a binary search tree

• SEARCH(S, k)• MINIMUM(S)• MAXIMUM(S)• SUCCESSOR(S, x)• PREDECESSOR(S, x)• INSERT(S, x)• DELETE(S, x)

O(h)

O(h) h is the height of the treeO(h)

O(h)

O(h)

O(h)

O(h)

What is a binary search tree?

• A binary tree• Binary-search-tree property– For each node, all the nodes in its left sub tree is

smaller than to or equal to this node; all the nodes in its right sub tree is larger than or equal to this node

What the difference between “binary search tree” and a “max-heap”?

Not a complete binary tree

With a different tree property

What is a binary search tree?

11

9 12

8 10 11 12

2

6

5 7

2 5 8

Yes Yes

What is a binary search tree?

2

5

5

6 8

7

1

2

6

4

3

YesYes

What is a binary search tree?

11

9 12

8 12 11 12

10

11

9 25

8 10 20 78

8 13 19

No No

Elements in a binary search tree• Can we use an array to represent a binary

search tree?– No– So some tree structure information has to be

stored

Elements in a binary search tree• binary search tree node {– Key– Satellite data– Left node (left)– Right node (right)– Parent node (p)}

11

9 12

8 10 11 12

2

p

left right

p

left right

p

left right

p

left rightp

left rightp

left right

p

left right

p

left right

11

9 12

8 10 11 12

2

NIL

NIL NIL NIL NIL NIL NIL NIL

NIL NIL

Operations are based on this structure

Print all keys in sorted order• Preorder tree walk• Inorder tree walk• Postorder tree walk

Print all keys in sorted order• Preorder-tree-walk (node x){– If(x==NIL) return;– Access(x);– Preorder-tree-walk(x.left);– Preorder-tree-walk(x.right)}

Print all keys in sorted order• Preorder-tree-walk ( );

Print all keys in sorted order• Inorder-tree-walk (node x){– If(x==NIL) return;– Inorder-tree-walk( x.left);– Access(x);– Inorder-tree-walk( x.right)}

Print all keys in sorted order• Inorder-tree-walk ( );

Print all keys in sorted order• Postorder-tree-walk (node x){– If(x==NIL) return;– Postorder-tree-walk( x.left);– Postorder-tree-walk( x.right)– Access(x);}

Print all keys in sorted order• Postorder-tree-walk ( );

Print all keys in sorted order• Preorder tree walk

• Inorder tree walk

• Postorder tree walk

Print all keys in sorted order• Preorder tree walk

• Inorder tree walk

• Postorder tree walk

11

9 12

8 10 11 12

11 9 8 10 12 11 12

8 9 10 11 11 12 12

8 10 9 11 12 12 11

Sorted order from inorder tree walk!

Print all keys in sorted order

• Time complexity of inorder tree walk– Access each node once

Searching in a binary search tree

TREE-SEARCH• Input: root pointer (x), key (k)• Output: a element whose key is the same as

the input key; NIL if no element has the input key

1. if(x==NIL or x.key==k) return x;2. if(k<x.key) return TREE-SEARCH(x.left, k);3. Return TREE-SEARCH(x.right, k);

11

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 11)11

11

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 11)11

8

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 8)11

TREE-SEARCH( , 8)9

TREE-SEARCH( , 8)8

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 20)11

TREE-SEARCH( , 20)12

TREE-SEARCH( , 20)12

NIL

TREE-SEARCH( NIL , 20)

NIL

Means there is no such a node in the tree

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 20)30

30

Illegal, but never happen if start from the root

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 2)11

What’s the worst case?

Worst successful search

Worst unsuccessful search TREE-SEARCH( , 1)11

O(h)

O(h)

Searching in a binary search treeIterative code could be more efficient than recursive code

TREE-SEARCH (x, k)1. if(x==NIL or x.key==k) return x;2. if(k<x.key) return TREE-SEARCH(x.left, k);3. Return TREE-SEARCH(x.right, k);

TREE-SEARCH (x, k)1. current=x;2. While (current!=NIL and current.key!=k){

if(x.key<k) current=x.left;else current=x.right;

}3. return current;

8

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 8)11

TREE-SEARCH (x, k)1. current=x;2. while (current!=NIL and current.key!=k){

if(x.key<k) current=x.left;else current=x.right;

}3. return current;

Searching in a binary search tree

11

9 12

8 10 11 12

2

TREE-SEARCH( , 20)11

NIL

NIL

Minimum and maximum

11

9 12

8 10 11 12

2

As a human, can you tell where is the minima and maxima?

Minimum and maximum

11

9 12

8 10 11 12

2

The minima of the tree rooted at x is: the minima of x.left if x.left is not NIL; x if x.left is NIL

TREE-MINIMUM( x ) //the recursive one

1. if (x.left==NIL) return x;2. return TREE-MINIMUM(x.left);

0. if(x==NIL) return NIL;

It has some cost, so if x is guaranteed not NIL we can remove it

Minimum and maximum

11

9 12

8 10 11 12

2

TREE-MINIMUM( ) //recursive11

2

Minimum and maximum

11

9 12

8 10 11 12

2

The minima of the tree rooted at x is: the leftmost node

TREE-MINIMUM( x ) //the iterative one

1. current = x;2. while(current.left!=NIL)

current = current.left;3. return current;

0. if(x==NIL) return NIL;

2

Minimum and maximum

11

9 12

8 10 11 12

2

TREE-MINIMUM( ) //iterative11

Minimum and maximum

TREE-MINIMUM( x ) //the iterative one0. if(x==NIL) return NIL;1. current = x;2. while(current.left!=NIL)

current = current.left;3. return current;

TREE-MINIMUM( x ) //the recursive one0. if(x==NIL) return NIL;1. if (x.left==NIL) return x;2. return TREE-MINIMUM(x.left);

TREE-MAXIMUM( x ) //the recursive one0. if(x==NIL) return NIL;1. if (x.right==NIL) return x;2. return TREE-MAXIMUM( x.right);

TREE-MAXIMUM( x ) //the iterative one0. if(x==NIL) return NIL;1. current = x;2. while(current.right!=NIL)

current = current.right;3. return current;

O(h) O(h)

Time complexity?

Successor and predecessor

• What is a successor of x?• What is a successor of x if there is another

node has the same key in the binary search tree?

11

9 12

8 10 11 12

8 9 10 11 11 12 12

Successor and predecessor

15

6 18

3 7 17 20

2 4 13

9

TREE-SUCCESSOR( ) 15

The minimum of the right sub tree

TREE-SUCCESSOR( ) 13

There is no right sub tree

The lowest ancestor whose left child is also an ancestor of or 13 13

Successor and predecessorTREE-SUCCESSOR( x ) // When x has a right sub tree1. if(x.right!=NIl) return TREE-MINIMUM(x);

// When x does not have a right sub tree2. current = x3. currentParent = x.p4. while( currentParent!=NIL and currentParent.left!=current ){

current = currentParent;currentParrent = currenParent.p;

}5. return currentParent;

17

Successor and predecessor15

6 18

3 7 17 20

2 4 13

9

TREE-SUCCESSOR( ) 15

2 3 4 6 7 9 13 15 17 18 20

TREE-MINIMUM

Successor and predecessor15

6 18

3 7 17 20

2 4 13

9

TREE-SUCCESSOR( ) 9

2 3 4 6 7 9 13 15 17 18 20

current

currentParent

current == currentParent.left is true

Has no right sub tree

13

Successor and predecessor15

6 18

3 7 17 20

2 4 13

9

TREE-SUCCESSOR( ) 13

2 3 4 6 7 9 13 15 17 18 20

current

currentParent

current == currentParent.left is falsecurrentParent == NIL is false

No right subtree

current == currentParent.left is falsecurrentParent == NIL is false

current == currentParent.left is true

15

Successor and predecessor15

6 18

3 7 17 20

2 4 13

9

TREE-SUCCESSOR( ) 20

2 3 4 6 7 9 13 15 17 18 20

current

currentParentcurrent == currentParent.left is falsecurrentParent == NIL is false

No right subtree

current == currentParent.left is falsecurrentParent == NIL is false

currentParent == NIL is true

NIL

TREE-SUCCESSOR( x ) // When x has a right sub tree1. if(x.right!=NIl) return TREE-MINIMUM(x);

// When x does not have a right sub tree2. current = x3. currentParent = x.p4. while( !(currentParent==NIL or currentParent.left==current) ){

current = currentParent;currentParrent = currenParent.p;

}5. return currentParent;

TREE-PREDECESSOR( x ) // When x has a left sub tree1. if(x.left!=NIl) return TREE-MAXIMUM(x);

// When x does not have a left sub tree2. current = x3. currentParent = x.p4. while( !(currentParent==NIL or currentParent.right==current) ){

current = currentParent;currentParrent = currenParent.p;

}5. return currentParent;

Time complexity?

O(h)

Insertion and deletion

11

9 19

8 10 18 22

2

As a human, how to insert a element to a binary search tree?

Insertion and deletionTREE-INSERT( T, z ) if(T.root==NIL){ T.root = z; z.p = NIL;}else { INSERT(t.root, z);}

INSERT(x, z)if(z.key<x.key)

if(z.left==NIL){ z.p=x; x.left=z;}else INSERT(x.left, z);

else if(z.right==NIL){ z.p=x; x.right=z;}else INSERT(x.right, z);

Insertion and deletion

11

9 19

8 10 18 22

2

TREE-INSERT( T, ) //recursive 3

Not NIL

Not NIL

Not NIL

NIL

3

Insertion and deletionTREE-INSERT( T, z ) // iterative1. posParent = NIL;2. pos = T.root; // try to find a position, start from T.root3. while(pos!=NIL){4. posParent = pos;5. if(z.key < pos.key)6. pos = pos.left;7. else8. pos = pos.right;9. }10. z.p = posParent;11. if(posParent==NIL); // T is empty12. T.root = z;13. else if(z.key<posParent.key)14. posParent.left = z;15. else16. posParent.right = z;

Find a position

Modify z

Modify posParent

NIL

Insertion and deletion11

9 19

8 10 18 22

2

TREE-INSERT( T, ) //iterative3

pos

posParent

…..3. while(pos!=NIL){4. posParent = pos;5. if(z.key < pos.key)6. pos = pos.left;7. else8. pos = pos.right;9. }…

3

NIL

Insertion and deletion

11

9 19

8 10 18 22

2

As a human, how to delete a element from a binary search tree?

The element has less than 2 children

The element has two children and its successor is the right child

The element has two children and its successor is not the right child

Insertion and deletion

11

9 19

8 10 18 22

2

The element has less than one child

TREE-DELETE( )22 // no child

TREE-DELETE( )8 // no child

TRANSPLANT( T, u, v )

Replace a tree with another tree

Insertion and deletion

TRANSPLANT( T, u, v ) //in T, replace u tree with v tree //modify v1. if(v!=NIL) v.p = u.p;

//modify u’s parent2. if( u.p==NIL)3. T.root = v;4. else if (u == u.p.left)5. u.p.left = v;6. else7. u.p.right=v;

Insertion and deletion11

9 19

8 10 18 22

2

TRANSPLANT( T, u, v ) //modify v1. if(v!=NIL) v.p = u.p;

//modify u’s parent2. if( u.p==NIL)3. T.root = v;4. else if (u == u.p.left)5. u.p.left = v;6. else7. u.p.right=v;

TRANSPLANT( T, , ) 8 2

p

left right

p

left right

Insertion and deletion11

9 19

8 10 18 22

2

TRANSPLANT( T, u, v ) //modify v1. if(v!=NIL) v.p = u.p;

//modify u’s parent2. if( u.p==NIL)3. T.root = v;4. else if (u == u.p.left)5. u.p.left = v;6. else7. u.p.right=v;

TRANSPLANT( T, , .right) 222

p

left right

NIL

Insertion and deletion11

9 19

8 10 18 22

2

TRANSPLANT( T, u, v ) //modify v1. if(v!=NIL) v.p = u.p;

//modify u’s parent2. if( u.p==NIL)3. T.root = v;4. else if (u == u.p.left)5. u.p.left = v;6. else7. u.p.right=v;

TRANSPLANT( T, , ) 18 9

p

left right

p

left right

Insertion and deletion11

19

18 22

9

8 10

2

TRANSPLANT( T, u, v ) //modify v1. if(v!=NIL) v.p = u.p;

//modify u’s parent2. if( u.p==NIL)3. T.root = v;4. else if (u == u.p.left)5. u.p.left = v;6. else7. u.p.right=v;

TRANSPLANT( T, , ) 18 9

TREE-DELETE( T, z) // When z has less than two children1. if(z.left==NIL) TRANSPLANT(T, z, z.right)2. else if (z.right==NIL) TRANSPLANT(T, z, z.left)// When z has two children3. else{ //Get the successor of z4. y= TREE-MINIMUM(z.right); // it is TREE-SUCCESSOR(z) // if the successor is not z’s right child5. if(z.right != y){ // upgrade the succesor’s right6. TRANSPLANT(T, y, y.right); // assign z’right to the successor 7. y.right = z.right;8. y.right.p = y;9. } // replace z with the successor8. TRANSPLANT(T, z, y); // assign z’left to the successor9. y.left = z.left;10. y.left.p = y.left;11. }

Insertion and deletion

TREE-DELETE( T, ) 15

6 18

3 7 17 20

2 4 13

9

7

Only one child

Insertion and deletionTREE-DELETE( T, )

15

6 18

3 17 20

2 4

7

13

9

6

Find the successor

Replace with the successor tree

6

Assign .left to the successor6

7

Insertion and deletionTREE-DELETE( T, )

15

6 19

3 7 17 20

2 4 13

9

15

Find the successor

Replace the successor with its right tree

Assign .left to the successor15

17

18

Replace with the successor

15

Assign .right to the successor15

Insertion and deletionTime complexity

TREE-INSERT(T, x)

TREE-DELETE(T, x)

Similar to TREE-SEARCH, O(h)

Because of TREE-SUCCESSOR, O(h)

How to build a binary search tree?

• By insertion• When it is done it randomly, the expected

height is O(lgn)• What is the worst case?– There is only 1 leaf

• How to avoid the worst case?– Randomly insert– Variations of binary search tree like RBT