A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:



Similar documents
root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Algorithms and Data Structures Written Exam Proposed SOLUTION

Ordered Lists and Binary Trees

Data Structures and Algorithms

Binary Search Trees (BST)

Binary Heap Algorithms

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

TREE BASIC TERMINOLOGIES

Chapter 14 The Binary Search Tree

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Converting a Number from Decimal to Binary

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Symbol Tables. Introduction

Binary Heaps. CSE 373 Data Structures

Why Use Binary Trees?

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

The ADT Binary Search Tree

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Binary Trees and Huffman Encoding Binary Search Trees

CSE 326: Data Structures B-Trees and B+ Trees

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Analysis of Algorithms I: Binary Search Trees

Section IV.1: Recursive Algorithms and Recursion Trees

Lecture Notes on Binary Search Trees

Analysis of a Search Algorithm

Binary Search Trees CMPSC 122

Full and Complete Binary Trees

Sample Questions Csci 1112 A. Bellaachia

Persistent Binary Search Trees

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Algorithms Chapter 12 Binary Search Trees

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

From Last Time: Remove (Delete) Operation

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Lecture Notes on Binary Search Trees

Big Data and Scripting. Part 4: Memory Hierarchies

Algorithms and Data Structures

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

How To Create A Tree From A Tree In Runtime (For A Tree)

Data Structures and Data Manipulation

Analysis of Binary Search algorithm and Selection Sort algorithm

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

10CS35: Data Structures Using C

CompSci-61B, Data Structures Final Exam

Alex. Adam Agnes Allen Arthur

Analysis of Algorithms I: Optimal Binary Search Trees

Heaps & Priority Queues in the C++ STL 2-3 Trees

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

Binary Search Trees. Ric Glassey

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Cpt S 223. School of EECS, WSU

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Binary Search Trees 3/20/14

Optimal Binary Search Trees Meet Object Oriented Programming

A Comparison of Dictionary Implementations

PES Institute of Technology-BSC QUESTION BANK

Stacks. Linear data structures

COMPUTER SCIENCE. Paper 1 (THEORY)

Data Structure with C

Lecture Notes on Linear Search

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Iterators. Provides a way to access elements of an aggregate object sequentially without exposing its underlying representation.

Pseudo code Tutorial and Exercises Teacher s Version

Data Structures. Level 6 C Module Descriptor

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Data Structures CSC212 (1) Dr Muhammad Hussain Lecture - Binary Search Tree ADT

LINKED DATA STRUCTURES

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Data Structures Using C++ 2E. Chapter 5 Linked Lists

6 March Array Implementation of Binary Trees

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

Lecture 12 Doubly Linked Lists (with Recursion)

Physical Data Organization

DATABASE DESIGN - 1DL400

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Introduction to data structures

Chapter 13: Query Processing. Basic Steps in Query Processing

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Data Structures and Algorithms(5)

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Unit Storage Structures 1. Storage Structures. Unit 4.3

Transcription:

Binary Search Trees 1 The general binary tree shown in the previous chapter is not terribly useful in practice. The chief use of binary trees is for providing rapid access to data (indexing, if you will) and the general binary tree does not have good performance. Suppose that we wish to store data elements that contain a number of fields, and that one of those fields is distinguished as the key upon which searches will be performed. A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and: 1. All keys in the left subtree (if there is one) are less than the key in the root node. 2. All keys in the right subtree (if there is one) are greater than (or equal to)* the key in the root node. 3. The left and right subtrees of the root are binary search trees. * In many uses, duplicate values are not allowed.

BST Insertion 2 Here, the key values are characters (and only key values are shown). Inserting the following key values in the given order yields the given BST: D G H E B D F C D In a BST, insertion is always at the leaf level. Traverse the BST, comparing the new value to existing ones, until you find the right spot, then add a new leaf node holding that value. B C D E G F H What is the resulting tree if the (same) key values are inserted in the order: B C D D E F G H or E B C D D F G H

Searching in a BST 3 Because of the key ordering imposed by a BST, searching resembles the binary search algorithm on a sorted array, which is O(log N) for an array of N elements. A BST offers the advantage of purely dynamic storage, no wasted array cells and no shifting of the array tail on insertion and deletion. D B G A C E H Trace searching for the key value E.

BST Deletion 4 Deletion is perhaps the most complex operation on a BST, because the algorithm must result in a BST. The question is: what value should replace the one being deleted? As with the general tree, we have cases: - Removing a leaf node is trivial, just set the relevant child pointer in the parent node to NULL. - Removing an internal node which has only one subtree is also trivial, just set the relevant child pointer in the parent node to target the root of the subtree. D B G C E NULL H D F

BST Deletion 5 - Removing an internal node which has two subtrees is more complex Simply removing the node would disconnect the tree. But what value should replace the one in the targeted node? To preserve the BST property, we may take the smallest value from the right subtree, which would be the closest succcessor of the value being deleted. B C D E F G F H Fortunately, the smallest value will always lie in the left-most node of the subtree.

BST Deletion 6 So, we first find the left-most node of the right subtree, and then swap data values between it and the targeted node. B E G Note that at this point we don t necessarily have a BST. C F H Now we must delete the copied value from the right subtree. E F That looks straightforward here since the node in question is a leaf. However - the node will NOT be a leaf in all cases - the occurrence of duplicate values is a complicating factor - so we might want to have a helper function to clean up at this point

Deleting the Minimum Value 7 Suppose we want to delete the value E from the BST: E After swapping the F with the E, we must delete B G We must be careful to not confuse this with the other node containing an F. C F H F Also, consider deleting the value G. In this case, the right subtree is just a leaf node, whose parent is the node originally targeted for deletion. Moral: be careful to consider ALL cases when designing.

Generic BST Interface Here s a partial generic BST interface, adapted from Weiss: 8 public class BST extends Comparable<? super AnyType>> { private static class BinaryNode<AnyType> {... private BinaryNode<AnyType> root; public BST( ) {... public void clear( ) {... public boolean isempty( ) {... public boolean contains( AnyType x ) {... public AnyType find( AnyType x ) {... public AnyType findmin( ) {... public AnyType findmax( ) {... public void insert( AnyType x ) {... public void delete( AnyType x ) {... public void printtree( ) {......

Generic BST Interface Here s a partial generic BST interface, adapted from Weiss: 9 public class BST extends Comparable<? super AnyType>> {... public int compareto(object o) Returns: a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. See Weiss 1.5.5

Generic BST Node Here s a partial generic BST interface, adapted from Weiss: private static class BinaryNode<AnyType> { // Constructors BinaryNode( AnyType theelement ){ 10 this( theelement, null, null ); BinaryNode( AnyType theelement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt ){ element = theelement; left = lt; right = rt; AnyType element; // The data in the node BinaryNode<AnyType> left; // Left child BinaryNode<AnyType> right; // Right child

BST contains() Implementation 11 The BST contains() function takes advantage of the BST data organization: public boolean contains( AnyType x ) { return contains( x, root ); private boolean contains( AnyType x, BinaryNode<AnyType> t ) { if ( t == null ) return false; int compareresult = x.compareto( t.element ); if ( compareresult < 0 ) return contains( x, t.left ); else if ( compareresult > 0 ) return contains( x, t.right ); else return true; // Match Search direction is determined by relationship of target data to data in current node.

BST find() Implementation 12 The BST find() function provides client access to data objects within the tree: public AnyType find( AnyType x ) { return find( x, root ); private AnyType find( AnyType x, BinaryNode<AnyType> t ) { if ( t == null ) return null; int compareresult = x.compareto( t.element ); if ( compareresult < 0 ) return find( x, t.left ); else if ( compareresult > 0 ) return find( x, t.right ); else return t.element; // Match Warning: be sure you understand the potential dangers of supplying this function and the benefits of doing so

BST insert() Implementation 13 The public Insert() function is just a stub to call the recursive helper: public void insert( AnyType x ) { root = insert( x, root ); Warning: the BST definition in these notes does not allow for duplicate data values to occur, the logic of insertion may need to be changed for your specific application. The stub simply calls the helper function.. The helper function must find the appropriate place in the tree to place the new node. The design logic is straightforward: - locate the parent "node" of the new leaf, and - hang a new leaf off of it, on the correct side

BST insert() Helper The insert() helper function: 14 private BinaryNode<AnyType> insert( AnyType x, BinaryNode<AnyType> t ) { if ( t == null ) return new BinaryNode<AnyType>( x, null, null ); int compareresult = x.compareto( t.element ); if ( compareresult < 0 ) t.left = insert( x, t.left ); else if ( compareresult > 0 ) t.right = insert( x, t.right ); else ; // Duplicate; do nothing return t; When the parent of the new value is found, one more recursive call takes place, passing in a NULL pointer to the helper function. Note that the insert helper function must be able to modify the node pointer parameter, and that the search logic is precisely the same as for the find function.

BST delete() Implementation 15 The public delete() function is very similar to the insertion function: public void delete( AnyType x ) { root = delete( x, root ); The delete() helper function design is also relatively straightforward: - locate the parent of the node containing the target value - determine the deletion case (as described earlier) and handle it: - parent has only one subtree - parent has two subtrees The details of implementing the delete helper function are left to the reader

Parent Pointers 16 Some binary tree implementations employ parent pointers in the nodes. - increases memory cost of the tree (probably insignificantly) - increases complexity of insert/delete/copy logic (insignificantly) - provides some unnecessary alternatives when implementing insert/delete - may actually simplify the addition of iterators to the tree (later topic)

Some Refinements The given BST template may also provide additional features: - a function to provide the size of the tree - a function to provide the height of the tree - a function to display the tree in a useful manner 17 It is also useful to have some instrumentation during testing. For example: - log the values encountered and the directions taken during a search This is also easy to add, but it poses a problem since we generally do not want to see such output when the BST is used. I resolve this by adding some data members and mutators to the template that enable the client to optionally associate an output stream with the object, and to turn logging of its operation on and off as needed.