CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
The complexity and correctness of algorithms (with binary trees as an example)
UNC Chapel Hill Lin/Foskey/Manocha Binary Search Tree Her bir node u bir object olan bir linked data structure ile temsil edilebilir. Her bir node key,
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Search Trees Comp 550.
CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.
Binary Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Binary Search Tree Qamar Abbas.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Binary Search Trees … From
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Binary Search Trees What is a binary search tree?
Design & Analysis of Algorithm n-ary Tree & Binary Tree
CS200: Algorithms Analysis
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Ch. 12: Binary Search Trees Ming-Te Chi
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Binary SearchTrees [CLRS] – Chap 12.
Design and Analysis of Algorithms
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees
Binhai Zhu Computer Science Department, Montana State University
Presentation transcript:

CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations

CSE 2331/5331 Set Operations Maximum Extract-Max Insert Increase-key Search Delete Successor Predecessor We can use priority queue (implemented by heap) Not efficient with heap! Linked list? Array?

CSE 2331/5331 Search Tree Support all of previous operations Time proportional to tree height Can be used to implement priority queues Different types of search trees Binary search tree, red-black trees, etc

CSE 2331/5331 Binary Search Tree First, it is a binary tree Represented by a linked data structure Each node contains at least fields: Key Left Right Parent Nil if does not exists

CSE 2331/5331 Binary Search Tree cont. Secondly, the binary-search-tree property For any node x, Key[ y ] ≤ Key[x] if y in left subtree of x Key[ x ] ≤ Key[ y] if y in right subtree of x

CSE 2331/5331 Example

CSE 2331/5331 Properties Not unique for the same set of elements Minimum Does it have to be a leaf ? Maximum

CSE 2331/5331 Query A Tree Tree-search ( x, k) Given a node x and a key k, returns a node y in the subtree rooted at x if y.key = k if exists, Nil otherwise Tree-search ( x, k) if x = Nil or k = x.key then return x if k < x.key then return Tree-search( x.left, k ) else return Tree-search( x.right, k ) Time complexity: O(h), where h is height of tree.

Iterative Tree Search CSE 2331/5331

Example Binary search tree on the same set of numbers. Which tree is better?

Exercise CSE 2331/5331 Assign the following numbers so that it becomes a binary search tree: 8, 12, 16, 17, 20, 24, 31, 35, 42 Where should we put 8? Where 12 ? Where 42 ? Where 35 ?

Inorder Tree Walk CSE 2331/5331

Inorder Tree Walk Inorder tree walk / transversal Provide a way to visit all nodes in the tree Visit tree nodes in sorted order ! During binary-search tree property. Running time: Preorder, postorder CSE 2331/5331

More Properties For any subtree rooted at x Minimum Maximum For any subtree rooted at x Inorder-tree-walk (x) produces a subsequence of Inorder-tree-walk (root) Let y be an ancestor of x Relations between Key[y] and Key[x]

CSE 2331/5331 Minimum / Maximum Tree-minimum(x) while ( x.left ≠ Nil) do x = x.left; return x; Tree-maximum(x) while ( x.right ≠ Nil) do x = x.right; return x; Time: O(h)

CSE 2331/5331 Successor Successor(x) The node y with the smallest key greater than or equal to x.key Ambiguous when multiple nodes have same key Successor in the inorder tree walk Return Nil if none exists -- has largest key

CSE 2331/5331 Example

CSE 2331/5331 Two Cases If right child of x exists Leftmost node in right subtree Otherwise Lowest ancestor y where x is in the left-subtree of y Tree-successor(x) if x.right ≠ Nil then return Tree-minimum(x.right) y = x.parent while y ≠ Nil and x = y.right do x = y y = y.parent return y Time: O(h) why?

CSE 2331/5331 Predecessor (x) Two cases The left child of x exists Rightmost child in the left subtree of x Otherwise Lowest ancestor y where x is in the right subtree of y. Completely symmetric

Complete Binary Tree A complete binary tree is a binary search tree: all internal nodes have exactly two children all leaves are at the same distance from the root CSE 2331/5331

Balanced Binary Trees CSE 2331/5331

Summary Using binary search tree Traversal (inorder tree walk) Min / max Search Successor / predecessor All (other than traversal) have running time O(h) h is height of the tree. CSE 2331/5331 We could use a sorted array to do all these in efficiently! However, array is not efficient in dynamic updates.

CSE 780 Algorithms Tree-Insert(T, z) Insert z into tree T, and resulting tree still binary search tree Tree-Insert(T, 8) Use Tree-search !

CSE 780 Algorithms Pseudocode Tree-insert(T, z) y = Nil, x = root[T] while (x ≠ Nil) do y = x if ( z.key < x.key ) then x = x.left else x = x.right z.parent = y if (y = Nil) then root[T] = z else if (z.key < y.key) theny.left = z else y.right = z Time: O(h) Locate potential parent y of z.

CSE 780 Algorithms Build Binary Search Tree

CSE 780 Algorithms Deletion delete 4 ?delete 17 ?delete 7 ?delete 3 ?delete 6 ?delete 15 ?

CSE 780 Algorithms Three Cases for Deleting z Case 1: Leaf node Case 2: Has only one child Replace z with its child Case 3: Has both children Find its successor y y cannot have left child! replacing y with its right child! Replace z with y The “replacement’’ is implemented by the transplant operation

Transplant Operation CSE 2331/5331

Case 3: More Detailed View Goal: delete node z Let y be the successor of z Case-(3.1) y is the right child of z Then replace z by y CSE 2331/5331

Case 3: cont. Case (3.b): y is not the right child of z First replace y by its own right child, and then replace z by y. CSE 2331/5331

Delete CSE 2331/5331 together cover Cases 1 & 2. Time complexity: O(h)

An Exercise CSE 2331/5331 Delete I ? Delete G ? Delete K ? Delete B ?

CSE 780 Algorithms Remarks All complexity depends on height h h =  (lg n), h =  (n) To guarantee performance: Balanced tree ! Randomly build a tree Expected height is O(lg n) Still problem: future insertions …