1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the...

18
1 BST Trees 5 3 8 1 4 9

Transcript of 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the...

Page 1: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

1

BST Trees

5

3 8

1 4 9

Page 2: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

2

A binary search tree is a binary tree in which every node satisfies the following:

• the key of every node in the left subtree is smaller than the key of this node• the key of every node in the right subtree is larger than the key of this node

Note: Duplication nodes ?

A binary search tree is a binary tree in which every node satisfies the following:

• the key of every node in the left subtree is smaller than the key of this node• the key of every node in the right subtree is larger than the key of this node

Note: Duplication nodes ?

Binary search tree (BST)

5

3 8

1 4 9

Page 3: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

3

Binary search tree

5

3 8

1 4 9

The left subtree and the right subtree are also BST (Recursion)

No two nodes in a BST have the same key

If we traverse a BST inorder, we list the key in ascending order

Page 4: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

4

Some examples

8

15

10

2 12

6 14

3

4

16

1

7

9

3

5

8

8

4

2 6

1 3 75

12

10 14

9 11 1513

Page 5: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

5

Is this a BST?

5

3 8

1 7 9

This is NOT BST!

Every node satisfies the following: -- the key of the left node is smaller than the its own key -- the key of the right node is larger than its own key

Every node satisfies the following: -- the key of the left node is smaller than the its own key -- the key of the right node is larger than its own key

This is an incorrect check of BST.This is an incorrect check of BST.

Page 6: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

6

Search BST

8

4

2 6

1 3 75

12

10 14

9 11 1513

target

To find a key in a BST, we start at the root. Compare the target key with the key of the current node.

- target == p->key, Done.- target < p->key, go left- target > p->key, go right

To find a key in a BST, we start at the root. Compare the target key with the key of the current node.

- target == p->key, Done.- target < p->key, go left- target > p->key, go right

Note that the principle behind is similar to binary search...

Note that the principle behind is similar to binary search...

5Find

Page 7: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

7

Basic Functions

Search Find Min Find Max Insert(x) Delete

Page 8: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

8

C++ Implementation of BST Search // Internal method to find an item in a subtree.

// x is item to search for // t is the node that roots the tree // Return node containing the matched item.

template <class Comparable> BinaryNode<Comparable> * BinarySearchTree<Comparable>:: find( const Comparable & x, Node *t ) const { while( t != NULL ) { if( x < t->element ) // element is the key value for each node t = t->left; else if( t->element < x ) t = t->right; else return t; // Match } return NULL; // Not found }

// Internal method to find an item in a subtree. // x is item to search for // t is the node that roots the tree // Return node containing the matched item.

template <class Comparable> BinaryNode<Comparable> * BinarySearchTree<Comparable>:: find( const Comparable & x, Node *t ) const { while( t != NULL ) { if( x < t->element ) // element is the key value for each node t = t->left; else if( t->element < x ) t = t->right; else return t; // Match } return NULL; // Not found }

Page 9: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

9

Balance tree

8

4

2 6

1 3 75

12

10 14

9 11 1513

1

13

15

2

The efficiency of BST search depends on the shape of the tree. If the tree is balance, (minimum height, very bushy), the search is fast (log(n) steps). If the tree is a long narrow chain, the search is slow (n steps)

The efficiency of BST search depends on the shape of the tree. If the tree is balance, (minimum height, very bushy), the search is fast (log(n) steps). If the tree is a long narrow chain, the search is slow (n steps)

14

3

4

5

log(16)=4

Compare this with sequential and binary search ...

Compare this with sequential and binary search ...

Page 10: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

10

Binary Search Tree Class - FindMin/Maxtemplate <class Comparable>

BinaryNode<Comparable> * BinarySearchTree<Comparable>::findMin( Node *t ) const{

if( t != NULL ){

while( t->left != NULL )

t = t->left; // the leftmost node

}

return t;

}

template <class Comparable>

BinaryNode<Comparable> * BinarySearchTree<Comparable>::findMax( Node *t ) const{

If( t != NULL )

while( t->right != NULL )

t = t->right; // the rightmost node

return t;

}

Page 11: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

11

Insert & Delete in BST First we’ll study simple-minded Insert

and Delete procedure. They may result in highly unbalanced tree

Then we’ll study a special kind of BST called AVL tree which maintains near balance in inserting and deleting.

Page 12: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

12

Insert in BST (recursive version)// Internal method to insert into a subtree

// x is the item to insert// t is the node that roots the tree// Set the new root// Throw an exception if x is already in ttemplate <class Comparable>void BinarySearchTree<Comparable>::insert( const Comparable & x, Node * & t ) const {

if( t == NULL )t = new Node( x, NULL, NULL );

else if( x < t->element )insert( x, t->left );

else if( x > t->element)insert( x, t->right );

elsethrow DuplicateItemException( );

}

6

31 158

92

10

5 12

4 7 14

17

New nodes are always added as leaves.

New nodes are always added as leaves.

Page 13: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

13

Delete in BST

Case 1: If it has no children, that is, it is a leaf, we just remove it.

Case 1: If it has no children, that is, it is a leaf, we just remove it.

Page 14: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

14

Delete in BST

L

L

Case 2: If it has only one child, (i.e. only one subtree), we splice out it. That is, we attach that subtree to the parent of the node.

Case 2: If it has only one child, (i.e. only one subtree), we splice out it. That is, we attach that subtree to the parent of the node.

Page 15: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

15

Delete in BST, examples

6

16

15

9

10

5 12

7 14

17

6

16

15

9

10

127

14

17

6

16

15

9

10

127

17

Case 2: delete a node that has only one subtree. Case 2: delete a node that has only one subtree.

Page 16: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

16

Delete in BST

Case 3: If it has nonempty left and right subtrees. • Find the minimum node in right subtree, then copy its key value to X, and remove the min node from right subtree.• Or Find the min node in right subtree, then swap it with the target node, and remove the target node

Case 3: If it has nonempty left and right subtrees. • Find the minimum node in right subtree, then copy its key value to X, and remove the min node from right subtree.• Or Find the min node in right subtree, then swap it with the target node, and remove the target node

RL

R’L

Min in R

Page 17: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

17

Delete Tree, example

6

3115

14

92

10

5 12

4 816

7

15

14

10

12

31

2

4 16

9

8

6

7

Case 3: delete a node that has nonempty left and right subtrees.

Case 3: delete a node that has nonempty left and right subtrees.

Page 18: 1 BST Trees 5 38 149. 2 A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.

18

Binary Search Tree Binary search tree, using simple insert and

delete procedures the tree is nearly balance

add - fast O(log n) delete a target - fast O(log n) search - fast O(log n)

the tree is highly unbalance, it becomes a singly linked list (the worst case) add, delete and search - slow O(n)

effectively using sequential search to find a location