Binary Search Trees CIS 606 Spring 2010. Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.

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,
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.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
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 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU.
Red-Black Trees Lecture 10 Nawazish Naveed. Red-Black Trees (Intro) BSTs perform dynamic set operations such as SEARCH, INSERT, DELETE etc in O(h) time.
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.
Elementary Data Structures Data Structures and Algorithms A. G. Malamos.
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.
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
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 5 1. Binary search tree sort 2.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
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 (BST)
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.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk.
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,
Binary Search Trees What is a binary search tree?
Insertion/Deletion in binary trees
Binary search tree. Removing a node
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
CS200: Algorithms Analysis
Lecture 7 Algorithm Analysis
Lecture 7 Algorithm Analysis
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
Binary SearchTrees [CLRS] – Chap 12.
Design and Analysis of Algorithms
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Presentation transcript:

Binary Search Trees CIS 606 Spring 2010

Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and as a priority queue. – Basic operations take time proportional to the height of the tree. – For complete binary tree with n nodes: worst case θ(lg n). – For linear chain of n nodes: worst case θ(n). – Different types of search trees include binary search trees, red-black trees (covered in Chapter 13), and B-trees (covered in Chapter 18). We will cover binary search trees, tree walks, and operations on binary search trees.

Binary search trees Binary search trees are an important data structure for dynamic sets. – Accomplish many dynamic-set operations in O(h) time, where h = height of tree. – As in Section 10.4, we represent a binary tree by a linked data structure in which each node is an object. – T.root points to the root of tree T. – Each node contains the attributes key (and possibly other satellite data). left: points to left child. right: points to right child. p: points to parent. T.root.p = NIL. – Stored keys must satisfy the binary-search-tree property. If y is in left subtree of x, then y.key ≤ x.key. If y is in right subtree of x, then y.key ≥ x.key.

Binary search trees

Example Correctness: Follows by induction directly from the binary-search-tree property. Time: Intuitively, the walk takes Θ(n) time for a tree with n nodes, because we visit and print each node once.

Querying a binary search tree Time: The algorithm recurses, visiting nodes on a downward path from the root. Thus, running time is O(h), where h is the height of the tree.

Maximum and minimum

Successor and predecessor Assuming that all keys are distinct, the successor of a node x is the node y such that y.key is the smallest key > x.key. (We can find x’s successor based entirely on the tree structure. No key comparisons are necessary.) If x has the largest key in the binary search tree, then we say that x’s successor is NIL. There are two cases: 1.If node x has a non-empty right subtree, then x’s successor is the minimum in x’s right subtree. 2.If node x has an empty right subtree, notice that: As long as we move to the left up the tree (move up through right children), we’re visiting smaller keys. x’s successor y is the node that x is the predecessor of (x is the maximum in y’s left subtree).

Successor and predecessor

Example Find the successor of the node with key value 15. Find the successor of the node with key value 6. Find the successor of the node with key value 4. Find the predecessor of the node with key value 6.

Time For both the TREE-SUCCESSOR and TREE- PREDECESSOR procedures, in both cases, we visit nodes on a path down the tree or up the tree. Thus, running time is O(h), where h is the height of the tree.

Insertion and deletion Insertion and deletion allows 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.

Insertion and deletion

Example Run TREE-INSERT(T, C) on the first sample binary search tree.

Example Result

Time Same as TREE-SEARCH. On a tree of height h, procedure takes O(h) time. TREE-INSERT can be used with INORDER-TREE- WALK to sort a given set of numbers.

Deletion Conceptually, deleting node z from binary search tree T has three cases: 1.If z has no children, just remove it. 2.If z has just one child, then make that child take z’s position in the tree, dragging the child’s subtree along. 3.If z has two children, then find z’s successor y and replace z by y in the tree. y must be in z’s right subtree and have no left child. The rest of z’s original right subtree becomes y’s new right subtree, and z’s left subtree becomes y’s new left subtree. – This case is a little tricky because the exact sequence of steps taken depends on whether y is z’s right child.

Deletion – The code organizes the cases a bit differently. Since it will move subtrees around within the binary search tree, it uses a subroutine, TRANSPLANT, to replace one subtree as the child of its parent by another subtree.

Deletion

Example

Time O(h), on a tree of height h. Everything is O(1) except for the call to TREEMINIMUM.

Minimizing running time We’ve been analyzing running time in terms of h (the height of the binary search tree), instead of n (the number of nodes in the tree). – Problem: Worst case for binary search tree is Θ(n)—no better than linked list. – Solution: Guarantee small height (balanced tree) — h = O(lg n). In later chapters, by varying the properties of binary search trees, we will be able to analyze running time in terms of n. – Method: Restructure the tree if necessary. Nothing special is required for querying, but there may be extra work when changing the structure of the tree (inserting or deleting). Red-black trees are a special class of binary trees that avoids the worst-case behavior of O(n) that we can see in “plain” binary search trees. Red-black trees are covered in detail in Chapter 13.