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



Similar documents
Algorithms Chapter 12 Binary Search Trees

Introduction to Data Structures and Algorithms

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

Binary Search Trees CMPSC 122

Analysis of Algorithms I: Binary Search Trees

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

Binary Search Trees (BST)

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

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

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

Converting a Number from Decimal to Binary

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

Ordered Lists and Binary Trees

Chapter 14 The Binary Search Tree

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

TREE BASIC TERMINOLOGIES

Full and Complete Binary Trees

Data Structures and Algorithms

Binary Trees and Huffman Encoding Binary Search Trees

Binary Search Trees 3/20/14

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

From Last Time: Remove (Delete) Operation

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

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:

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

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

DATA STRUCTURES USING C

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

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

Data Structure [Question Bank]

Binary Heaps. CSE 373 Data Structures

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

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)

Big Data and Scripting. Part 4: Memory Hierarchies

Algorithms and Data Structures

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

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)

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

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

Introduction to Data Structures and Algorithms

Cpt S 223. School of EECS, WSU

Exam study sheet for CS2711. List of topics

GRAPH THEORY LECTURE 4: TREES

Algorithms and Data Structures

The ADT Binary Search Tree

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

Classification/Decision Trees (II)

Data Structure with C

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

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

Binary Heap Algorithms

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

Analysis of Algorithms I: Optimal Binary Search Trees

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

6 March Array Implementation of Binary Trees

Fundamental Algorithms

The Expectation Maximization Algorithm A short tutorial

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

Data Structures. Level 6 C Module Descriptor

Alex. Adam Agnes Allen Arthur

Binary Search Trees. Adnan Aziz. Heaps can perform extract-max, insert efficiently O(log n) worst case

CS711008Z Algorithm Design and Analysis

Lecture 2 February 12, 2003

Rotation Operation for Binary Search Trees Idea:

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Lecture 1: Course overview, circuits, and formulas

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

Sample Questions Csci 1112 A. Bellaachia

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,

Binary Search Trees. Ric Glassey

OPTIMAL BINARY SEARCH TREES

An Evaluation of Self-adjusting Binary Search Tree Techniques

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

Symbol Tables. Introduction

Persistent Data Structures and Planar Point Location

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Data Structures UNIT III. Model Question Answer

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

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

Complexity of Union-Split-Find Problems. Katherine Jane Lai

How Asymmetry Helps Load Balancing

Analysis of Algorithms, I

Lecture Notes on Binary Search Trees

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

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Strategic Deployment in Graphs. 1 Introduction. v 1 = v s. v 2. v 4. e 1. e e 3. e 2. e 4

Catalan Numbers. Thomas A. Dowling, Department of Mathematics, Ohio State Uni- versity.

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

PES Institute of Technology-BSC QUESTION BANK

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Why Use Binary Trees?

Persistent Data Structures

2. FINDING A SOLUTION

Transcription:

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: For all nodes x and y, if y belongs to the left subtree of x, then the key at y is less than the key at x, and if y belongs to the right subtree of x, then the key at y is greater than the key at x. We will assume that the keys of a BST are pairwise distinct. Each node has the following attributes: p, left, and right, which are pointers to the parent, the left child, and the right child, respectively, and key, which is key stored at the node. 1

An example 7 4 12 2 6 9 19 3 5 8 11 15 20 2

Traversal of the Nodes in a BST By traversal we mean visiting all the nodes in a graph. Traversal strategies can be specified by the ordering of the three objects to visit: the current node, the left subtree, and the right subtree. We assume the the left subtree always comes before the right subtree. Then there are three strategies. 1. Inorder. The ordering is: the left subtree, the current node, the right subtree. 2. Preorder. The ordering is: the current node, the left subtree, the right subtree. 3. Postorder. The ordering is: the left subtree, the right subtree, the current node. 3

Inorder Traversal Pseudocode This recursive algorithm takes as the input a pointer to a tree and executed inorder traversal on the tree. While doing traversal it prints out the key of each node that is visited. Inorder-Walk(x) 1: if x = nil then return 2: Inorder-Walk(left[x]) 3: Print key[x] 4: Inorder-Walk(right[x]) We can write a similar pseudocode for preorder and postorder. 4

2 1 3 1 3 2 3 1 2 inorder preorder postorder 7 4 12 2 6 9 19 3 5 8 11 15 20 What is the outcome of inorder traversal on this BST? How about postorder traversal and preorder traversal? 5

Inorder traversal gives: 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 19, 20. Preorder traversal gives: 7, 4, 2, 3, 6, 5, 12, 9, 8, 11, 19, 15, 20. Postorder traversal gives: 3, 2, 5, 6, 4, 8, 11, 9, 15, 20, 19, 12, 7. So, inorder travel on a BST finds the keys in nondecreasing order! 6

Operations on BST 1. Searching for a key We assume that a key and the subtree in which the key is searched for are given as an input. We ll take the full advantage of the BST-property. Suppose we are at a node. If the node has the key that is being searched for, then the search is over. Otherwise, the key at the current node is either strictly smaller than the key that is searched for or strictly greater than the key that is searched for. If the former is the case, then by the BST property, all the keys in th left subtree are strictly less than the key that is searched for. That means that we do not need to search in the left subtree. Thus, we will examine only the right subtree. If the latter is the case, by symmetry we will examine only the right subtree. 7

Algorithm Here k is the key that is searched for and x is the start node. BST-Search(x, k) 1: y x 2: while y nil do 3: if key[y] = k then return y 4: else if key[y] < k then y right[y] 5: else y left[y] 6: return ( NOT FOUND ) 8

An Example 7 search for 8 4 11 2 6 9 13 NIL What is the running time of search? 9

2. The Maximum and the Minimum To find the minimum identify the leftmost node, i.e. the farthest node you can reach by following only left branches. To find the maximum identify the rightmost node, i.e. the farthest node you can reach by following only right branches. BST-Minimum(x) 1: if x = nil then return ( Empty Tree ) 2: y x 3: while left[y] nil do y left[y] 4: return (key[y]) BST-Maximum(x) 1: if x = nil then return ( Empty Tree ) 2: y x 3: while right[y] nil do y right[y] 4: return (key[y]) 10

3. Insertion Suppose that we need to insert a node z such that k = key[z]. Using binary search we find a nil such that replacing it by z does not break the BST-property. 11

BST-Insert(x, z, k) 1: if x = nil then return Error 2: y x 3: while true do { 4: if key[y] < k 5: then z left[y] 6: else z right[y] 7: if z = nil break 8: } 9: if key[y] > k then left[y] z 10: else right[p[y]] z 12

4. The Successor and The Predecessor The successor (respectively, the predecessor) of a key k in a search tree is the smallest (respectively, the largest) key that belongs to the tree and that is strictly greater than (respectively, less than) k. The idea for finding the successor of a given node x. If x has the right child, then the successor is the minimum in the right subtree of x. Otherwise, the successor is the parent of the farthest node that can be reached from x by following only right branches backward. 13

An Example 23 7 25 4 12 2 6 9 19 3 5 8 11 15 20 14

Algorithm BST-Successor(x) 1: if right[x] nil then 2: { y right[x] 3: while left[y] nil do y left[y] 4: return (y) } 5: else 6: { y x 7: while right[p[x]] = x do y p[x] 8: if p[x] nil then return (p[x]) 9: else return ( NO SUCCESSOR ) } 15

The predecessor can be found similarly with the roles of left and right exchanged and with the roles of maximum and minimum exchanged. For which node is the successor undefined? What is the running time of the successor algorithm? 16

5. Deletion Suppose we want to delete a node z. 1. If z has no children, then we will just replace z by nil. 2. If z has only one child, then we will promote the unique child to z s place. 3. If z has two children, then we will identify z s successor. Call it y. The successor y either is a leaf or has only the right child. Promote y to z s place. Treat the loss of y using one of the above two solutions. 17

5 1 6 3 2 4 5 1 6 3 2 4 8 7 8 7 8 11 5 11 9 13 1 6 9 13 10 3 10 2 4 8 11 5 11 9 13 3 6 9 13 10 2 4 7 10 8 9 5 11 5 11 1 6 9 13 1 6 10 13 3 7 10 3 2 4 2 4 18

Algorithm This algorithm deletes z from BST T. BST-Delete(T, z) 1: if left[z] = nil or right[z] = nil 2: then y z 3: else y BST-Successor(z) 4: y is the node that s actually removed. 5: Here y does not have two children. 6: if left[y] nil 7: then x left[y] 8: else x right[y] 9: x is the node that s moving to y s position. 10: if x nil then p[x] p[y] 11: p[x] is reset If x isn t NIL. 12: Resetting is unnecessary if x is NIL. 19

Algorithm (cont d) 13: if p[y] = nil then root[t ] x 14: If y is the root, then x becomes the root. 15: Otherwise, do the following. 16: else if y = left[p[y]] 17: then left[p[y]] x 18: If y is the left child of its parent, then 19: Set the parent s left child to x. 20: else right[p[y]] x 21: If y is the right child of its parent, then 22: Set the parent s right child to x. 23: if y z then 24: { key[z] key[y] 25: Move other data from y to z } 27: return (y) 20

Summary of Efficiency Analysis Theorem A On a binary search tree of height h, Search, Minimum, Maximum, Successor, Predecessor, Insert, and Delete can be made to run in O(h) time. 21

Randomly built BST Suppose that we insert n distinct keys into an initially empty tree. Assuming that the n! permutations are equally likely to occur, what is the average height of the tree? To study this question we consider the process of constructing a tree T by inserting in order randomly selected n distinct keys to an initially empty tree. Here the actually values of the keys do not matter. What matters is the position of the inserted key in the n keys. 22

The Process of Construction So, we will view the process as follows: A key x from the keys is selected uniformly at random and is inserted to the tree. Then all the other keys are inserted. Here all the keys greater than x go into the right subtree of x and all the keys smaller than x go into the left subtree. Thus, the height of the tree thus constructed is one plus the larger of the height of the left subtree and the height of the right subtree. 23

Random Variables n = number of keys X n = height of the tree of n keys Y n = 2 X n. We want an upper bound on E[Y n ]. For n 2, we have E[Y n ] = 1 n n i=1 2E[max{Y i 1, Y n i }]. E[max{Y i 1, Y n i }] E[Y i 1 + Y n i ] E[Y i 1 ] + E[Y n i ] Collecting terms: E[Y n ] 4 n n 1 i=1 E[Y i ]. 24

Analysis We claim that for all n 1 E[Y n ] 1 4 We prove this by induction on n. ( ) n+3 3. Base case: E[Y 1 ] = 2 0 = 1. Induction step: We have E[Y n ] 4 n Using the fact that n 1 i=1 E[Y i ] n 1 i=0 ( i + 3) ( n + 3 = 3 4 E[Y n ] 4 n 1 4 (n + 3) 4 E[Y n ] 1 4 (n + 3) 3 ) 25

Jensen s inequality A function f is convex if for all x and y, x < y, and for all λ, 0 λ 1, f(λx + (1 λ)y) λf(x) + (1 λ)f(y) Jensen s inequality states that for all random variables X and for all convex function f f(e[x]) E[f(X)]. Let this X be X n and f(x) = 2 x. Then E[f(X)] = E[Y n ]. So, we have 2 E[X n] 1 4 ( n + 3 The right-hand side is at most (n + 3) 3. By taking the log of both sides, we have 3 ). E[X n ] = O(log n). Thus the average height of a randomly build BST is O(log n). 26