BST Search To find a value in a BST search from the root node: If the target is less than the value in the node search its left subtree If the target is.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

Chapter 12 Binary Search Trees
Binary Search Tree Smt Genap
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
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.
CSC 205 – Java Programming II Lecture 35 April 17, 2002.
Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
Binary Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 Trees.
Department of Computer Science University of Maryland, College Park
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
Trees part 2.. Full Binary Trees A BC GDE F Full binary tree A binary tree is full if all the internal nodes (nodes other than leaves) has two children.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Marc Smith and Jim Ten Eyck
Deletion algorithm – Phase 2: Remove node or replace its with successor TreeNode deleteNode(TreeNode n) { // Returns a reference to a node which replaced.
Binary Search Trees Chapter 7 Objectives
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.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
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.
COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
CSC 205 Java Programming II Lecture 28 BST Implementation.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Binary Search Trees (BST)
Binary Search Trees … From
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Binary Search Trees ©Robert E. Tarjan Dictionary: contains a set S of items, each with associated information. Operations: Access(x): Determine.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
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 Search Trees Chapter 7 Objectives
AA Trees.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Multiway search trees and the (2,4)-tree
Chapter 10.1 Binary Search Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Search Sorted Array: Binary Search Linked List: Linear Search
CSC 143 Binary Search Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
CSC 205 – Java Programming II
CSC 205 – Java Programming II
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
COSC2007 Data Structures II
Presentation transcript:

BST Search To find a value in a BST search from the root node: If the target is less than the value in the node search its left subtree If the target is greater than the value in the node search its right subtree Otherwise return data, etc. If null value is reached, return null (“not found”). How many comparisons? One for each node on the path Worst case: height of the tree

BST Search Example click on a node to show its value

Search algorithm (recursive) T retrieveItem(TreeNode n, long searchKey) // returns a node containing the item with the key searchKey // or null if not found { if (n == null) { return null; } else { if (searchKey == n.getItem().getKey()) { // item is in the root of some subtree return n.getItem(); } else if (searchKey < n.getItem().getKey()) { // search the left subtree return retrieveItem(n.getLeft(), searchKey); } else { // search the right subtree return retrieveItem(n.getRight(), searchKey); } // end if } // end retrieveItem

BST Insertion The BST property must hold after insertion Therefore the new node must be inserted in the correct position This position is found by performing a search If the search ends at the (null) left child of a node make its left child refer to the new node If the search ends at the (null) right child of a node make its right child refer to the new node The cost is about the same as the cost for the search algorithm, O(height)

BST Insertion Example insert 43 create new node find position insert new node 43

Insertion algorithm (recursive) TreeNode insertItem(TreeNode n, T newItem) // returns a reference to the new root of the subtree rooted in n { TreeNode newSubtree; if (n == null) { // position of insertion found; insert after leaf // create a new node n = new TreeNode (newItem, null, null); return n; } // end if // search for the insertion position if (newItem.getKey() < n.getItem().getKey()) { // search the left subtree newSubtree = insertItem(n.getLeft(), newItem); n.setLeft(newSubtree); return n; } else { // search the right subtree newSubtree = insertItem(n.getRight(), newItem); n.setRight(newSubtree); return n; } // end if } // end insertItem

BST Deletion After deleting a node the BST property must still hold Deletion is not as straightforward as search or insertion So much so that sometimes it is not even implemented! There are a number of different cases that have to be considered

BST Deletion Cases The node to be deleted has no children Remove it (assign null to its parent’s reference) The node to be deleted has one child Replace the node with its subtree The node to be deleted has two children Replace the node with its predecessor = the right most node of its left subtree (or with its successor, the left most node of its right subtree) If that node has a child (and it can have at most one child) attach that to the node’s parent

BST Deletion – target is a leaf delete 30

BST Deletion – target has one child delete 79 replace with subtree

BST Deletion – target has one child delete 79 after deletion

BST Deletion – target has 2 children delete 32 temp find successor and detach

BST Deletion – target has 2 children delete temp find successor attach target node’s children to successor

BST Deletion – target has 2 children delete temp find successor attach target node’s children to successor make successor child of target’s parent

BST Deletion – target has 2 children delete temp note: successor had no subtree

BST Deletion – target has 2 children delete 63 temp find predecessor - note it has a subtree Note: predecessor used instead of successor to show its location - an implementation would have to pick one or the other

BST Deletion – target has 2 children delete 63 temp find predecessor attach predecessor’s subtree to its parent

BST Deletion – target has 2 children delete temp find predecessor attach subtree attach target’s children to predecessor

BST Deletion – target has 2 children delete temp find predecessor attach subtree attach children attach predecssor to target’s parent

BST Deletion – target has 2 children delete 63 59

Deletion algorithm – Phase 1: Finding Node TreeNode deleteItem(TreeNode n, long searchKey) { // Returns a reference to the new root. // Calls: deleteNode. TreeNode newSubtree; if (n == null) { throw new TreeException("TreeException: Item not found"); } else { if (searchKey==n.getItem().getKey()) { // item is in the root of some subtree n = deleteNode(n); // delete the node n } // else search for the item else if (searchKey<n.getItem().getKey()) { // search the left subtree newSubtree = deleteItem(n.getLeft(), searchKey); n.setLeft(newSubtree); } else { // search the right subtree newSubtree = deleteItem(n.getRight(), searchKey); n.setRight(newSubtree); } // end if return n; } // end deleteItem