1 Binary Search Trees (BST) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from a.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
Treaps.  Good (logarithmic) performance with random data  Linear performance if the data is sorted ◦ Solutions – Splay Trees Amortized O( lg n) performance.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
AVL Search Trees Inserting in an AVL tree Insertion implementation Deleting from an AVL tree.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Trees, Binary Trees, and Binary Search Trees COMP171.
AVL Search Trees What is an AVL Tree? AVL Tree Implementation.
1 Trees Definition of a Tree Tree Terminology Importance of Trees Implementation of Trees Binary Trees –Definition –Full and Complete Binary Trees –Implementation.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
AVL Search Trees Inserting in an AVL tree Insertion implementation Deleting from an AVL tree.
1 Binary Search Trees (BST) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from a.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on MinHeap: Insert Delete Converting an array.
AVL Search Trees What is an AVL Tree? AVL Tree Implementation. Why AVL Trees? Rotations. Insertion into an AVL tree Deletion from an AVL tree.
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
04 Trees Part I CS 310 photo ©Oregon Scenics used with permissionOregon Scenics All figures labeled with “Figure X.Y” Copyright © 2006 Pearson Addison-Wesley.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
Marc Smith and Jim Ten Eyck
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 BST Trees 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.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Binary Search Trees (BSTs) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
CS 367 Introduction to Data Structures Lecture 8.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
BST Trees
Binary Search Tree Chapter 10.
Trees What is a Tree? Tree terminology Why trees?
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Binary Search Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Search Trees (BST)
Binary Search Trees A special case of a Binary Tree
Binary Trees, Binary Search Trees
AVL Search Trees Inserting in an AVL tree Insertion implementation
AVL Search Trees Inserting in an AVL tree Insertion implementation
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Trees, Binary Search Trees
AVL Search Trees What is an AVL Tree? AVL Tree Implementation.
AVL Search Trees Inserting in an AVL tree Insertion implementation
Presentation transcript:

1 Binary Search Trees (BST) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from a BST

2 Binary Search Trees (Definition) A binary search tree (BST) is a binary tree that is empty or that satisfies the BST ordering property: 1.The key of each node is greater than each key in the left subtree, if any, of the node. 2.The key of each node is less than each key in the right subtree, if any, of the node. Thus, each key in a BST is unique. Examples: AA B C D

3 Why BST DeletionInsertionRetrievalData Structure O(log n) FAST O(log n) FAST O(log n) FAST BST O(n) SLOW O(n) SLOW O(log n) FAST* Sorted Array O(n) SLOW O(n) SLOW O(n) SLOW Sorted Linked List BSTs provide good logarithmic time performance in the best and average cases. Average case complexities of using linear data structures compared to BSTs: *using binary search

4 Binary Search Tree Implementation Since search trees are designed to support efficient searching they implement the SearchableContainer interface. The class hierarchy of BSTs is MyComparable Container Tree AbstractObject AbstractContainer AbstractTree GeneralTree BinarySearchTree SearchableContainerSearchTree

5 Binary Search Tree Implementation (Contd.) The SearchTree interface is defined as: 1 public interface SearchTree extends Tree, SearchableContainer{ 2 public abstract MyComparable findMin( ) ; 3 public abstract MyComparable findMax( ) ; 4 } The BinarySearchTree class inherits the instance variables key, left, and right of the BinaryTree class: 1 public class BinarySearchTree extends BinaryTree implements SearchTree{ 2 private BinarySearchTree getLeftBST( ) 3 { return (BinarySearchTree) getLeft( ) ; } 4 5 private BinarySearchTree getRightBST( ) 6 { return (BinarySearchTree) getRight( ) ; } 7 //... 8 }

6 Binary Search Tree Implementation (Contd.) The find method of the BinarySearchTree class: 1 public MyComparable find(MyComparable comparable)\{ 2 if(isEmpty( )) 3 return null ; 4 MyComparable key = (MyComparable) getKey( ) ; 5 if(comparable.isEQ(key)) 6 return key ; 7 else if(comparable.isLT(key)) 8 return getLeftBST( ).find(comparable) ; 9 else 10return getRightBST( ).find(comparable) ; 11 }

7 Binary Search Tree Implementation (Contd.) The findMin method of the BinarySearchTree class: By the BST ordering property, the minimum key is the key of the left-most node that has an empty left-subtree. 1 public MyComparable findMin( ){ 2 if(isEmpty( )) 3 return null ; 4 if(getLeftBST( ).isEmpty( )) 5 return (MyComparable) getKey( ) ; 6 else 7 return getLeftBST( ).findMin( ) ; 8 }

8 Binary Search Tree Implementation (Contd.) The findMax method of the BinarySearchTree class: By the BST ordering property, the maximum key is the key of the right-most node that has an empty right-subtree public MyComparable findMax( ) { 2 if(isEmpty( )) 3 return null ; 4 if(getRightBST( ).isEmpty( )) 5 return (MyComparable) getKey( ) ; 6 else 7 return getRightBST( ).findMax( ) ; 8 }

9 Insertion in a BST By the BST ordering property, a new node is always inserted as a leaf node. The insert method, given in the next page, recursively finds an appropriate empty subtree to insert the new key. It then transforms this empty subtree into a leaf node by invoking the attachKey method: 1 public void attachKey(Object obj) 2 { 3 if(! isEmpty( )) 4 throw new InvalidOperationException( ) ; 5 else 6 { 7 key = obj ; 8 left = new BinarySearchTree( ) ; 9 right = new BinarySearchTree( ) ; 10 } 11 }

10 Insertion in a BST 1 public void insert (MyComparable comparable) { 2 if(isEmpty( )) 3 attachKey(comparable) ; 4 else 5 { 6 MyComparable key = (MyComparable) getKey( ) ; 7 if(comparable.isEQ(key)) 8 throw new IllegalArgumentException("duplicate key" ) ; 9 else if(comparable.isLT(key)) 10 getLeftBST( ).insert(comparable) ; 11 else 12 getRightBST( ).insert(comparable) ; 13 } 14 }

11 Deletion in BST There are three cases: 1.The right subtree of the node x to be deleted is empty. Make the reference pointing to x point to the left child of x, if any. After this, delete x. Example: Delete

12 Deletion in BST (Contd.) 2.The left subtree of the node x to be deleted is empty. Make the reference pointing to x point to the right child of x, if any. After this, delete x. Example: Delete Note: Deletion a leaf node is a special case of any of case1 & case2.

13 Deletion by copying 3.Both subtrees of the node x to be deleted are not empty. Four deletion methods are possible: (a) Copy the minimum key in the right subtree of x to the node x, then delete the node with this minimum key. Example: Delete

14 Deletion by copying (Contd.) (b) Copy the maximum key in the left subtree of x to the node x, then delete the node with this maximum key. Example: Delete

15 Deletion by Merging (c) Make the right subtree of x to be the right subtree of the rightmost node in the left subtree of x. After this, delete x. Example: The right subtree of node7 Delete is attached as the right subtrees of the rightmost node in the left subtree of node7

16 Deletion by Merging (Contd.) (c) Make the left subtree of x to be the left subtree of the leftmost node in the right subtree of x. After this, delete x. Example: Delete 7 The left subtree of node7 is attached as the left subtrees of the leftmost node in the right subtree of node

17 Deletion by Copying CODE Here is the deletion by Copying Code: 1 public void withdraw(MyComparable comparable){ 2 if(isEmpty( )) 3 throw new IllegalArgumentException("object notfound") ; 4MyComparable key = (MyComparable) getKey( ) ; 5 if(comparable.isLT(key)) 6 getLeftBST( ).withdraw(comparable) ; 7 else if(comparable.isGT(key)) 8 getRightBST( ).withdraw(comparable) ; 9 else{ // the key is found 10 if(isLeaf( )) detachKey( ) ; 11 else if( ! getLeftBST( ).isEmpty( )){ 12 MyComparable max = getLeftBST( ).findMax( ) ; 13 super.key = max ; 14 getLeftBST( ).withdraw(max) ; } 15 else { // the right subtree is not empty 16 MyComparable min = getRightBST( ).findMin( ) ; 17 super.key = min ; 18 getRightBST( ).withdraw(min) ; } 19 } 20 }