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



Similar documents
Binary Search Trees (BST)

Chapter 14 The Binary Search Tree

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

Ordered Lists and Binary Trees

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:

Data Structures and Algorithms

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

Analysis of Algorithms I: Binary Search Trees

The ADT Binary Search Tree

Lecture Notes on Binary Search Trees

Binary Heap Algorithms

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

Alex. Adam Agnes Allen Arthur

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:

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Algorithms and Data Structures

Lecture Notes on Binary Search Trees

Algorithms Chapter 12 Binary Search Trees

Sample Questions Csci 1112 A. Bellaachia

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

Optimal Binary Search Trees Meet Object Oriented Programming

Analysis of Algorithms I: Optimal Binary Search Trees

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

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

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

From Last Time: Remove (Delete) Operation

TREE BASIC TERMINOLOGIES

Data Structures Fibonacci Heaps, Amortized Analysis

Stacks. Linear data structures

Big Data and Scripting. Part 4: Memory Hierarchies

Lecture Notes on Linear Search

Data Structure with C

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

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

Analysis of a Search Algorithm

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

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

Full and Complete Binary Trees

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

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

DATA STRUCTURE - STACK

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

CmpSci 187: Programming with Data Structures Spring 2015

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

Data Structures and Algorithms Written Examination

CompSci-61B, Data Structures Final Exam

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

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

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

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

Rotation Operation for Binary Search Trees Idea:

Converting a Number from Decimal to Binary

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

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

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

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)!

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

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

Introduction to Object-Oriented Programming

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

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Binary Heaps. CSE 373 Data Structures

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

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Data Structures. Level 6 C Module Descriptor

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

Optimizing Generation of Object Graphs in Java PathFinder

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

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

Abstraction and Abstract Data Types

COMPUTER SCIENCE. Paper 1 (THEORY)

Data Structures and Data Manipulation

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

DATA STRUCTURES USING C

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

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

OPTIMAL BINARY SEARCH TREES

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

Introduction to Data Structures and Algorithms

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)

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

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

Why Use Binary Trees?

CS 2412 Data Structures. Chapter 2 Stacks and recursion

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

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

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

Binary search algorithm

Binary Search Trees. Ric Glassey

G. H. RAISONI COLLEGE OF ENGG NAGPUR-16 Session DEPARTMENT CSE Semester IV SUBJECT DSPD

Symbol Tables. Introduction

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

Transcription:

(1) Binary Search Tree ADT 56 26 200 18 28 190 213 12 24 27

(2) Binary Search Tree ADT (BST) It is a binary tree with the following properties 1. with each node associate a key 2. the key of each node is greater than the keys of the nodes in its left subtree and it is less that the keys of the nodes in its right subtree 56 26 200 18 28 190 213 12 24 27

Note: The property 2 is called binary-search-tree property This property guarantees that: - The minimum is located at the left-most node. - The maximum is located at the right-most node. (3) 56 26 200 18 28 190 213 right-most iht t left-most 12 24 27

Why BST? It speeds up search operation on a collection of data elements Specification of Binary Search Tree ADT Elements: Any valid data type Structure: A binary tree such that if N is any node in the tree then all nodes in its left subtree are smaller than N, and all nodes in its right subtree are larger than N. Note: A node Ni is larger than the node Mifk key value of fni is larger than that of M and vice versa. Domain: Number of elements is bounded Operations: Operation Specification void empty() Precondition: none. Process: returns true if the BST has no nodes. (4)

(5) Operation Specification void findkey (int k) Precondition: none. Process: searches the BST for a node whose key = k. If found then that node will be set as the current node and returns true. And if search failed, then (1) BST empty then just return false; or (2) BST not empty then current node is the node to which a node containing k would be attached as a child if it were added to BST and return false. void insert(intint k, Type val) Precondition: BST not full. Process: (1) if we already have a node with key = k then current retain its old value (the value prior to calling this operation) and return false; or (2) insert a new node with the given key and data and setting it as current node, return true. void update(type) Precondition/Requires : BST is not empty. Process: update the value of data of the current node, key remains unchanged. Type retrieve() Precondition: BST is not empty. Process: returns data of the current node.. bool remove_key(int k) Precondition: none. Process: removes the node containing the key = k, and in case BST is not empty py then sets the root as the current node. Returns true or false depending on whether the operation was successful or not.

Represent ion of Binary Search Tree ADT (6) Since BST is a special kind binary tree, it can be represented using - Linked List Note : Array is not suitable for BST

(7) Implementation of BST public class BSTNode <T> { public int key; public T data; public BSTNode<T> left, right; public BSTNode(int k, T val) { key = k; data = val; left = right = null; public BSTNode (int k, BSTNode<T> l, BSTNode<T> r) { key = k; left = l; right = r;

(8) Implementation of BST public class Flag { boolean value; /** Creates a new instance of Flag */ public Flag() { value = false; public Flag(boolean v){ value = v; public boolean get_value (){ return value; public void set_value(boolean v){ value = v;

(9) Implementation of BST public class BST <T> { BSTNode<T> root, current; public BST() private BSTNode<T> findparent (BSTNode<T> p) private BSTNode<T> find_min(bstnode<t> p) private BSTNode<T> remove_ aux(int key, BSTNode<T> p, Flag flag) public boolean empty() public T retrieve () public boolean findkey(int tkey) public boolean insert (int k, T val) public boolean remove_key (int tkey) public boolean update(int key, T data)

Implementation of Operations private BSTNode<T> findparent (BSTNode<T> p) { LinkStack<BSTNode<T>> stack = new LinkStack<BSTNode<T>>(); BSTNode<T> q = root; while (q.right!= p && q.left!= p){ if (q.right!= null) stack.push(q.right); return q; if (q.left!= null) q = q.left; else q = stack.pop(); private BSTNode<T> find_min(bstnode<t> p){ if (p == null) return null; while (p.left!= null){ p = p.left; return p; (10)

Implementation of Operations (11) private BSTNode<T> remove_aux(int key, BSTNode<T> p, Flag flag) { BSTNode<T> q, child = null; if (p == null) return null; if (key < p.key) p.left = remove_aux(key, p.left, flag); else if (key > p.key) p.right = remove_aux(key, p.right, flag); else { flag.set_value(true); if (p.left!= null && p.right!= null){ q = find_ min(p.right); p.key = q.key; p.data = q.data; p.right = remove_aux(q.key, p.right, flag); else { if (p.right == null) child = p.left; else if (p.left == null) child = p.right; return child; return p; Start with node p and remove the node whose key is key recursively.

Implementation of Operations (12) public BST() { root = current = null; public boolean empty() { return root == null? true: false; public T retrieve () { return current.data;

Implementation of Operations public boolean findkey(int tkey){ BSTNode<T> p,q; p = root; q = root; if (empty()) return false; while (p!= null){ q = p; if (p.key == tkey){ current = p; return true; else if (tkey < p.key) p = p.left; else p = p.right; current = q; return false; (13)

Implementation of Operations (14) public boolean insert (int k, T val){ BSTNode<T> p, q = current; if (findkey(k)){ current = q; return false; /* key already in the BST */ p = new BSTNode<T>(k, val); if (empty()) { root = current = p; return true; else { /* current is pointing to the parent of the new key. if (k < current.key) current.left = p; else current.right t = p; current = p; return true;

Implementation of Operations (15) public boolean remove_key (int tkey){ Flag removed = new Flag(false); BSTNode<T> p; p = remove_aux(tkey, root, removed); current = root = p; return removed.get_value(); value();