Binary Search Trees 3/20/14

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

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

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

Analysis of Algorithms I: 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

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

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

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

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

Binary Heaps. CSE 373 Data Structures

Ordered Lists and Binary Trees

Binary Search Trees. Ric Glassey

CS711008Z Algorithm Design and Analysis

Converting a Number from Decimal to Binary

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

6 March Array Implementation of Binary Trees

Binary Trees and Huffman Encoding Binary Search Trees

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

Algorithms Chapter 12 Binary Search Trees

Data Structures and Algorithms

From Last Time: Remove (Delete) Operation

OPTIMAL BINARY SEARCH TREES

Binary Search Trees (BST)

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

Binary Heap Algorithms

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

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

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

Chapter 14 The Binary Search Tree

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:

Big Data and Scripting. Part 4: Memory Hierarchies

Binary Search Trees CMPSC 122

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

Data Structure [Question Bank]

Introduction to Data Structures and Algorithms

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

Merkle Hash Trees for Distributed Audit Logs

Algorithms and Data Structures

Optimal Binary Search Trees Meet Object Oriented Programming

Rotation Operation for Binary Search Trees Idea:

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

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)

Full and Complete Binary Trees

DATABASE DESIGN - 1DL400

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

Algorithms and Data Structures Written Exam Proposed SOLUTION

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Exam study sheet for CS2711. List of topics

Persistent Data Structures and Planar Point Location

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

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

R-trees. R-Trees: A Dynamic Index Structure For Spatial Searching. R-Tree. Invariants

A Comparison of Dictionary Implementations

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

Data storage Tree indexes

Structure for String Keys

Data Structures. Level 6 C Module Descriptor

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

schema binary search tree schema binary search trees data structures and algorithms lecture 7 AVL-trees material

Symbol Tables. Introduction

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

Algorithms and Data Structures

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

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

1/1 7/4 2/2 12/7 10/30 12/25

Physical Data Organization

External Memory Geometric Data Structures

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

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

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

Zabin Visram Room CS115 CS126 Searching. Binary Search

Analysis of Algorithms I: Optimal Binary Search Trees

Alex. Adam Agnes Allen Arthur

Persistent Data Structures

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

The ADT Binary Search Tree

Lecture 4: Balanced Binary Search Trees

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

Classification/Decision Trees (II)

DATA STRUCTURES USING C

Why Use Binary Trees?

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

Cpt S 223. School of EECS, WSU

- 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

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

Using Data-Oblivious Algorithms for Private Cloud Storage Access. Michael T. Goodrich Dept. of Computer Science

Persistent Binary Search Trees

Data Structure with C

Data Structures and Data Manipulation

Binary Coded Web Access Pattern Tree in Education Domain

Data Structures and Algorithms(5)

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Union-Find Problem. Using Arrays And Chains

Fundamental Algorithms

GRAPH THEORY LECTURE 4: TREES

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

Transcription:

Binary Search Trees 3/0/4 Presentation for use ith the textbook Data Structures and Algorithms in Java, th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldasser, Wiley, 04 Binary Search Trees 4 = 04 Goodrich, Tamassia, Goldasser Binary Search Trees Ordered Maps! Keys are assumed to come from a total order.! Items are stored in order by their keys! This allos us to support nearest neighbor queries:! Item ith largest key less than or equal to k! Item ith smallest key greater than or equal to k 04 Goodrich, Tamassia, Goldasser Binary Search Trees

Binary Search Trees 3/0/4 Binary Search! Binary search can perform nearest neighbor queries on an ordered map that is implemented ith an array, sorted by key n similar to the high-lo children s game n at each step, the number of candidate items is halved n terminates after O(log n) steps! Example: find(7) 0 3 4 7 4 l m h 0 3 4 7 4 l m h 0 3 4 7 4 l m h 0 3 4 7 4 l=m =h 04 Goodrich, Tamassia, Goldasser Binary Search Trees 3 Search Tables! A search table is an ordered map implemented by means of a sorted sequence n We store the items in an array-based sequence, sorted by key n We use an external comparator for the keys! Performance: n Searches take O(log n) time, using binary search n Inserting a ne item takes O(n) time, since in the orst case e have to shift n/ items to make room for the ne item n Removing an item takes O(n) time, since in the orst case e have to shift n/ items to compact the items after the removal! The lookup table is effective only for ordered maps of small size or for maps on hich searches are the most common operations, hile insertions and removals are rarely performed (e.g., credit card authorizations) 04 Goodrich, Tamassia, Goldasser Binary Search Trees 4

Binary Search Trees 3/0/4 Binary Search Trees! A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the folloing property: n Let u, v, and be three nodes such that u is in the left subtree of v and is in the right subtree of v. We have key(u) key(v) key()! External nodes do not store items! An inorder traversal of a binary search trees visits the keys in increasing order 4 04 Goodrich, Tamassia, Goldasser Binary Search Trees Search! To search for a key k, e trace a donard path starting at the root! The next node visited depends on the comparison of k ith the key of the current node! If e reach a leaf, the key is not found! Example: get(4): n Call TreeSearch(4,root)! The algorithms for nearest neighbor queries are similar Algorithm TreeSearch(k, v) if T.isExternal (v) return v if k key(v) return TreeSearch(k, left(v)) else if k = key(v) return v else { k key(v) } return TreeSearch(k, right(v)) 4 = 04 Goodrich, Tamassia, Goldasser Binary Search Trees 3

Binary Search Trees 3/0/4 Insertion! To perform operation put(k, o), e search for key k (using TreeSearch)! Assume k is not already in the tree, and let be the leaf reached by the search! We insert k at node and expand into an internal node! Example: insert 4 4 04 Goodrich, Tamassia, Goldasser Binary Search Trees 7 Deletion! To perform operation remove(k), e search for key k! Assume key k is in the tree, and let let v be the node storing k! If node v has a leaf child, e remove v and from the tree ith operation removeexternal(), hich removes and its parent! Example: remove 4 4 v 04 Goodrich, Tamassia, Goldasser Binary Search Trees 4

Binary Search Trees 3/0/4 Deletion (cont.)! We consider the case here the key k to be removed is stored at a node v hose children are both internal n e find the internal node that follos v in an inorder traversal n e copy key() into node v n e remove node and its left child z (hich must be a leaf) by means of operation removeexternal(z)! Example: remove 3 z v 3 v 04 Goodrich, Tamassia, Goldasser Binary Search Trees Performance! Consider an ordered map ith n items implemented by means of a binary search tree of height h n the space used is O(n) n methods get, put and remove take O(h) time! The height h is O(n) in the orst case and O(log n) in the best case 04 Goodrich, Tamassia, Goldasser Binary Search Trees 0