Ch. 12: Binary Search Trees Ming-Te Chi

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.
Introduction to Algorithms Jiafen Liu Sept
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.
Binary Search Trees Comp 550.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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,
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.
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.
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.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search 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.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
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?
BCA-II Data Structure Using C
Binary Search Trees.
Data Structures Review Session 2
Binary Search Tree.
CS200: Algorithms Analysis
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Elementary Data Structures
Ch. 12: Binary Search Trees Ming-Te Chi
Ch. 12: Binary Search Trees
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
Binary Trees, Binary Search Trees
Topic 6: Binary Search Tree Data structure Operations
Binary SearchTrees [CLRS] – Chap 12.
Design and Analysis of Algorithms
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees
Binary Trees, Binary Search Trees
Binhai Zhu Computer Science Department, Montana State University
Presentation transcript:

Ch. 12: Binary Search Trees Ming-Te Chi Algorithms Ch. 12: Binary Search Trees Ming-Te Chi Ch12 Binary Search Trees

12.0 What is a binary tree? Binary tree: Empty, or Composed by 3 disjoint set of nodes: root left subtree (binary tree), could be empty. right subtree (binary tree ), could be empty. Ch12 Binary Search Trees

12.1 What is a binary search tree? Binary tree with the binary-search property. Binary-search property: Let x be a node in a binary search 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]. Ch12 Binary Search Trees

Binary search Tree Ch12 Binary Search Trees

Tree traversal Preorder Inorder Postorder Ch12 Binary Search Trees

Inorder tree walk (traversal) INORDER_TREE_WALK(x) 1 if 2 INORDER_TREE_WALK(x.left) 3 print x.key[x] 4 INORDER_TREE_WALK(x.right) Ch12 Binary Search Trees

Theorem 12.1 If x is the root of an n-node subtree, then the call INORDER-TREE-WALK(x) takes (n) time. Proof: Divide and conquer (recurrence) Substitution method. Ch12 Binary Search Trees

Preorder tree walk Postorder tree walk Ch12 Binary Search Trees

12.2 Querying a binary search tree Ch12 Binary Search Trees

TREE_SEARCH(x, k) TREE_SEARCH(x, k) 1 if or 2 return x 3 if 4 return TREE_SEARCH(x.left, k) 5 else return TREE_SEARCH(x.right,k) Ch12 Binary Search Trees

ITERATIVE_SEARCH(x, k) 1 While and 2 do if 3 then 4 else 5 return x (more efficient) Ch12 Binary Search Trees

Q & A What is the complexity of TREE-SEARCH(x,k)? Ch12 Binary Search Trees

MAXIMUM and MINIMUM TREE_MINIMUM(x) TREE_MAXIMUM(x) 1 while x.left  NIL 2 x = x.left 3 return x TREE_MAXIMUM(x) 1 while x.right  NIL 2 x = x.right Ch12 Binary Search Trees

Q & A What is the complexity of TREE-MINIMUM & TREE-MAXIMUM? Ch12 Binary Search Trees

Successor and predecessor The successor of a node x is the smallest key greater than key[x]. Predecessor: The predecessor of a node x is the (write your own definition.) Ch12 Binary Search Trees

TREE_SUCCESSOR Two cases: Right subtree is empty: (Node 15, X=13) Right subtree is not empty: Minimum element in the right subtree. (node 17, when x=15) Ch12 Binary Search Trees

TREE_SUCCESSOR Right subtree is empty: (the successor is the lowest ancester of x whose left child is also an ancestor of x) Go up the tree from x until we see a node that is the left child of its parent. Remarks: x has no parent (x is the root), no successor. (no element is greater then x) x has parent: x is the left child of its parent, the parent is the successor x is the right child of its parent (x >= parent[x]), moves up one level. (ref. Figure 12.2 on page 257, node 13.) Ch12 Binary Search Trees

TREE_SUCCESSOR TREE_SUCCESSOR 1 if 2 then return TREE_MINIMUM(x.right) 3 4 while and 5 6 7 return y Ch12 Binary Search Trees

Theorem 12.2 The dynamic-set operations, SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR can be made to run in O(h) time on a binary search tree of height h. Ch12 Binary Search Trees

12.3 Insertion and deletion The dynamic-set operations, INSERT and DELETE change the structure of the binary-search-tree. The property of the BST remains. left[x] <= x <= right[x] O(h) operations ? Ch12 Binary Search Trees

Tree-Insert(T, z) 1 y = NIL  trace the parent of x (denoted by y) 2 x = T.root  trace the path 3 while x  NIL  moves down until x is set to NIL 4 y = x 5 if z.key < x.key 6 x = x.left 7 else x = x.right 8 z.p = y  z is inserted at x (p[z] is set to y) 9 if y = NIL 10 T.root[T] = z  tree T was empty 11 else if z.key < y.key 12 y.left = z 13 else y.right = z Ch12 Binary Search Trees

Inserting an item with key 13 into a binary search tree Ch12 Binary Search Trees

Deletion: DELETE(T, z) 3 cases z has no child. (simple) Set (P[z])’s z-child to NIL z has just one child. (left or right) Spice out z z has both a left and a right child: find z’s successor y Spice out z’s successor y (y has no left child) Replace z’s data by y’s data Ch12 Binary Search Trees

DELETE(T,z) - reorganization 3 cases z has no left child. Replace z by its right child z has just one child, which is its left child Replace z by its left child z has both a left and a right child, find z’s successor y 1. If y is z’s right child, replace z by y 2. Otherwise, y lies within z’s right subtree, but is not ’’s right child. Replace y, by its own right child, and then we replace z by y. Ch12 Binary Search Trees

z has no left child Replace z by its right child Ch12 Binary Search Trees

z has just one child, which is its left child Replace z by its left child Ch12 Binary Search Trees

z has two children find z’s successor y If y is z’s right child, replace z by y Ch12 Binary Search Trees

z has two children Otherwise, y lies within z’s right subtree, but is not z’s right child. Replace y, by its own right child, and then we replace z by y. Ch12 Binary Search Trees

TRANSPLANT Transplant(T, z, y) Ch12 Binary Search Trees

Ch12 Binary Search Trees

Theorem 12.3 The dynamic-set operations, INSERT and DELETE can be made to run in O(h) time on a binary search tree of height h. Ch12 Binary Search Trees

Remarks on binary-search-tree All basic operations take time proportional to the height of the tree. A complete binary tree with n nodes, the height is log n, the operations runs in O(log n) worst-case time. Complete tree: all leaves have the same depth and all internal nodes have the same degree. If the tree is linear chain of n nodes, the same operations take O(n) worst-case time. (No better than linked list) Ch12 Binary Search Trees

Remarks on binary-search-tree Expected height of randomly build binary search tree is O(log n). (ref. §12.4) In real life, randomly build binary search tree can not be expected. Variations of binary search tree could achieve this O(log n) height. Red-black tree is one of the examples. Ch12 Binary Search Trees