Upcoming SlideShare
×

# Bst

423 views
254 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
423
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
15
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Bst

1. 1. 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?
2. 2. 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)
3. 3. 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)
4. 4. 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.
5. 5. 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].
6. 6. 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
7. 7. 15 2010 2 12 18 252 12 18 25 281411 23
8. 8. 23 3018 10 20 27 3210 20 27 32 50156
9. 9. 23 3018 10 20 27 3210 20 27 32 50196 Violating the condition for BST
10. 10. 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
11. 11. search(12) 15 2010 2 12 18 25 15 10 122 12 18 25 281411 23 12 t
12. 12. search(23) 15 2010 2 12 18 25 15 20 252 12 18 25 281411 23 t 25 23
13. 13. Binary Search Tree Search 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; }
14. 14. search(13) 15 2010 2 12 18 25 15 10 122 12 18 25 281411 23 12 t 14 
15. 15. Tree-Minimum TreeNode *Tree_Minimum() { TreeNode * x=root; While (x->left != null)While (x->left != null) x= x->left; return x }
16. 16. Tree Maximum TreeNode *Tree_Maximum() { TreeNode* x=root; While (x->right != null)While (x->right != null) x= x->right; return x }
17. 17. 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 is the rightmost node in its left subtree: The successor of x is the leftmost node in its right subtree
18. 18. BST Operations Preview 25 10 37 15 30 65 Insert 8 10 25 158 37 30 6515 30 65 Delete 25 158 30 65 The successor of 25 65 3710 30 15
19. 19. 15 2010 2 12 18 25 insert(13)insert(19)insert(4) 2 12 18 25 281411 23
20. 20. Binary Search Tree – Insert void 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; } }
21. 21. insert(13) 15 2010 2 12 18 25 15 10 122 12 18 25 281411 23 12 14 13  
22. 22. Insertion Example: insert z = 32 into the BST below 25 20 35 x Compare 32 and 25 traverse the right subtree 40 20 12 35 Compare 32 and 35, traverse the left subtree insert 32 as left child 40 35 12 20 25 x y 35 12 20 25 4032 y z
23. 23. delete(2) 15 2010 2 12 18 25 delete(14)delete(12) 2 12 18 25 281411 23 13
24. 24. Delete a node from a BST 1. Locate the desired node by search; call it t 2. 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.
25. 25. Deletion (A) Case A: node x (to be deleted) is a leaf node Operation: update the parent node to have an empty subtree. 25 15 40 20 17 4530 453020 15 40 25 x
26. 26. Deletion (B) Case B: node x has a left child but no right child Operation: attach the left subtree of x to the parent 25 15 40 20 17 4530 453017 15 40 25 x
27. 27. Deletion (C) Case C: node x has a right child but no left child Operation: attach the right subtree of x to the parent 25 15 40 20 17 4530 453017 20 40 25 x
28. 28. 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 value greater 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 C D r x y zB z A y C D r B
29. 29. 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 replacement of 30 34 33 35 25 10 28 65 34 50
30. 30. Time Complexity 15 2010 2 12 18 252 12 18 25 281411 23 Search Insert Delete
31. 31. 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
32. 32. Height Balanced Trees k = log (n)k = log (n)
33. 33. 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