Analysis of Algorithms I: Binary Search Trees



Similar documents
Algorithms Chapter 12 Binary Search Trees

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

Introduction to Data Structures and Algorithms

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

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

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

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

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

Analysis of Algorithms I: Optimal Binary Search Trees

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

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

Lecture 2 February 12, 2003

Binary Search Trees CMPSC 122

Full and Complete Binary Trees

From Last Time: Remove (Delete) Operation

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -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

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

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)

Binary Trees and Huffman Encoding Binary Search Trees

Binary Search Trees 3/20/14

Ordered Lists and Binary Trees

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

Converting a Number from Decimal to Binary

Big Data and Scripting. Part 4: Memory Hierarchies

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

6 March Array Implementation of Binary Trees

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

CS711008Z Algorithm Design and Analysis

Data Structures and Algorithms

Classification/Decision Trees (II)

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

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

Cpt S 223. School of EECS, WSU

Data Structure with C

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

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

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

Alex. Adam Agnes Allen Arthur

Rotation Operation for Binary Search Trees Idea:

OPTIMAL BINARY SEARCH TREES

Binary Heaps. CSE 373 Data Structures

The ADT Binary Search Tree

GRAPH THEORY LECTURE 4: TREES

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

Symbol Tables. Introduction

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:

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

Data Structures Fibonacci Heaps, Amortized Analysis

Lecture 4: Balanced Binary Search Trees

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,

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

Binary Search Trees (BST)

External Memory Geometric Data Structures

Algorithms and Data Structures

CS473 - Algorithms I

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

TREE BASIC TERMINOLOGIES

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

Data Structure [Question Bank]

Physical Data Organization

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

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)

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

Exam study sheet for CS2711. List of topics

Algorithms and Data Structures Written Exam Proposed SOLUTION

Data Structures. Level 6 C Module Descriptor

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

DATA STRUCTURES USING C

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

Persistent Data Structures and Planar Point Location

Binary Heap Algorithms

Binary Search Trees. Ric Glassey

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

Approximation Algorithms

Why Use Binary Trees?

Data Warehousing und Data Mining

An Evaluation of Self-adjusting Binary Search Tree Techniques

Lecture Notes on Binary Search Trees

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

12 Binary Search Trees

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

Algorithms and Data Structures

On line construction of suffix trees 1

Load Balancing. Load Balancing 1 / 24

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

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Persistent Binary Search Trees

Chapter 13: Query Processing. Basic Steps in Query Processing

A Comparison of Dictionary Implementations

Scheduling Shop Scheduling. Tim Nieberg

International Journal of Software and Web Sciences (IJSWS)

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

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

Transcription:

Analysis of Algorithms I: Binary Search Trees Xi Chen Columbia University

Hash table: A data structure that maintains a subset of keys from a universe set U = {0, 1,..., p 1} and supports all three dictionary operations: Insert, Delete and Search. Has very good performance: Universal hashing and Perfect hashing. But what if we need a data structure that supports additional operations: 1 Minimum ( ): return the smallest key; 2 Maximum ( ): return the largest key; 3 Successor, Predecessor, Nearest matches: to be defined later Starting from this class, we study binary search trees that support all these operations. We show that all these operations can be done in time linear in the height h of the tree.

Binary tree: A tree in which each internal node has at most two child nodes, usually referred to as its left and right child node. How to represent a binary tree? Check Section 10.4 of the text book. Each node x in the tree consists of three pointers: 1 x.p points to its parent node (nil if x is the root); 2 x.l and x.r point to its left and right child nodes; nil if x has no left or right child; e.g., if x is a leaf then both are nil.

Binary search tree: A binary tree in which each node x has a key x.k from the universe set U (or a pointer that leads to an object that has a key from U). Moreover, the keys satisfy the following binary-search-tree property: Let x be any node in the tree. Then for any node y in the left subtree of x, we have y.k x.k. For any node z in the right subtree of x, we have x.k z.k. Note that we allow duplicate keys in a binary search tree, which sometimes makes things more complicated. We usually use n to denote the number of nodes and h to denote the height of the tree being considered.

Height of a binary search tree: length of the longest path from the root to a leaf. This parameter is crucial to the worst-case performance of a binary search tree. Note that two binary search trees may have exactly the same set of keys but have extremely different shape, one balanced and the other unbalanced (see the example on Page 287). In general, a binary search tree can be extremely unbalanced (e.g., a tree consists of a single path of length n 1) and has a height much larger than O(lg n) and thus, has bad worst-case performance. Now we start to describe operations that a binary search tree supports with O(h) worst-case running time. In the next class, we will see how to maintain a balanced binary search tree with height bounded by O(lg n).

Let v be a node in the tree. Start with Min and Max: Min (v): find a node in the subtree of v with the smallest key 1 while v.l nil 2 do v = v.l 3 return v Keep moving to the left until v has no left child.

Max (v): find a node in the subtree of v with the largest key 1 while v.r nil 2 do v = v.r 3 return v Symmetric. Just keep moving to the right child.

Given a node v, the keys in its subtree can be sorted in linear time: Inorder (v): visit each node in the subtree of v in the sorted order 1 If v.l nil 2 Inorder (v.l) 3 print v.k 4 If v.r nil 5 Inorder (v.r)

The running time is linear in the number of nodes in the subtree of v. Check Theorem 12.1 on page 288. Use the following recurrence: ( ) T (n) max T (k) + T (n k 1) + O(1) 0 k n and the substitution method.

Search (v, k): Find a node with key = k U in the subtree of v 1 If v.k = k, return v 2 If k < v.k 3 If v.l = nil, return nil; otherwise return Search (v.l, k) 4 If k > v.k 5 If v.r = nil, return nil; otherwise return Search (v.r, k) Check page 291 for an implementation without using recursions.

The following two operations assume that the keys are distinct. Successor (v): find the node with the smallest key > v.k. First, if the right subtree of v is nonempty, then the successor of v is the leftmost node in v s right subtree (or equivalently, the node with the smallest key in v s right subtree). Now if v.r = nil, examine the path from v back to the root. Find the lowest ancestor u of v whose left child is also an ancestor of v, and u is the successor of v. I leave both proofs of correctness as exercises. If no such u exists, it means v has the largest key in the tree and has no successor.

Deletion is easy (but keep in mind that we need to maintain the binary-search-tree property). If we want to delete a node v with no child, just remove it. If v has one child, then remove v and connect v s parent node with v s child. If v is the left child of v.p then v s child becomes the new left child of v.p, and vice versa. The tricky case is when v has both left and right child nodes.

For this case, let u = v.r. We have u nil because v is assumed to have two children. Next, starting from u, keep going left until reaching a node w with no left child. Check that w has the smallest key in the subtree of u. Finally we remove w (using one of the first two cases because w does not have left child), and then replace the key of u by the key of w. Verify the correctness as how the binary-search-tree property is maintained.

Insertion is easier. We start by searching for the input key k in the binary tree. This finally leads us to a leaf u. If u.k > k, create a new node v as the left child of u and set v.k = k. If u.k k, create a new node v as the right child of u and set v.k = k. Again, verify the correctness that the binary-search-tree property is well maintained.

While both insertion and deletion are easy, they may lead to highly unbalanced trees and they are responsible for very bad worst-case performance. For example, start with an empty tree and insert 1, 2,..., n. This results in a tree (a path actually) of height n 1. In the next class we will see how to insert and delete carefully in order to maintain a balanced tree with O(lg n) height. This gives us a data structure that supports all operations in worst-case O(lg n) running time.