• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Bst
 

Bst

on

  • 127 views

 

Statistics

Views

Total Views
127
Views on SlideShare
127
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Bst Bst Presentation Transcript

    • 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?
    • 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)
    • 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)
    • 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.
    • 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].
    • 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
    • 15 2010 2 12 18 252 12 18 25 281411 23
    • 23 3018 10 20 27 3210 20 27 32 50156
    • 23 3018 10 20 27 3210 20 27 32 50196 Violating the condition for 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
    • search(12) 15 2010 2 12 18 25 15 10 122 12 18 25 281411 23 12 t
    • search(23) 15 2010 2 12 18 25 15 20 252 12 18 25 281411 23 t 25 23
    • 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; }
    • search(13) 15 2010 2 12 18 25 15 10 122 12 18 25 281411 23 12 t 14 
    • Tree-Minimum TreeNode *Tree_Minimum() { TreeNode * x=root; While (x->left != null)While (x->left != null) x= x->left; return x }
    • Tree Maximum TreeNode *Tree_Maximum() { TreeNode* x=root; While (x->right != null)While (x->right != null) x= x->right; return x }
    • 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
    • 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
    • 15 2010 2 12 18 25 insert(13)insert(19)insert(4) 2 12 18 25 281411 23
    • 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; } }
    • insert(13) 15 2010 2 12 18 25 15 10 122 12 18 25 281411 23 12 14 13  
    • 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
    • delete(2) 15 2010 2 12 18 25 delete(14)delete(12) 2 12 18 25 281411 23 13
    • 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.
    • 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
    • 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
    • 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
    • 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
    • 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
    • Time Complexity 15 2010 2 12 18 252 12 18 25 281411 23 Search Insert Delete
    • 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
    • Height Balanced Trees k = log (n)k = log (n)
    • 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