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.

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.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
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.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
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,
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.
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.
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.
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.
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
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 5 1. Binary search tree sort 2.
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 (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.
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.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
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?
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Analysis of Algorithms
CS200: Algorithms Analysis
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Ch. 12: Binary Search Trees Ming-Te Chi
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
Binary SearchTrees [CLRS] – Chap 12.
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Presentation transcript:

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 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 (ch.13), and B-trees(ch.18).

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. We represent a binary tree by a linked list data structure in which each node is an object. root[T ] points to the root of tree T. 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. –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].

The binary-search-tree property allows us to print keys in a binary search tree in order, recursively, using an algorithm called an inorder tree walk. Elements are printed in monotonically increasing order. 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.

Preorder and Postorder Tree Walk PREORDER-TREE-WALK(x) if x = NIL then print key[x] PRE-ORDER-TREE-WALK(left[x]) PRE-ORDER-TREE-WALK(right[x]). POSTORDER-TREE-WALK(x) if x = NIL then POST-ORDER-TREE-WALK(left[x]) POST-ORDER-TREE-WALK(right[x]). print key[x]

Querying a binary search tree

Initial call: TREE-SEARCH(root[T ], k). 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.

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 run in O(h) time, where h is the height of the tree. -- Both visit nodes that form a downward path from the root to a leaf.

Successor and Predecessor Assumption: all keys are distinct. The successor of a node x is the node y such that key[y] is the smallest key > key[x]. –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 y ( x is the maximum in y ’s left subtree).

TREE-PREDECESSOR is symmetric to TREE-SUCCESSOR.

Example: Find the successor of the node with key value 15. (Answer: Key value 17) Find the successor of the node with key value 6. (Answer: Key value 7) Find the successor of the node with key value 4. (Answer: Key value 6) Find the predecessor of the node with key value 6. (Answer: Key value 4) Time: Running time is O(h), where h is the height of the tree. 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

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.

To insert value v into the binary search tree, the procedure is given node z, with key[z] = v, left[z] = NIL, and right[z] = NIL. Beginning at root of the tree, trace a downward path, maintaining two pointers. –Pointer x : traces the downward path. –Pointer y : “trailing pointer” to keep track of parent of x. Traverse the tree downward by comparing the value of node at x with v, and move to the left or right child accordingly. When x is NIL, it is at the correct position for node z. Compare z ’s value with y ’s value, and insert z at either y ’s left or right, appropriately. 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. Insertion

TREE-DELETE is broken into three cases. Case 1: z has no children. –Delete z by making the parent of z point to NIL, instead of to z. Case 2: z has one child. –Delete z by making the parent of z point to z ’s child, instead of to z. Case 3: z has two children. –z ’s successor y has either no children or one child. ( y is the minimum node—with no left child—in z ’s right subtree.) –Delete y from the tree (via Case 1 or 2). –Replace z ’s key and data with y ’s. Example: Demonstrate on the above sample tree. –For Case 1, delete K. –For Case 2, delete H. –For Case 3, delete B, swapping it with C. Time: O(h), on a tree of height h. Deletion F BH ADK C

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) like “plain” binary search trees. – chap. 13.