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

Similar documents
Analysis of Algorithms I: Binary Search Trees

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

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

Introduction to Data Structures and Algorithms

Algorithms Chapter 12 Binary Search Trees

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

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

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

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

Binary Heaps. CSE 373 Data Structures

Binary Trees and Huffman Encoding Binary Search Trees

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

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

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

Binary Search Trees 3/20/14

Chapter 14 The Binary Search Tree

From Last Time: Remove (Delete) Operation

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)

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

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:

Full and Complete Binary Trees

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Ordered Lists and Binary Trees

Big Data and Scripting. Part 4: Memory Hierarchies

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

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

Lecture 2 February 12, 2003

Binary Heap Algorithms

Binary Search Trees. Ric Glassey

Data Structures and Algorithms

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

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

Symbol Tables. Introduction

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Binary Search Trees (BST)

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

Load Balancing. Load Balancing 1 / 24

Classification/Decision Trees (II)

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

TREE BASIC TERMINOLOGIES

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

Algorithms and Data Structures

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

Binary Coded Web Access Pattern Tree in Education Domain

CS711008Z Algorithm Design and Analysis

Data Structures Fibonacci Heaps, Amortized Analysis

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

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

6 March Array Implementation of Binary Trees

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

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

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Persistent Data Structures

Converting a Number from Decimal to Binary

Exam study sheet for CS2711. List of topics

Algorithms and Data Structures

OPTIMAL BINARY SEARCH TREES

Cpt S 223. School of EECS, WSU

A Comparison of Dictionary Implementations

Binary Search Trees CMPSC 122

Lecture 1: Course overview, circuits, and formulas

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

External Memory Geometric Data Structures

Persistent Data Structures and Planar Point Location

Why Use Binary Trees?

Optimizations. Optimization Safety. Optimization Safety. Control Flow Graphs. Code transformations to improve program

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

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

Optimal Binary Search Trees Meet Object Oriented Programming

The ADT Binary Search Tree

International Journal of Software and Web Sciences (IJSWS)

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

Introduction to Data Structures and Algorithms

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

A First Investigation of Sturmian Trees

Efficient Interval Management in Microsoft SQL Server

Dynamic 3-sided Planar Range Queries with Expected Doubly Logarithmic Time

CSE 504: Compiler Design. Data Flow Analysis

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

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

12 Binary Search Trees

Algorithms and Data Structures Written Exam Proposed SOLUTION

Rotation Operation for Binary Search Trees Idea:

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

DATABASE DESIGN - 1DL400

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

Fundamental Algorithms

8.1 Min Degree Spanning Tree

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

Data Structure with C

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

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

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

CS473 - Algorithms I

COREsim Transcript Data Format

Analysis of Algorithms I: Optimal Binary Search Trees

Algorithms. Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search line segment intersection kd trees interval search trees rectangle intersection

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Chapter 13: Query Processing. Basic Steps in Query Processing

Transcription:

Binary search tree Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later). Data strutcure fields usually include for a given node x, the following attributes: x.left (left child);; x.right (right child);; x.p (parent);; x.key. Root of entire tree pointed to by T.root Example of storing keys in a binary search tree: (binary because branches either to left or to right) Do you see a pattern? Main property of binary search tree: Left side: key smaller equal to parent Right side: key larger equal to parent Given node with key k Left: All keys < k Right: All keys > k You can see this in tree example above node by node going from parent to child, and it is also true for the whole subtree to the left and to the right. Is this tree unique for the given set of keys? Answer: No;; The same set of keys have many possible trees.

Another example: Question: How can we traverse the tree and print out keys in ascending order: First left side: Then root: Then right: 2 5 5 6 7 8

What about this one? (answer: same: 2 5 5 6 7 8) Runtime for inorder-tree-walk? Phi(n) for a binary tree with n nodes Why? Omega(n) because visits every node of the tree at least once and print the key;; book shows also O(n) using subsitution method (it s intuitive in any case that you traverse all nodes no more than constant amount of times). Query operations on binary search tree: search;; max;; min;; successor;; predecessor. Run time depends on? Height of binary search tree. Question: Is the height always log n? Answer: Not necessarily, since tree could be made non efficiently and each time branch to the right only. Then its run time could be O(n) and as bad as a linked list. Therefore height and run time for search queries could be O(log n) but could also be O(n)! Search: Main idea of searching for key k: if looking for k smaller than node.key, search to the left, otherwise to the right. Searching for k = 13:

Search trajectory for k=13: 15 -> 6 -> 7 -> 13 Tree-Search(x,k) 1. if k=x.key or x==nil return x 2. if k < x.key return Tree-Search(x.left, k) 3. else if k > x.key return Tree-Search(x.right, k) (more formal in book and can also be written iterative in book) Run time: height of tree. Again, could be O(log n) or O(n) depending on tree and its balance. Min/max: Question: finding the min? Answer: keep traversing to the left subtree until we encounter a nil Finding the min: Finding the max: keep traversing to the right subtree

Successor: (similar for predecessor) Case 1: Right sub-tree non empty: left-most node of right subtree (why? It s the smallest value that is larger than the given node) Example case 1: successor of 15 Right sub-tree non empty: Left most node of right sub-tree Case 2: Right subtree is empty, then successor is an ancestor (we ll first define ancestor and then more specifically what we mean here) Ancestor definition in Cormen book: an ancestor of node x is any node from the root to node x. This is either a parent of x or the node x itself. More specifically: successor of node x is an ancestor that is the lowest ancestor of node x, whose left child is also an ancestor of x. Put differently: Example case 2: successor of 9 Right sub-tree empty: Successor of 9 is 13; lowest ancestor of 9 whose left child (here 9) is also an ancestor of 9

Another example case 2: successor of node 4 Right sub-tree empty: Successor of 4 is 6; lowest ancestor of 4 whose left child is also an ancestor of 4 Why does this work? Consider node x (here node 4). Intuitively, if left child of ancestor y is also an ancestor of node x (here node 6), then node x is smaller than the ancestor y. All ancestor nodes before y (here before node 6 = node 3) do not have a left child that is an ancestor of x, which means that x is a right child of that ancestor and so that ancestor is smaller than x. Another example case 2: successor of node 20 Answer: none. Why? Another example case 2: successor of node 13 is 15. Why? Run time of successor: O(height), since we either follow a path up the tree or a path down the tress