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
Binary Search Tree Smt Genap
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
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 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
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.
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
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.
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.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary 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.
10 Binary-Search-Tree Data Structure  Binary-trees and binary-search-trees  Searching  Insertion  Deletion  Traversal  Implementation of sets using.
David Stotts Computer Science Department UNC Chapel Hill.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
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.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
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.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Trees and Binary Search Trees
BST Trees
Binary search tree. Removing a node
AVL Search Trees Introduction What is an AVL Tree?
Binary Search Trees.
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees What is a Tree? Tree terminology Why trees?
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 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 The BinarySearchTree class inherits the instance variables key, left, and right of the BinaryTree class: public class BinarySearchTree extends BinaryTree implements SearchableContainer { private BinarySearchTree getLeftBST(){ return (BinarySearchTree) getLeft( ) ; } private BinarySearchTree getRightBST( ){ return (BinarySearchTree) getRight( ) ; } //... }

5 Binary Search Tree Implementation (Cont’d) The find method of the BinarySearchTree class: public Comparable find(Comparable comparable) { if(isEmpty()) return null; Comparable key = (Comparable) getKey(); if(comparable.compareTo(key)==0) return key; else if (comparable.compareTo(key)<0) return getLeftBST().find(comparable); else return getRightBST().find(comparable); }

6 Binary Search Tree Implementation (Cont’d) 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. public Comparable findMin() { if(isEmpty()) return null; if(getLeftBST().isEmpty()) return (Comparable)getKey(); else return getLeftBST().findMin(); }

7 Binary Search Tree Implementation (Cont’d) 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 Comparable findMax() { if(isEmpty()) return null; if(getRightBST().isEmpty()) return (Comparable)getKey(); else return getRightBST().findMax(); }

8 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: public void attachKey(Object obj) { if(!isEmpty()) throw new InvalidOperationException(); else { key = obj; left = new BinarySearchTree(); right = new BinarySearchTree(); }

9 Insertion in a BST public void insert(Comparable comparable){ if(isEmpty()) attachKey(comparable); else { Comparable key = (Comparable) getKey(); if(comparable.compareTo(key)==0) throw new IllegalArgumentException("duplicate key"); else if (comparable.compareTo(key)<0) getLeftBST().insert(comparable); else getRightBST().insert(comparable); }

10 Deletion in a BST There are three cases: 1.The node to be deleted is a leaf node. 2.The node to be deleted has one non-empty child. 3.The node to be deleted has two non-empty children. CASE 1: DELETING A LEAF NODE Convert the leaf node into an empty tree by using the detachKey method: // In Binary Tree class public Object detachKey( ){ if(! isLeaf( )) throw new InvalidOperationException( ) ; else { Object obj = key ; key = null ; left = null ; right = null ; return obj ; }

11 Deleting a leaf node (cont’d) Example: Delete 5 in the tree below: Delete

12 Deleting a one-child node CASE 2: THE NODE TO BE DELETED HAS ONE NON-EMPTY CHILD (a) The right subtree of the node x to be deleted is empty. Example: Delete target temp 6 target // Let target be a reference to the node x. BinarySearchTree temp = target.getLeftBST(); target.key = temp.key; target.left = temp.left; target.right = temp.right; temp = null;

13 Deleting a one-child node (cont’d) (b) The left subtree of the node x to be deleted is empty. Example: Delete target temp target 14 9 // Let target be a reference to the node x. BinarySearchTree temp = target.getRightBST(); target.key = temp.key; target.left = temp.left; target.right = temp.right; temp = null;

14 CASE 3: DELETING A NODE THAT HAS TWO NON-EMPTY CHILDREN DELETION BY COPYING: METHOD#1 Copy the minimum key in the right subtree of x to the node x, then delete the one-child or leaf-node with this minimum key. Example: Delete

15 DELETING A NODE THAT HAS TWO NON-EMPTY CHILDREN DELETION BY COPYING: METHOD#2 Copy the maximum key in the left subtree of x to the node x, then delete the one-child or leaf-node with this maximum key. Example: Delete

16 Two-child deletion method#1 code // find the minimum key in the right subtree of the target node Comparable min = target.getRightBST().findMin(); // copy the minimum value to the target target.key = min; // delete the one-child or leaf node having the min target.getRightBST().withdraw(min); All the different cases for deleting a node are handled in the withdraw (Comparable key) method of BinarySearchTree class