ceiling function for binary search tree

Similar documents
Binary Search Trees (BST)

Ordered Lists and Binary Trees

Data Structures and Algorithms

Binary Search Trees CMPSC 122

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

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

DATA STRUCTURES USING C

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

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:

Chapter 14 The Binary Search Tree

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

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

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

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

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

Converting a Number from Decimal to Binary

Data Structure with C

Data Structure [Question Bank]

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

Binary Trees and Huffman Encoding Binary Search Trees

Algorithms and Data Structures Written Exam Proposed SOLUTION

TREE BASIC TERMINOLOGIES

OPTIMAL BINARY SEARCH TREES

Data Structures. Level 6 C Module Descriptor

Analysis of Algorithms I: Binary Search Trees

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

Parallelization: Binary Tree Traversal

Lecture Notes on Binary Search Trees

Sample Questions Csci 1112 A. Bellaachia

Alex. Adam Agnes Allen Arthur

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

Graduate Assessment Test (Sample)

Lecture Notes on Binary Search Trees

Algorithms Chapter 12 Binary Search Trees

10CS35: Data Structures Using C

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Binary Heaps. CSE 373 Data Structures

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

PES Institute of Technology-BSC QUESTION BANK

Analysis of Algorithms I: Optimal Binary Search Trees

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

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

Exam study sheet for CS2711. List of topics

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

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

Binary Heap Algorithms

Binary Search Trees 3/20/14

The ADT Binary Search Tree

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

CSI33 Data Structures

Java Software Structures

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)

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

Any two nodes which are connected by an edge in a graph are called adjacent node.

Data Structures Using C++

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

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

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Lecture 6: Trees, Binary Trees and Binary Search Trees (BST)

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

Binary Search Trees. Ric Glassey

2. FINDING A SOLUTION

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

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.

Cours de C++ Utilisations des conteneurs

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

Iterator Pattern. CSIE Department, NTUT Chien-Hung Liu. Provide a way to access the elements of an aggregate object sequentially

Object Oriented Software Design

Introduction to data structures

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

From Last Time: Remove (Delete) Operation

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

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

Zabin Visram Room CS115 CS126 Searching. Binary Search

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

recursion, O(n), linked lists 6/14

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

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

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

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

Big Data and Scripting. Part 4: Memory Hierarchies

Data Structures and Algorithms DSA. Annotated Reference with Examples

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Chapter 8: Binary Trees

Full and Complete Binary Trees

Cpt S 223. School of EECS, WSU

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

Union-Find Problem. Using Arrays And Chains

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

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

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

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

How To Teach C++ Data Structure Programming

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

Persistent Binary Search Trees

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

Transcription:

ceiling function for binary search tree Key ceiling(const Key& x) Returns the smallest key in the tree that is >= x Throws logic_error if all keys in the tree are < x

Example ceiling(45)? ceiling(15)? ceiling(5)?

Example 45 ceiling(45) =

Example 15 ceiling(15) =

Example 5 ceiling(5) = 10

Example 90 ceiling(90)?

Example ceiling(5) Search for 5 Save pointer to the smallest value >= 5 as you search

Example ceiling(5) Search for 5 Save pointer to the smallest value >= 5 as you search

Example ceiling(5) Search for 5 Save pointer to the smallest value >= 5 as you search

Example ceiling(5) Search for 5 Save pointer to the smallest value >= 5 as you search

Example 5 ceiling(5) = 10 Search for 5 Save pointer to the smallest value >= 5 as you search If not found, the last value saved is the ceiling

Example ceiling(35)? Search for 35 Save a pointer to the smallest value >= 35 as you search

Example ceiling(35)? Search for 35 Save a pointer to the smallest value >= 35 as you search

Example ceiling(35)? Search for 35 Save a pointer to the smallest value >= 35 as you search Don t save, it isn t >= 35

Example ceiling(35)? Search for 35 Save a pointer to the smallest value >= 35 as you search Don t save 30, it isn t >= 35

Example 35 ceiling(35) = 40 Search for 35 Save a pointer to the smallest value >= 35 as you search If not found, the last value saved is the ceiling.

private ceiling function Key ceiling(const Key& k, Node *saved, Node *rt) { } Called recursively. If k is < rt->key,?? if k is > rt->key?? if rt is NULL??

public ceiling function Key ceiling(const Key& k) { Node *p = ceiling(k, NULL, root); if (p == NULL) { throw logic_error( no ceiling if key is > all existing keys ); } return p->key; }

Iterator for binary search tree How can we implement the two iterator members of BST iterator beginkey() iterator endkey()

A print member function to print all the values in a binary search tree Write a recursive private print and a public print

Private print method void print(node *rt) { { if (rt == NULL) return; print(rt->left); cout << rt-> key << endl; print(rt->right); } 10 30

Public print method void print() { { print(root); } 10 30

preorder inorder postorder Binary Tree Traversals

Binary Tree Traversals preorder visit the root visit the left subtree (in preorder) visit the right subtree (in preorder)

Binary Tree Traversals inorder visit the left subtree (in inorder) visit the root visit the right subtree (in inorder)

Binary Tree Traversals postorder visit the left subtree (in postorder) visit the right subtree (in postorder) visit the root

Preorder Example List values in preorder: root, left, right, 40, 10 30

Inorder Example List values in inorder: left (in inorder), root, right (in inorder) First item: not the root. First do the left subtree in inorder: left, root, right but not 40, the root of left subtree 10 30

Inorder and binary search trees List values in inorder: 10 30 40 60 10 30 For a binary search tree, inorder lists the values in their sorted order. preorder and postorder do not.

Iterators! But the print member ONLY allows printing and prints ALL values. The iterator members let the application decide what to do with the values in the binary search tree and so is much more useful.

iterator member functions of BST version 1 class BST { Node *root; int sz; vector<e> v; // for iterator member function. public: typedef typename vector<e>::iterator iterator; iterator keysbegin() { v.clear(); visitnodes(root); return v.begin(); }

private visitnodes member template<typename Key, typename Value> class BST { Node *root; int sz; vector<key> v; // for iterator member function. void visitnodes(node *rt) { if (rt == NULL) return; visitnodes(rt->left); v.push_back(rt->key); visitnodes(rt->right); } public: };

Advantages/Disadvantages of version 1 Advantage: Easy to implement (no iterator class needed) Disadvantage: Can t have two or more iterators active for same BST (only one vector member)

Iterator class next Key Next key after 30? After 40? After?

Iterator class next Key If current Node is 30, we need to have saved some values that would be examined when searching for 30 that are > 30 What are they?