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



Similar documents
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:

Analysis of Algorithms I: 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. Adnan Aziz. Heaps can perform extract-max, insert efficiently O(log n) worst case

Binary Search Trees CMPSC 122

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

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

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

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

Introduction to Data Structures and Algorithms

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

Chapter 14 The Binary Search Tree

Binary Search Trees (BST)

Ordered Lists and Binary Trees

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

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

12 Binary Search Trees

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

Symbol Tables. Introduction

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

Data Structures and Algorithms

Lecture 2 February 12, 2003

Converting a Number from Decimal to Binary

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

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

Binary Search Trees 3/20/14

Binary Search Trees. Ric Glassey

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

Binary Trees and Huffman Encoding Binary Search Trees

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

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

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

Data Structures Fibonacci Heaps, Amortized Analysis

Data Structure [Question Bank]

Lecture 4: Balanced Binary Search Trees

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

Persistent Binary Search Trees

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.

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

Algorithms and Data Structures

Exam study sheet for CS2711. List of topics

The ADT Binary Search Tree

Algorithms and Data Structures

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Binary Heaps. CSE 373 Data Structures

From Last Time: Remove (Delete) Operation

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

Data Structures. Level 6 C Module Descriptor

Laboratory Module 6 Red-Black Trees

PES Institute of Technology-BSC QUESTION BANK

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

CS711008Z Algorithm Design and Analysis

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

Binary Heap Algorithms

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)

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

Data Structure with C

OPTIMAL BINARY SEARCH TREES

Full and Complete Binary Trees

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Rotation Operation for Binary Search Trees Idea:

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)

Cpt S 223. School of EECS, WSU


International Journal of Advanced Research in Computer Science and Software Engineering

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

TREE BASIC TERMINOLOGIES

Lecture Notes on Binary Search Trees

Big Data and Scripting. Part 4: Memory Hierarchies

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

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

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

Fundamental Algorithms

10CS35: Data Structures Using C

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

A Comparison of Dictionary Implementations

Lecture Notes on Binary Search Trees

DATA STRUCTURES USING C

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

An Evaluation of Self-adjusting Binary Search Tree Techniques

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

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:

Data Structures and Algorithms(5)

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

An Immediate Approach to Balancing Nodes of Binary Search Trees

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

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

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Persistent Data Structures and Planar Point Location

DATABASE DESIGN - 1DL400

6 March Array Implementation of Binary Trees

GRAPH THEORY LECTURE 4: TREES

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

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Transcription:

Binary Search Trees \I think that I shall never see a poem as lovely as a tree Poem's are wrote by fools like me but only G-d can make atree \ {Joyce Kilmer Binary search trees provide a data structure which ef- ciently supports all six dictionary operations. A binary tree is a rooted tree where each node contains at most two children. Each child can be identied as either a left or right child. parent left right A binary tree can be implemented where each node has left and right pointer elds, an (optional) parent pointer, and a data eld.

Binary Search Trees A binary search tree labels each node in a binary tree with a single key such that for anynodex, and nodes in the left subtree of x have keys x and all nodes in the right subtree of x have key's x. 2 3 7 8 6 8 7 5 5 6 2 3 Left: A binary search tree. binary search tree. Right: A heap but not a The search tree labeling enables us to nd where any key is. Start at the root - if that isnottheonewewant, search either left or right depending upon whether what we want is or then root.

Searching in a Binary Tree Dictionary search operations are easy in binary trees... TREE-SEARCH(x, k) if (x = NIL)or(k = key[x]) then return x if (k <key[x]) then return TREE-SEARCH(left[x],k) else return TREE-SEARCH(right[x],k) The algorithm works because both the left and right subtrees of a binary search tree are binary search trees { recursive structure, recursive algorithm. This takes time proportional to the height of the tree, O(h).

Maximum and Minimum Where are the maximum and minimum elements in a binary tree? TREE-MAXIMUM(X) while right[x] 6= NIL do x = right[x] return x TREE-MINIMUM(x) while left[x] 6= NIL do x = left[x] return x Both take time proportional to the height of the tree, O(h).

Where is the predecessor? Where is the predecessor of a node in a tree, assuming all keys are distinct? X PREDECESSOR(X) SUCCESSOR(X) If X has two children, its predecessor is the maximum value in its left subtree and its successor the minimum value in its right subtree.

predecessor(x) X If it does not have a left child, a node's predecessor is its rst left ancestor. The proof of correctness comes from looking at the in-order traversal of the tree.

H A F B G D C E Tree-Successor(x) if right[x] 6= NIL then return Tree-Minimum(right[x]) y p[x] while (y 6= NIL) and (x = right[y]) do x y y p[y] return y Tree predecessor/successor both run in time proportional to the height of the tree. Inorder-Tree-walk(x) if (x <>NIL) then Inorder-Tree-Walk(left[x]) print key[x] Inorder-Tree-walk(right[x]) A-B-C-D-E-F-G-H

Tree Insertion Do a binary search to nd where it should be, then replace the termination NIL pointer with the new item. LEAF INSERTION FIGURE Tree-insert(T z) y =NIL x = root[t ] while x 6= NIL do y = x if key[z] <key[x] then x = left[x] else x = right[x] p[z] y if y = NIL then root[t ] z else if key[z] <key[y] then left[y] z else right[y] z y is maintained as the parent of x, since x eventually becomes NIL. The nal test establishes whether the NIL was a left or right turn from y. Insertion takes time proportional to the height of the tree, O(h).

Tree Deletion Deletion is somewhat more tricky than insertion, because the node to die may not be a leaf, and thus eect other nodes. Case (a), where the node is a leaf, is simple - just NIL out the parents child pointer. Case (b), where a node has one chld, the doomed node can just be cut out. Case (c), relabel the node as its successor (which has at most one child when z has two children!) and delete the successor! This implementation of deletion assumes parent pointers to make the code nicer, but if you had to save space they could be dispensed with by keeping the pointers on the search path stored in a stack. Tree-Delete(T z) if (left[z] = NIL)or(right[z] = NIL) then y z else y Tree-Successor(z) if left[y] 6= NIL then x left[y] else x right[y] if x 6= NIL then p[x] p[y] if p[y] =NIL then root[t ] x else if (y = left[p[y]])

then left[p[y]] x else right[p[y]] x if (y <>z) then key[z] key[y] /* If y has other elds, copy them, too. */ return y Lines 1-3 determine which node y is physically removed. Lines 4-6 identify x as the non-nil decendant, if any. Lines 7-8 give x anewparent. Lines 9-10 modify the root node, if necessary Lines 11-13 reattach the subtree, if necessary. Lines 14-16 if the removed node is deleted, copy. Conclusion: deletion takes time proportional to the height of the tree.

Balanced Search Trees All six of our dictionary operations, when implemented with binary search trees, take O(h), where h is the height of the tree. The best height we could hope to get is lg n, if the tree was perfectly balanced, since P blg nc i=0 2i n But if we get unlucky with our order of insertion or deletion, we could get linear height! insert(a) insert(b) insert(c) insert(d). A B C D In fact, random search trees on average have (lg N) height, but we are worried about worst case height. We can't easily use randomization - Why?

Perfectly Balanced Trees Perfectly balanced trees require a lot of work to maintain: 9 5 13 3 7 11 15 2 4 6 8 10 12 14 16 1 If we insert the key 1, we must move every single node in the tree to rebalance it, taking (n) time. Therefore, when we talk about "balanced" trees, we mean trees whose height is O(lg n), so all dictionary operations (insert, delete, search, min/max, successor/predecessor) take O(lg n) time. Red-Black trees are binary search trees where each node is assigned a color, where the coloring scheme helps us maintain the height as (lg n).