Bst

33
Search Sorted Array: Binary Search Linked List: Linear Search Can we Apply the Binary Search O(log N) O(N) algorithm on a linked list? Why not?

Transcript of Bst

Page 1: Bst

Search

• Sorted Array: Binary Search • Linked List: Linear Search • Can we Apply the Binary Search

• O(log N) • O(N)

• Can we Apply the Binary Search algorithm on a linked list?

• Why not?

Page 2: Bst

Sorted Array

• Rigid Structure– Fixed Size– Need to know the size of the largest data set– Need to know the size of the largest data set– Wastage

• Search: O (log n)• Insertion: O (n)• Deletion: O (n)

Page 3: Bst

Binary Search Tree

• Binary Tree• Dynamic Structure (size is flexible) • Data is stored in a sorted fashion• Data is stored in a sorted fashion• A special class of BST has the following

properties:– Search: O (log n)– Insertion: O (log n)– Deletion: O (log n)

Page 4: Bst

Binary Search Tree (BST)

1. Data value in the root node is greater

• A BST is a binary tree with the following properties:

2. Both the left subtree and right subtree are BSTs.

1. Data value in the root node is greater than all the data values stored in the left subtree and is less than or equal to all the values stored in the right subtree.

Page 5: Bst

Binary Search Trees

Storage of elements for efficient access.

The binary-search-tree property:

Supporting dynamic set operations.

The binary-search-tree property:

If node y in left subtree of node x, then key[y] < key[x].

If node y in right subtree of node x, then key[y] ≥ key[x].

Page 6: Bst

A BST Example

50

30 55

25 35

10

20

31 37

53 60

62

Search path for 37

Search time O(h) where h is tree height

Page 7: Bst

15

2010

2 12 18 252 12 18 25

281411 23

Page 8: Bst

23

3018

10 20 27 3210 20 27 32

50156

Page 9: Bst

23

3018

10 20 27 3210 20 27 32

50196

Violating the condition for BST

Page 10: Bst

Inorder Traversal of BST

50

30 55

25 35

10

20

31 37

53 60

62

Prints out keys in sorted order: 10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62

Page 11: Bst

search(12)

15

2010

2 12 18 25

15

10

122 12 18 25

281411 23

12

t

Page 12: Bst

search(23)

15

2010

2 12 18 25

15

20

252 12 18 25

281411 23

t

25

23

Page 13: Bst

Binary Search TreeSearch

TreeNode * BinaryTree::search(int key)

{

TreeNode *t = root;

while (t) {

if (t->data == key) break;if (t->data == key) break;

else if (t->data > key) t = t->left;

else t = t->right;

}

return t;

}

Page 14: Bst

search(13)

15

2010

2 12 18 25

15

10

122 12 18 25

281411 23

12

t

14

Page 15: Bst

Tree-Minimum

TreeNode *Tree_Minimum()

{ TreeNode * x=root;

While (x->left != null)While (x->left != null)

x= x->left;

return x

}

Page 16: Bst

Tree Maximum

TreeNode *Tree_Maximum()

{ TreeNode* x=root;

While (x->right != null)While (x->right != null)

x= x->right;

return x

}

Page 17: Bst

Successor and Predecessor

50

30 55

x

10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62

25 35

10

20

31 37

53 60

62

The predecessor of x isthe rightmost node inits left subtree:

The successor of xis the leftmost nodein its right subtree

Page 18: Bst

BST Operations Preview

25

10 37

15 30 65

Insert 810

25

158

37

30 6515 30 65

Delete 25

158 30 65

The successor of 25

65

3710

30

15

Page 19: Bst

15

2010

2 12 18 25

insert(13)insert(19)insert(4)

2 12 18 25

281411 23

Page 20: Bst

Binary Search Tree – Insertvoid BinaryTree::insert(int data){

TreeNode *t1, *t2, *t3;t1 = new TreeNode;t1->left = NULL; t1->right = NULL;t2 = root;if (!root) root = t1;else {else {

while (t2) {t3 = t2; if (t2->data > key) t2 = t2->left;else t2 = t2->right;

}if (t2->data > key) t3->left = t1;else t3->right = t1;

}}

Page 21: Bst

insert(13)

15

2010

2 12 18 25

15

10

122 12 18 25

281411 23

12

14

13

Page 22: Bst

Insertion

Example: insert z = 32 into the BST below

25

20 35

xCompare 32 and 25traverse the right subtree

40

20

12

35

Compare 32and 35, traverse theleft subtree

insert 32 as left child

40

35

12

20

25

x

y

35

12

20

25

4032

y

z

Page 23: Bst

delete(2)15

2010

2 12 18 25

delete(14)delete(12)

2 12 18 25

281411 23

13

Page 24: Bst

Delete a node from a BST

1. Locate the desired node by search; call it t2. If t is a leaf, disconnect it from its parent

and set the pointer in the parent node equal to NULLto NULL

3. If it has only one child then remove t from the tree by making t’s parent point to its child.

4. Otherwise, find the largest/smallest among t’s LST/RST; call it p. Copy p’s information into t. Delete p.

Page 25: Bst

Deletion (A)

Case A: node x (to be deleted) is a leaf nodeOperation: update the parent node to have an empty subtree.

25

15 40

20

17

4530 453020

15 40

25

x

Page 26: Bst

Deletion (B)

Case B: node x has a left child but no right childOperation: attach the left subtree of x to the parent

25

15 40

20

17

4530 453017

15 40

25

x

Page 27: Bst

Deletion (C)

Case C: node x has a right child but no left childOperation: attach the right subtree of x to the parent

25

15 40

20

17

4530 453017

20 40

25

x

Page 28: Bst

Deletion (D)

Case D: node x has two children

Operation: 1. Select as the replacement (of x) node y, the successor of x. y has the smallest valuegreater than that of x.

2. Unlink y from the tree.3. Connect y’s right subtree to its parent. 4. Finally, connect y at the deleted node. 4. Finally, connect y at the deleted node.

A

CD

r

x

y

zB zA

y

C D

r

B

Page 29: Bst

Deletion (D) - an Example

40

30

40

33

10, 25, 28, 30, 33, 34, 35, 40, 50, 65 10, 25, 28, 33, 34, 35, 40, 50, 65

30

25

10 28

35

33

65

50

replacementof 30 34

33

3525

10 28

65

3450

Page 30: Bst

Time Complexity

15

2010

2 12 18 252 12 18 25

281411 23

Search

Insert

Delete

Page 31: Bst

15

18

20

26

insert(15)

insert(18)

insert(20)

insert(26)

insert(27)

insert(34)

27

34

insert(34)

Time Complexity

O(k) where k is the height

Page 32: Bst

Height Balanced Treesk = log (n)k = log (n)

Page 33: Bst

Running Time

All basic operations on a binary search tree of height h run in O(h) time.

Main question: How to guarantee h = O(lg n)?

One answer: red-black trees and AVL