Binary Search Trees (BST)



Similar documents
Ordered Lists and Binary Trees

Data Structures and Algorithms

Chapter 14 The Binary Search Tree

Binary Search Trees CMPSC 122

TREE BASIC TERMINOLOGIES

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

Converting a Number from Decimal to Binary

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

Full and Complete Binary Trees

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

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

DATA STRUCTURES USING C

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) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

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

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

Binary Trees and Huffman Encoding Binary Search Trees

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

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

Data Structures. Level 6 C Module Descriptor

Data Structure [Question Bank]

The ADT Binary Search Tree

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

Analysis of Algorithms I: Optimal Binary Search Trees

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

Data Structure with C

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

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

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

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

Algorithms Chapter 12 Binary Search Trees

Binary Heap Algorithms

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

Lecture Notes on Binary Search Trees

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

Binary Heaps. CSE 373 Data Structures

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

Sample Questions Csci 1112 A. Bellaachia

From Last Time: Remove (Delete) Operation

Analysis of Algorithms I: Binary Search Trees

Lecture Notes on Binary Search Trees

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

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

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

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

Algorithms and Data Structures

Optimal Binary Search Trees Meet Object Oriented Programming

Alex. Adam Agnes Allen Arthur

OPTIMAL BINARY SEARCH TREES

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

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

Binary Search Trees 3/20/14

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Data Structures UNIT III. Model Question Answer

Binary Search Trees. Ric Glassey

Big Data and Scripting. Part 4: Memory Hierarchies

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

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

GRAPH THEORY LECTURE 4: TREES

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

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

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

Parallelization: Binary Tree Traversal

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

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

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

Graduate Assessment Test (Sample)

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

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

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

Chapter 8: Binary Trees

10CS35: Data Structures Using C

Data Structures Fibonacci Heaps, Amortized Analysis

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

Data Structures Using C++

Exam study sheet for CS2711. List of topics

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

PES Institute of Technology-BSC QUESTION BANK

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

Symbol Tables. Introduction

Java Software Structures

Data Structures and Algorithms(5)

M-way Trees and B-Trees

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

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

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

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

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

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

Binary Coded Web Access Pattern Tree in Education Domain

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

DATABASE DESIGN - 1DL400

Structural Design Patterns Used in Data Structures Implementation

A COMPARATIVE STUDY OF LINKED LIST SORTING ALGORITHMS

2. FINDING A SOLUTION

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

Transcription:

Binary Search Trees (BST) 1. Hierarchical data structure with a single reference to node 2. Each node has at most two child nodes (a left and a right child) 3. Nodes are organized by the Binary Search property: Every node is ordered by some key data field(s) For every node in the tree, its key is greater than its left child s key and less than its right child s key 25 15 10 22 4 12 18 24 50 35 70 31 44 66 90

Some BST Terminology 1. The Root node is the top node in the hierarchy 2. A Child node has exactly one Parent node, a Parent node has at most two child nodes, Sibling nodes share the same Parent node (ex. node 22 is a child of node 15) 3. A Leaf node has no child nodes, an Interior node has at least one child node (ex. 18 is a leaf node) 4. Every node in the BST is a Subtree of the BST ed at that node 25 subtree (a BST 15 50 w/ 50) 10 22 4 12 18 24 35 70 31 44 66 90

Implementing Binary Search Trees Self-referential class is used to build Binary Search Trees public class BSTNode { Comparable data; BSTNode left; BSTNode right; public BSTNode(Comparable d) { data = d; left = right = null; } left refers to the left child right refers to the left child data field refers to object that implements the Comparable interface, so that data fields can be compared to order nodes in the BST Single reference to the of the BST All BST nodes can be accessed through reference by recursively accessing left or right child nodes

Operations on BST Naturally recursive: Each node in the BST is itself a BST Some Operations: Create a BST Find node in BST using its key field Add a node to the BST Traverse the BST visit all the tree nodes in some order

Create a Binary Search Tree bst_node = null; // an empty BST = new BSTNode(new Integer(35)); // a BST w/1 node Root.setLeft(new BSTNode(new Integer(22)); // add left // child data 35 left right data 22 left right In this example, the data fields ref to Integer objects

Find a Node into the BST Use the search key to direct a recursive binary search for a matching node 1. Start at the node as current node 2. If the search key s value matches the current node s key then found a match 3. If search key s value is greater than current node s 1. If the current node has a right child, search right 2. Else, no matching node in the tree 4. If search key is less than the current node s 1. If the current node has a left child, search left 2. Else, no matching node in the tree

Example: search for 45 in the tree (key fields are show in node rather than in separate obj ref to by data field): 1. start at the, 45 is greater than 25, search in right subtree 2. 45 is less than 50, search in 50 s left subtree 3. 45 is greater than 35, search in 35 s right subtree 4. 45 is greater than 44, but 44 has no right subtree so 45 is not in the BST 25 (1) (2) 15 10 22 4 12 18 24 (3) 50 35 (4) 70 31 44 66 90

Insert Node into the BST Always insert new node as leaf node 2. Start at node as current node 3. If new node s key < current s key 1. If current node has a left child, search left 2. Else add new node as current s left child 4. If new node s key > current s key 1. If current node has a right child, search right 2. Else add new node as current s right child

Example: insert 60 in the tree: 1. start at the, 60 is greater than 25, search in right subtree 2. 60 is greater than 50, search in 50 s right subtree 3. 60 is less than 70, search in 70 s left subtree 4. 60 is less than 66, add 60 as 66 s left child 15 (1) 25 50 (2) (3) 10 22 4 12 18 24 35 (4) 70 31 44 66 90 60

Traversals Visit every node in the tree and perform some operation on it (ex) print out the data fields of each node Three steps to a traversal 1. Visit the current node 2. Traverse its left subtree 3. Traverse its right subtree The order in which you perform these three steps results in different traversal orders: Pre-order traversal: (1) (2) (3) In-order traversal: (2) (1) (3) Post-order traversal: (2) (3) (1)

Traversal Code public void InOrder(bst_node ) { // stop the recursion: if( == null) { return; } // recursively traverse left subtree: InOrder(.leftChild()); // visit the current node: Visit(); // recursively traverse right subtree: InOrder(.rightChild()); }

// in main: a call to InOrder passing InOrder(); // The call stack after the first few // calls to InOrder(.leftChild()): Call Stack (drawn upside down): main: calls InOrder: InOrder: 15 25 InOrder: 10 22 InOrder: 4 12 18 24

Traversal Examples InOrder() visits nodes in the following order: 4, 10, 12, 15, 18, 22, 24, 25, 31, 35, 44, 50, 66, 70, 90 A Pre-order traversal visits nodes in the following order: 25, 15, 10, 4, 12, 22, 18, 24, 50, 35, 31, 44, 70, 66, 90 A Post-order traversal visits nodes in the following order: 4, 12, 10, 18, 24, 22, 15, 31, 44, 35, 66, 90, 70, 50, 25 25 15 10 22 4 12 18 24 50 35 70 31 44 66 90