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



Similar documents
Ordered Lists and Binary Trees

Data Structures and Algorithms

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:

Binary Search Trees (BST)

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

Binary Heap Algorithms

Chapter 14 The Binary Search Tree

Binary Heaps. CSE 373 Data Structures

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

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

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

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

The ADT Binary Search Tree

Full and Complete Binary Trees

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Analysis of Algorithms I: Optimal Binary Search Trees

Lecture Notes on Binary Search Trees

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

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

Analysis of a Search Algorithm

From Last Time: Remove (Delete) Operation

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

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

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

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

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

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

Lecture Notes on Binary Search Trees

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

Data Structures and Algorithms Lists

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

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

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

Data Structures and Data Manipulation

Analysis of Algorithms I: Binary Search Trees

Big Data and Scripting. Part 4: Memory Hierarchies

CompSci-61B, Data Structures Final Exam

TREE BASIC TERMINOLOGIES

A Practical Introduction to Data Structures and Algorithm Analysis - JAVA Edition. slides derived from material by Clifford A.

Why Use Binary Trees?

Data Structures and Algorithms

Algorithms and Data Structures

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

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

- 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

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

LINKED DATA STRUCTURES

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Converting a Number from Decimal to Binary

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

Binary Search Trees 3/20/14

Binary Trees and Huffman Encoding Binary Search Trees

Sample Questions Csci 1112 A. Bellaachia

Binary Search Trees. Ric Glassey

Optimal Binary Search Trees Meet Object Oriented Programming

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

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

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

Review of Hashing: Integer Keys

Data Structures and Algorithms Written Examination

ADTs,, Arrays, Linked Lists

Alex. Adam Agnes Allen Arthur

Efficient representation of integer sets

Binary Search Trees CMPSC 122

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

Analysis of Binary Search algorithm and Selection Sort algorithm

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

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

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

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

Data Structure with C

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

Symbol Tables. Introduction

Algorithms Chapter 12 Binary Search Trees

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Binary search algorithm

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Unit Storage Structures 1. Storage Structures. Unit 4.3

Chapter 8: Binary Trees

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

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

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

Pseudo code Tutorial and Exercises Teacher s Version

Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4)

DATA STRUCTURES USING C

6.090, IAP 2005 Lecture 8

COMPUTER SCIENCE. Paper 1 (THEORY)

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Abstraction and Abstract Data Types

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

STACKS,QUEUES, AND LINKED LISTS

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

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

6 March Array Implementation of Binary Trees

Data Structures and Algorithms(5)

Persistent Binary Search Trees

Transcription:

Binary Search Trees Last lecture: Tree terminology Kinds of binary trees Size and depth of trees This time: binary search tree ADT Java implementation Keys and records So far most examples assumed that we sort, insert, delete and search for integers. In fact in most applications we manipulate more complicated items. In general, assume that we manipulate records which have keys which can be compared, searched for etc. Record: employee s name, address, telephone, position, NI number, payroll number. Key: payroll number. Instead of sorting an array of integers in ascending order, could sort an array of records in the ascending order of keys. Binary search for a record given a key, etc. Data structures for storing data How easy it is to insert a new record How easy it is to remove a record How easy it is to find a record given a key Motivation Binary Search Trees Binary search on ordered arrays is efficient: O(log N) However insertion of an item in an ordered array is slow: O(N) Ordered arrays are best suited for static searching, where search space does not change. Binary search trees can be used for efficient dynamic searching. Binary trees Store data sorted in order of keys: all values stored in the left subtree of a node with key value K have key values < K, those in the right subtree have values >= K. 8 1

BST Search Searching for a record with key K in a tree: if tree is empty return null else check if K = key of the root (if yes return the found record) if the K < key of the root, search the left subtree otherwise search the right subtree 8 Searching for a record with key 7 8 Searching for a record with key 7 Searching for a record with key 7 Item not found. 8 8 Binary Search Tree ADT Logical domain: ordered binary trees which hold items of the form (key,record). Selected methods: BST() : create an empty binary search tree void insert(item) : insert item in BST, in key order void remove(key) : remove item with the given key item find(key): find and return item with the given key BST Insertion Inserting an item with key K (not checking for duplicates) in a tree: If tree is empty, make this item the root Else compare K to the key of the root, R If K < R: insert in the left subtree If K >= R: insert in the right subtree

Inserting a record with key 15: Inserting a record with key 15: Inserting a record with key 15: Inserting a record with key 15: 15 BST Deletion 1 First, find the item If it is a leaf, just delete it If it is not a leaf: if it has just one daughter, replace it by that daughter if it has two daughters, replace it by the leftmost node of its right subtree leaf () 8 3

1 1 leaf () leaf () 8 8 1 1 leaf () leaf () Remove it 8 8 More difficult case: delete 10 More difficult case: delete 10 8 8 4

3 More difficult case: delete 10 Replace it by its daughter () Last case: remove 8 8 3 3 Last case: remove Last case: remove s replacement (leftmost node in the right subtree: next larger key value) 8 8 3 Exercise Last case: remove s replacement (leftmost node in the right subtree: next larger key value) Replace 8 Remove 40 0 10 40 50 60 5

Exercise Exercise Remove 40 10 0 40 Remove 40 s replacement (leftmost node in the right subtree: next larger key value) 10 0 40 50 50 60 60 Exercise Java Implementation Remove 40 s replacement (leftmost node in the right subtree: next larger key value) Replace 0 10 50 60 Similar to linked lists: Define tree items (should have key() method) Define a tree node class (should have value and links to left and right daughters) Define a tree class which uses it (keeps the root value and has insert(), remove() and find() methods) Implementation taken from Shaffer s book. BinNode interface interface BinNode { public Object element(); public Object setelement(object v); public BinNode left(); public BinNode setleft(binnode p); public BinNode right(); public BinNode setright(binnode p); public boolean isleaf(); BinNodePtr class class BinNodePtr implements BinNode { private Object element; private BinNode left; private BinNode right; public BinNodePtr(){; public BinNodePtr(Object val){ left=right=null; element = val; 6

BinNodePtr class public Object element(){ return element; public Object setelement(object v){ element = v; return v; BinNodePtr class BinNode left(){ return left; public BinNode setleft(binnode p){ left = p; return p; BinNodePtr class public boolean isleaf() { return((left==null)&&(right==null)); Items in the search tree Assume that items implement the following interface (have integer keys): interface Elem { public int key(); class BST { private BinNode root; public BST(){ public Elem find(int key){ return findhelp(root, key); private Elem findhelp(binnode r, int k){ if (r == null) return null; Elem it = (Elem)r.element(); if (it.key() > k) { return findhelp(r.left(), k); else { if (it.key()==k) return it; else return findhelp(r.right(),k); 7

public void insert(elem val){ root = inserthelp(root, val); private BinNode inserthelp(binnode r,. Elem v){ if (r==null) return new BinNodePtr(v); Elem it = (Elem)r.element(); if (it.key() > v.key()){ r.setleft(inserthelp(r.left(),v)); else { r.setright(inserthelp(r.right(),v); return r; public void remove(int key){ root = removehelp(root, key); private BinNode removehelp(binnode r, int k){ if (r==null) return null; Elem it = (Elem)r.element(); if (k < it.key()){ setleft(removehelp(r.left(),k)); else if (k > it.key()){ setright(removehelp(r.right(),k)); else { // Found the node to be deleted if (r.left()==null) r = r.right(); else if (r.right()==null) r=r.left(); else { // the node has two children Elem temp = getmin(r.right()); r.setelement(temp); r.setright(deletemin(r.right())); return r; private Elem getmin(binnode r){ if (r.left() == null){ return(elem)r.element(); else { return getmin(r.left()); 8

private BinNode deletemin(binnode r){ if (r.left() == null){ return r.right(); else { r.setleft(deletemin(r.left()); return r; Reading Shaffer, Chapter 5 (5.1, 5., 5.3, 5.5) 9