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



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

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:

Analysis of Algorithms I: Binary Search Trees

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

Binary Search Trees CMPSC 122

Ordered Lists and Binary Trees

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

Binary Heaps. CSE 373 Data Structures

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

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 (BST)

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

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

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:

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

Rotation Operation for Binary Search Trees Idea:

12 Binary Search Trees

Symbol Tables. Introduction

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

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

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

Binary Search Trees 3/20/14

Converting a Number from Decimal to Binary

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

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

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

Chapter 14 The Binary Search Tree

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

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

Big Data and Scripting. Part 4: Memory Hierarchies

Cpt S 223. School of EECS, WSU

A Comparison of Dictionary Implementations

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

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

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

Binary Heap Algorithms

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Lecture 2 February 12, 2003

6 March Array Implementation of Binary Trees

CS711008Z Algorithm Design and Analysis

Fundamental Algorithms

Full and Complete Binary Trees

Persistent Data Structures and Planar Point Location

Binary Search Trees. Ric Glassey

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

From Last Time: Remove (Delete) Operation

Why Use Binary Trees?

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

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

Analysis of Algorithms I: Optimal Binary Search Trees

Data Structure [Question Bank]

TREE BASIC TERMINOLOGIES

- 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

Data Structures Fibonacci Heaps, Amortized Analysis

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

Data Structures and Data Manipulation

Persistent Binary Search Trees

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

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.

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)

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

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

The ADT Binary Search Tree

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)

Lecture Notes on Binary Search Trees

Algorithms and Data Structures Written Exam Proposed SOLUTION

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

DATA STRUCTURES USING C

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

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

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

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

Binary Trees and Huffman Encoding Binary Search Trees

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

Exam study sheet for CS2711. List of topics

Lecture 4: Balanced Binary Search Trees

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

Data Warehousing und Data Mining

Data Structures. Level 6 C Module Descriptor

Sample Questions Csci 1112 A. Bellaachia

Data Structure with C

10CS35: Data Structures Using C

An Evaluation of Self-adjusting Binary Search Tree Techniques

Lecture Notes on Binary Search Trees

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

DATABASE DESIGN - 1DL400

Laboratory Module 6 Red-Black Trees

Review of Hashing: Integer Keys

Algorithms and Data Structures

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

Lecture 1: Data Storage & Index

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

Transcription:

Binary Search Tree A binary search tree is a data structure that allows for key lookup, insertion, and deletion. It is a binary tree, meaning every node of the tree has at most two child nodes, a left child and a right child. Each node of the tree holds the following information: x.key - Value stored in node x x.left- Pointer to the left child of node x. NIL if x has no left child x.right - Pointer to the right child of node x. NIL if x has no right child x.parent - Pointer to the parent node of node x. NIL if x has no parent, i.e. x is the root of the tree Later on this week, we will learn about binary search trees that holds data in addition to the four listed above but for now we will focus on the vanilla binary search tree. A binary search tree has two simple properties: For each node x, every value found in the left subtree of x is less than or equal to the value found in x For each node x, every value found in the right subtree of x is greater than or equal to the value found in x

BST Operations There are operations of a binary search tree that take advantage of the properties above to search for keys. There are other operations that manipulate the tree to insert new key or remove old ones while maintaining these two properties. find(x, k) Description: Find key k in a binary search tree rooted at x. Return the node that contains k if it exists or NIL if it is not in the tree find(x, k) while x!= NIL and k!= x.key if k < x.key x = x.left x = x.right return x Analysis: At worst case, find goes down the longest branch of the tree. In this case, find takes O(h) time where h is the height of the tree

insert(x, k) Description: Insert key k into the binary search tree T insert(t, k) z.key = k //z is the node to be inserted z.parent = NIL x = root(t) while x!= NIL //find where to insert z z.parent = x if z.key < x.key x = x.left x = x.right if z.parent = NIL //in the case that T was an empty tree root(t) = z //set z to be the root if z.key < z.parent.key //otherwise insert z z.parent.left = z z.parent.right = z Analysis: At worst case, insert goes down the longest branch of the tree to find where to insert and then makes constant time operations to actually make the insertion. In this case, insert takes O(h) time where h is the height of the tree find-min(x) and find-max(x) Description: Return the node with the minimum or maximum key of the binary search tree rooted at node x find-min(x) while x.left!= NIL

x = x.left return x Analysis: At worst case, find-min goes down the longest branch of the tree before finding the minimum element. In this case, find-min takes O(h) time where h is the height of the tree next-larger(x) and next-smaller(x) Description: Return the node that contains the next larger (the successor) or next smaller (the predecessor) key in the binary search tree in relation to the key at node x Case 1: x has a right sub-tree where all keys are larger than x.key. The next larger key will be the minimum key of x s right sub-tree Case 2: x has no right sub-tree. We can find the next larger key by traversing up x s ancestry until we reach a node that s a left child. That node s parent will contain the next larger key next-larger(x) if x.right!= NIL //case 1 return find-min(x.right) y = x.parent while y!= NIL and x = y.right //case 2 x = y y = y.parent return y

Analysis: At worst case, next-larger goes through the longest branch of the tree if x is the root. Since find-min can take O(h) time, next-larger could also take O(h) time where h is the height of the tree delete(x) Description: Remove the node x from the binary search tree, making the necessary adjustments to the binary search tree to maintain its properties. (Note that this operation removes a specified node from the tree. If you wanted to delete a key k from the tree, you would have to first call find(k) to find the node with key k and then call delete to remove that node) Case 1: x has no children. Just delete it (i.e. change parent node so that it doesn t point to x) Case 2: x has one child. Splice out x by linking x s parent to x s child Case 3: x has two children. Splice out x s successor and replace x with x s successor delete(x) if x.left = NIL and x.right = NIL //case 1 if x.parent.left = x x.parent.left = NIL x.parent.right = NIL if x.left = NIL //case 2a connect x.parent to x.right if x.right = NIL //case 2b connect x.parent to x.left //case 3 y = next-larger(x) connect y.parent to y.right replace x with y

Analysis: In case 3, delete calls next-larger, which takes O(h) time. At worst case, delete takes O(h) time where h is the height of the tree inorder-tree-walk(x) Description: Print out the keys in the binary search tree rooted at node x in sorted order inorder-tree-walk(x) if x!= NIL inorder-tree-walk(x.left) print x.key inorder-tree-walk(x.right) Analysis: inorder-tree-walk goes through every node and traverses to each node s left and right children. Overall, inorder-tree-walk prints n keys and traverses 2n times, resulting in O(n) runtime