Algorithms Chapter 12 Binary Search Trees



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

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

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

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

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

Ordered Lists and Binary Trees

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

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

Chapter 14 The Binary Search Tree

Data Structures and Algorithms

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

Binary Search Trees CMPSC 122

Introduction to Data Structures and Algorithms

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

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

Binary Search Trees (BST)

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

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

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

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

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 Search Trees 3/20/14

Rotation Operation for Binary Search Trees Idea:

Converting a Number from Decimal to Binary

6 March Array Implementation of Binary Trees

Big Data and Scripting. Part 4: Memory Hierarchies

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

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. CSE 373 Data Structures

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

Binary Trees and Huffman Encoding Binary Search Trees

Full and Complete Binary Trees

Lecture 2 February 12, 2003

Lecture 4: Balanced Binary Search Trees

Data Structure [Question Bank]

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

The ADT Binary Search Tree

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 Search Trees. Adnan Aziz. Heaps can perform extract-max, insert efficiently O(log n) worst case

TREE BASIC TERMINOLOGIES

Data Structure with C

From Last Time: Remove (Delete) Operation

Algorithms and Data Structures

Algorithms and Data Structures

Binary Heap Algorithms

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

Data Structures Fibonacci Heaps, Amortized Analysis

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

Data Structures. Level 6 C Module Descriptor

Symbol Tables. Introduction

DATA STRUCTURES USING C

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.

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

CS711008Z Algorithm Design and Analysis

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

Cpt S 223. School of EECS, WSU

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,

OPTIMAL BINARY SEARCH TREES

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

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

Alex. Adam Agnes Allen Arthur

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

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

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

Binary search algorithm

DATABASE DESIGN - 1DL400

Classification/Decision Trees (II)

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

An Evaluation of Self-adjusting Binary Search Tree Techniques

Exam study sheet for CS2711. List of topics

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Persistent Binary Search Trees

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

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

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

Analysis of Algorithms I: Optimal Binary Search Trees

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

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

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

Lecture Notes on Binary Search Trees

Laboratory Module 6 Red-Black Trees

Section IV.1: Recursive Algorithms and Recursion Trees

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Sample Questions Csci 1112 A. Bellaachia

Lecture Notes on Binary Search Trees

GRAPH THEORY LECTURE 4: TREES

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

GENERATING THE FIBONACCI CHAIN IN O(log n) SPACE AND O(n) TIME J. Patera

Parallelization: Binary Tree Traversal

- 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

CS473 - Algorithms I

A Comparison of Dictionary Implementations

Transcription:

Algorithms Chapter 1 Binary Search Trees Outline Assistant Professor: Ching Chi Lin 林 清 池 助 理 教 授 chingchi.lin@gmail.com Department of Computer Science and Engineering National Taiwan Ocean University Overview Search trees are data structures that support many dynamicset operations. Dynamic set operations includes SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, and DELETE. Can be used as both a dictionary and as a priority queue. Basic operations take time proportional to the height of the tree, i.e., (h). For complete binary tree with n nodes: worst case (lgn). For linear chain of n nodes: worst case (n). Different types of search trees include binary search trees, red black trees (Chapter 13), and B trees (Chapter 18). We will cover binary search trees, tree walks, and operations on binary search trees. 3 Binary search trees We represent a binary tree by a linked data structure in which each node is an object. Each node contains the fields key and possibly other satellite data. left: points to left child. right: points to right child. p: points to parent. p[root[t ]] = NIL. Stored keys must satisfy the binary search tree property. 4 If y is in left subtree of x, then key[y] key[x]. If y is in right subtree of x, then key[y] key[x].

Figure 1.1 Binary search trees Inorder tree walk The binary search tree property allows us to print keys in a binary search tree in order, recursively. 3 7 3 Elements are printed in monotonically increasing order. 8 7 (a) A binary search tree on 6 nodes with height. (b) 8 A less efficient binary search tree with height 4 that contains the same keys. The inorder tree walk prints the keys in each of the two binary search trees from Figure 1.1 in the order, 3,,, 7, 8. 6 INORDER TREE WALK(x) 1. if x NIL. then INORDER TREE WALK(left[x]) 3. print key[x] 4. INORDER TREE WALK(right[x]) Properties of binary search trees Theorem If x is the root of an n node subtree, then the call INORDER TREE WALK(x) takes (n). Proof: T(0) = c, as It takes constant time on an empty subtree. Left subtree has k nodes and right subtree has n k 1 nodes. d : the time to execute INORDER TREE WALK(x), exclusive of the time spent in recursive calls. Prove by substitution method:t(n)=(c+d)n+c. For n = 0, we have (c+d) 0+c=c=T(0). For n > 0, T(n) = T(k) + T(n k 1)+d = ((c+d)k+c)+((c+d)(n k 1)+c)+d = (c+d)n+c (c+d)+c+d = (c+d)n+c. 7 Outline 8

Operations on binary search trees We shall examine SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR operations. The running times of these operations are all O(h). On most computers, the iterative version is more efficient. Time: The algorithm visiting nodes on a downward path from the root. Thus, running time is O(h). 9 TREE SEARCH(x, k) 1. if x = NIL or k = key[x]. then return x 3. if k < key[x] 4. then return TREE SEARCH(left[x], k). else return TREE SEARCH(right[x], k) ITERATIVE TREE SEARCH(x, k) 1. while x NIL and k key[x]. do if k < key[x] 3. then x left[x] 4. else x right[x]. return x Minimum and maximum The binary search tree property guarantees that the minimum key of a binary search tree is located at the leftmost node, and the maximum key of a binary search tree is located at the rightmost node. Traverse the appropriate pointers (left or right) until NIL is reached. Time: Both procedures visit nodes that form a downward path from the root to a leaf. Both procedures run in O(h)time. 10 TREE MINIMUM(x) 1. while left[x] NIL. do x left[x] 3. return x TREE MAXIMUM(x) 1. while right[x] NIL. do x right[x] 3. return x Successor and predecessor 1/ Successor and predecessor / Assuming that all keys are distinct, the successor of a node x is the node y such that key[y] is the smallest key > key[x]. The structure of a binary search tree allows us to determine the successor of a node without ever comparing keys. If x has the largest key in the binary search tree, then we say that x s successor is NIL. There are two cases: TREE SUCCESSOR(x) 1. if right[x] = NIL. then return TREE MINIMUM(right[x]) 3. y p[x] 4. while y NIL and x = right[y]. do x y 6. y p[y] 7. return y 1 6 18 3 7 17 0 4 13 9 If node x has a non empty right subtree, then x s successor is the minimum in x s right subtree. If node x has an empty right subtree and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x. 11 The successor of the node with key 13 is the node with key 1. Time: Since we either follow a path up the tree or follow a path down the tree. The running time is O(h). TREE PREDECESSOR is symmetric to TREE SUCCESSOR. 1

Outline Insertion and deletion The operations of insertion and deletion cause the dynamic set represented by a binary search tree to change. The binary search tree property must hold after the change. Insertion is more straightforward than deletion. 13 14 Insertion TREE INSERT(T, ) 1. y NIL; x root[t ]. while x NIL 3. do y x 4. if key[] < key[x]. then x left[x] 6. else x right[x] 7. p[] y 8. if y = NIL 9. then root[t ] /* Tree T was empty */ 10. else if key[] < key[y] 11. then left[y] 1. else right[y] Time: Since we follow a path down the tree. The running time is O(h). 1 1 18 9 1 19 13 17 Inserting an item with key 13 Deletion TREE DELETE is broken into three cases. Case 1: has no children. Delete by making the parent of point to NIL, instead of to. Case : has one child. Delete by making the parent of point to s child, instead of to. Case 3: has two children. s successor y has either no children or one child. (y is the minimum node with no left child in s right subtree.) Delete y from the tree (via Case 1 or ). Replace s key and satellite data with y s. 16

Case 1: Case : Case 3: 80 80 40 0 60 10 0 70 delete 80 delete delete 60 40 6 7 7 40 0 6 80 10 0 70 Deletion TREE DELETE(T, ) 1. if left[] = NIL or right[] = NIL. then y 3. else y TREE SUCCESSOR() 4. if left[y] = NIL. then x left[y] 6. else x right[y] 7. if x NIL 8. then p[x] p[y] 9. if p[y] = NIL 10. then root[t] x 11. else if y = left[p[y]] 1. then left[p[y]] x 13. else right[p[y]] x 14. if y 1. then key[] key[y] 16. copy y s satellite data into 17. return y Time: O(h). 7 7 18