Introduction to Data Structures and Algorithms



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

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

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

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

Introduction to Data Structures and Algorithms

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

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

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

12 Binary Search 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

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

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

Chapter 14 The Binary Search Tree

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

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

Binary Search Trees 3/20/14

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

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

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 and Algorithms

Binary Search Trees CMPSC 122

Symbol Tables. Introduction

Analysis of Algorithms I: Optimal Binary Search Trees

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

Ordered Lists and Binary Trees

Big Data and Scripting. Part 4: Memory Hierarchies

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

Rotation Operation for Binary Search Trees Idea:

Data Structure [Question Bank]

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

Converting a Number from Decimal to Binary

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 Heap Algorithms

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

Lecture 2 February 12, 2003

Cpt S 223. School of EECS, WSU

From Last Time: Remove (Delete) Operation

The ADT Binary Search Tree

Data Structures. Level 6 C Module Descriptor

Alex. Adam Agnes Allen Arthur

Algorithms and Data Structures

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

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

Data Structure with C

Binary Search Trees. Ric Glassey

Laboratory Module 6 Red-Black Trees

Lecture 4: Balanced Binary Search Trees

Algorithms and Data Structures

Full and Complete Binary Trees

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

Lecture Notes on Binary Search Trees

Solutions for Introduction to algorithms second edition

Algorithms and Data Structures Written Exam Proposed SOLUTION

Persistent Binary Search Trees

Lecture Notes on Binary Search Trees

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

CS473 - Algorithms I

6 March Array Implementation of Binary Trees

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

An Evaluation of Self-adjusting Binary Search Tree Techniques

TREE BASIC TERMINOLOGIES

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

DATA STRUCTURES USING C

International Journal of Advanced Research in Computer Science and Software Engineering

Binary search algorithm

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

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

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

OPTIMAL BINARY SEARCH TREES

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Section IV.1: Recursive Algorithms and Recursion Trees

Data Structures and Algorithms(5)

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

Analysis of Binary Search algorithm and Selection Sort algorithm

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.

A Comparison of Dictionary Implementations

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

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

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

CS711008Z Algorithm Design and Analysis

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Persistent Data Structures and Planar Point Location

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

10CS35: Data Structures Using C

External Memory Geometric Data Structures

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

DATABASE DESIGN - 1DL400

Zabin Visram Room CS115 CS126 Searching. Binary Search

Exam study sheet for CS2711. List of topics

Transcription:

Introduction to Data Structures and Algorithms Chapter: Binary Search Trees Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German) Martensstraße 3, 91058 Erlangen

Search Trees Search trees can be used to support dynamic sets, i.e. data structures that change during lifetime, where an ordering relation among the keys is defined. They support many operations, such as SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, DELETE. The time that an operation takes depends on the height h of the tree. Data Structures and Algorithms (175)

Binary Search Trees In the following we look at binary search trees a special kind of binary trees that support the mentioned operations on dynamic sets Data Structures and Algorithms (176)

Binary Search Tree A binary tree is a binary search tree, if the following binary-search-tree property is satisfied: For any node x of the tree: If y is a node in the left subtree of x then key[y] key[x] If y is a node in the right subtree of x then key[y] key[x] x x y y Data Structures and Algorithms (177)

Examples of Binary Search Trees 7 22 10 40 7 51 21 10 40 21 51 22 The same set of keys Data Structures and Algorithms (178)

Example: Printing the keys of a Binary Search Tree in ascending order 10 22 40 7 10 7 21 51 40 21 51 22 Data Structures and Algorithms (179)

Inorder tree walk Inorder_Tree_Walk prints the elements of a binary search tree in ascending order Inorder_Tree_Walk(x) if x!=nil then Inorder_Tree_Walk(left[x]) print(key[x]) Inorder_Tree_Walk(right[x]) For a tree T the initial call is Inorder_Tree_Walk(root[T]) Data Structures and Algorithms (180)

Inorder tree walk If x is the root of a binary (search) tree, the runtime of Inorder_Tree_Walk is Θ(n) Intuitive explanation: after the initial call of Inorder_Tree_Walk the following is true: for each of the (n -1) not-nil nodes of the tree there are exactly two calls of Inorder_Tree_Walk one for its left child and one for its right child (for details see [Corman]) Data Structures and Algorithms (181)

Searching for a node with given key k Recursive algorithm 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) For a tree T the initial call for searching for key k is Inorder_Tree_Walk(root[T],k) Runtime of Tree_Search(x,k) is O(h) where h is the height of the tree Data Structures and Algorithms (182)

Searching for a node with given key k Non-recursive algorithm Iterative_Tree_Search(x,k) while x!= NIL and k!= key[x] do if k<key[x] then x:= left[x] else x:= right[x] return x This non-recursive (iterative) version of tree-search is usually more efficient in practice since it avoids the runtime system overhead for recursion Data Structures and Algorithms (183)

Searching for a node with given key Example Search for k = 31 22 Search for k = 3 10 30 7 15 27 33 2 9 18 31 32 Data Structures and Algorithms (184)

Minimum and maximum The element with minimum key value is always in the leftmost position of the binary search tree (not necessarily a leaf!) Tree_Minimum(x) while left[x]!=nil do // left[x]=nil no smaller element x := left[x] return x Runtime of TreeMinimum and TreeMaximum ist O(h) Remark: The pseudo code TreeMaximum for the maximum is analogous Data Structures and Algorithms (185)

Minimum and maximum Example Search for Minimum 22 10 30 7 15 27 33 2 9 18 31 32 Data Structures and Algorithms (186)

Successor and predecessor If all keys are distinct, the successor (predecessor) of node x in sorted order is the node with the smallest key larger (smaller) than key[x] Tree_Successor(x) if right[x]!=nil then return Tree_Minimum(right[x]) y := p[x] while y!=nil and x=right[y] do x := y y := p[y] return[y] Data Structures and Algorithms (187)

Successor and predecessor Remark: The pseudo code for Tree_Predescessor is analogous Runtime of Tree_Successor or Tree_Predecessor ist O(h) Because of the binary search property, for finding the successor or predecessor it is not necessary to compare the keys: We find the successor or predecessor because of the structure of the tree. So even if keys are not distinct, we define the successor (predecessor) of node x as the nodes returned by Tree_Successor(x) or Tree_Predeccessor(x). Data Structures and Algorithms (188)

First summary On a binary search tree of heigth h, the dynamic-set operations SEARCH, MINIMUM, MAXIMUM, SUCCESSOR and PREDECESSOR can be implemented in time O(h) Data Structures and Algorithms (189)

Insertion and deletion The operations of insertion and deletion change the dynamic set represented by a binary search tree If a new node is inserted into a binary search tree, or if a node is deleted, the structure of the tree has to be modified in such a way that the binary-search-tree property will still hold: A new node z to be inserted has fields left[z] = right[z] = NIL key[z] = v, v - any value The new node is always inserted as a leaf Data Structures and Algorithms (190)

Insertion (pseudo code) Tree_Insert(T,z) y := NIL x := root[t] while x!=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 Runtime of Tree_Insert: O(h) // Tree t was empty Data Structures and Algorithms (191)

Insertion (example) a) Tree_Insert(T,z) (where key(z) = 14) b) Tree_Insert(T,z) (where key(z) = 39) 22 10 30 7 15 27 43 2 18 41 Data Structures and Algorithms (192)

Insertion (example) a) Tree_Insert(T,z) (where key(z) = 14) b) Tree_Insert(T,z) (where key(z) = 39) 22 10 30 7 15 27 43 2 14 18 41 39 Data Structures and Algorithms (193)

Deletion For deleting a node z from a binary search tree we can distinguish three cases z is a leaf z has only one child z has two children If z is a leaf, the leaf is simply deleted If z has only one child, then z is spliced out by making a new link from p[z] to the child of z If z has two children, then we first find its successor y (which has no left child!), then splice out y, and finally replace the contents of z (key and satellite data) by the contents of y. Data Structures and Algorithms (194)

Deletion (pseudo code) Tree_Delete(T,z) if left[z]= NIL or right[z]= NIL then y := z else y := Tree_Successor(z) if left[y]!= NIL then x := left[y] else x := right[y] if x!= NIL then p[x] := p[y] if p[y] = NIL then root[t] := x (go on next page) Data Structures and Algorithms (195)

Deletion (pseudo code) else if y = left[p[y]] then left[p[y]]:= x else right[p[y]]:= x if y!= z then key[z] := key[y] (copy y`s satellite data into z) return y The runtime of Tree_Delete is O(h) Data Structures and Algorithms (196)

Analysis (results) Height of randomly built binary search trees with n nodes Best case: h = lg n Worst case: h = n 1 Average case behaviour: Expected height of a randomly built search tree with n modes = O(lg n) Data Structures and Algorithms (197)

Analysis (results) If the tree is a complete binary tree with n nodes, then the worst-case time is Θ(lg n). If the tree is very unbalanced (i.e. the tree is a linear chain), the worst-case time is Θ(n). Luckily, the expected height of a randomly built binary search tree is O(lg n) basic operations take time Θ(lg n) on average. Data Structures and Algorithms (198)