Binhai Zhu Computer Science Department, Montana State University

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
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)
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
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.
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,
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 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.
Binary Search Tree Qamar Abbas.
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.
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.
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.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
1 CSC 211 Data Structures Lecture 25 Dr. Iftikhar Azim Niaz 1.
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?
BST Trees Saurav Karmakar
AND TELECOMMUNICATIONS BUSINESS
Binary Search Tree (BST)
Analysis of Algorithms
Section 8.1 Trees.
Data Structures & Algorithm Design
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
US Treasury & Its Borrowing
Hashing and Hash Tables
ძებნის ორობითი ხეები BST (Binary Search Trees)
Binhai Zhu Computer Science Department, Montana State University
Heaps,heapsort and priority queue
Binhai Zhu Computer Science Department, Montana State University
Brief Review of Proof Techniques
AOE/ESM 4084 Engineering Design Optimization
Technology Update Kris Young Director of Technology
Ch. 12: Binary Search Trees Ming-Te Chi
Presenting a Technical Report
Numerical Methods Charudatt Kadolkar 12/9/2018
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
Binhai Zhu Computer Science Department, Montana State University
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
2015/16 Evaluation Summary October 4, 2016 Jordan Harris
2016 State Assessment Results
Topic 6: Binary Search Tree Data structure Operations
TROY SCHOOL DISTRICT ENROLLMENT PROJECTIONS February 7, 2017
Chapter 20: Binary Trees.
Design and Analysis of Algorithms
2015/16 Evaluation Summary October 18, 2016 Jordan Harris
Analysis of Algorithms CS 477/677
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
Binhai Zhu Computer Science Department, Montana State University
Business Services Update Board of Education Workshop March 7, 2017
Binhai Zhu Computer Science Department, Montana State University
Binhai Zhu Computer Science Department, Montana State University
Presentation transcript:

Binhai Zhu Computer Science Department, Montana State University Binary search trees Binhai Zhu Computer Science Department, Montana State University Frequently, presenters must deliver material of a technical nature to an audience unfamiliar with the topic or vocabulary. The material may be complex or heavy with detail. To present technical material effectively, use the following guidelines from Dale Carnegie Training®.   Consider the amount of time available and prepare to organize your material. Narrow your topic. Divide your presentation into clear segments. Follow a logical progression. Maintain your focus throughout. Close the presentation with a summary, repetition of the key steps, or a logical conclusion. Keep your audience in mind at all times. For example, be sure data is clear and information is relevant. Keep the level of detail and vocabulary appropriate for the audience. Use visuals to support key points or steps. Keep alert to the needs of your listeners, and you will have a more receptive audience. 7/4/2019

Definition Binary search tree: (1) Each node, besides a key field and some satellite data, contains left, right, and p pointers that point to its left child, its right child and its parent. (2) The root is the only node whose parent field is NIL. Of course, all the leaves nodes have both NIL left field and NIL right field. 7/4/2019

Example root 10 7 14 9 16 5 leaf 2 6 8 leaf leaf leaf 7/4/2019

Operations Search 10 7 14 9 16 5 2 6 8 7/4/2019

Operations Search Minimum Maximum Predecessor Successor Insert Delete 10 7 14 9 16 5 2 6 8 7/4/2019

Binary search tree property 10 7 14 9 16 5 2 6 8 For any node x, let y be a node in the left tree of x, then key[y] ≤ key[x]. Likewise, if y is a node in the right subtree of x then key[x]≤key[y]. 7/4/2019

Binary search tree property 10 x 7 14 9 16 5 z y 2 6 8 For any node x, let y be a node in the left tree of x, then key[y] ≤ key[x]. Likewise, if z is a node in the right subtree of x then key[x]≤key[z]. 7/4/2019

Tree traversals Inorder Preorder Postorder 10 7 14 9 16 5 2 6 8 7/4/2019

Tree traversals Inorder(node x) If x ≠ NIL Inorder(x→left) print(x) Inorder(x→right) 10 7 14 9 16 5 2 6 8 7/4/2019

Tree traversals Inorder(node x) If x ≠ NIL Inorder(x→left) print(x) Inorder(x→right) 10 7 14 9 16 5 2 6 8 2,5,6,7,8,9,10,14,16 7/4/2019

Tree traversals Inorder(node x) If x ≠ NIL Inorder(x→left) print(x) Inorder(x→right) 10 7 14 9 16 5 2 6 8 2,5,6,7,8,9,10,14,16 (that’s exactly the sorted ordering!) 7/4/2019

Tree traversals Preorder(node x) If x ≠ NIL print(x) Preorder(x→left) Preorder(x→right) 10 7 14 9 16 5 2 6 8 7/4/2019

Tree traversals Preorder(node x) If x ≠ NIL print(x) Preorder(x→left) Preorder(x→right) 10 7 14 9 16 5 2 6 8 10,7,5,2,6,9,8,14,16 7/4/2019

Tree traversals Postorder(node x) If x ≠ NIL Postorder(x→left) Postorder(x→right) print(x) 10 7 14 9 16 5 2 6 8 7/4/2019

Tree traversals Postorder(node x) If x ≠ NIL Postorder(x→left) Postorder(x→right) print(x) 10 7 14 9 16 5 2 6 8 2,6,5,8,9,7,16,14,10 7/4/2019

Tree traversals What is the running time? 10 7 14 9 16 5 2 6 8 7/4/2019

Minimum and Maximum 10 7 14 9 16 5 2 6 8 7/4/2019

Minimum and Maximum 10 Minimum(node x) while x → left ≠ NIL do x ← x→left return x 7 14 9 16 5 2 6 8 7/4/2019

Minimum and Maximum 10 Minimum(node x) while x → left ≠ NIL do x ← x→left return x Maximum(node x) while x → right ≠ NIL do x ← x→right 7 14 9 16 5 2 6 8 7/4/2019

Search x 10 7 14 9 16 5 k=11? 2 6 8 k=6 7/4/2019

Search Search(node x, k) if x = NIL or k =key[x] then return x if x < key[x] then return Search(x→left,k) else return Search(x→right,k) x 10 7 14 9 16 5 2 6 8 k=6 7/4/2019

Search Search(node x, k) if x = NIL or k =key[x] then return x if x < key[x] then return Search(x→left,k) else return Search(x→right,k) Iterative-Search(node x,k) while x≠NIL and k≠key[x] if k < key[x] then x ← x→left else x ← x→right return x x 10 7 14 9 16 5 2 6 8 k=6 7/4/2019

Successor The successor of x is the node with the smallest key greater than key[x]. 10 7 x 14 9 16 5 2 6 8 7/4/2019

Successor Successor(node x) if x→right ≠ NIL then return Minimum(x→right) y ← x→p while y≠NIL and x==y→right x ← y y ← y→p return y Successor 10 7 14 9 16 5 x 2 6 8 7/4/2019

Successor Successor(node x) if x→right ≠ NIL then return Minimum(x→right) y ← x→p while y≠NIL and x==y→right x ← y y ← y→p return y Successor 10 7 14 9 16 5 x 2 6 8 Search, Minimum, Maximum, Successor all run in O(h) time, where h is the height of the corresponding binary search tree. 7/4/2019

Insertion Insert a new node z with key[z]=v into a tree T. 10 7 14 9 16 5 2 6 8 9.5 z 7/4/2019

Insertion Insert(T,z) y ← NIL x ← root(T) While x ≠ NIL y ← x if key[z] < key[x] then x ← x→left else x ← x→right z→p ← y x 10 7 14 9 16 5 9.5 2 6 8 z 7/4/2019

Insertion Insert(T,z) y ← NIL x ← root(T) While x ≠ NIL y ← x if key[z] < key[x] then x ← x→left else x ← x→right z→p ← y y x 10 7 14 9 16 5 2 6 8 9.5 z 7/4/2019

Insertion Insert(T,z) y ← NIL x ← root(T) While x ≠ NIL y ← x if key[z] < key[x] then x ← x→left else x ← x→right z→p ← y y 10 7 x 14 9 16 5 2 6 8 9.5 z 7/4/2019

Insertion Insert(T,z) y ← NIL x ← root(T) While x ≠ NIL y ← x if key[z] < key[x] then x ← x→left else x ← x→right z→p ← y 10 y 7 x 14 9 16 5 2 6 8 9.5 z 7/4/2019

Insertion Insert(T,z) y ← NIL x ← root(T) While x ≠ NIL y ← x if key[z] < key[x] then x ← x→left else x ← x→right z→p ← y 10 y 7 14 9 x 16 5 2 6 8 9.5 z 7/4/2019

Insertion Insert(T,z) y ← NIL x ← root(T) While x ≠ NIL y ← x if key[z] < key[x] then x ← x→left else x ← x→right z→p ← y 10 7 14 y 9 16 5 x←NIL 9.5 2 6 8 z 7/4/2019

Insertion Insert(T,z) y ← NIL x ← root(T) While x ≠ NIL y ← x if key[z] < key[x] then x ← x→left else x ← x→right z→p ← y 10 7 14 y 9 16 5 9.5 2 6 8 z 7/4/2019

Insertion Insert(T,z) y ← NIL x ← root(T) While x ≠ NIL y ← x if key[z] < key[x] then x ← x→left else x ← x→right z→p ← y If y = NIL then root[T]←z else if key[z] < key [y] then y→left ← z else y→right ← z 10 7 14 y 9 16 5 9.5 2 6 8 z 7/4/2019

Deletion 10 z z 7 14 9 16 5 2 6 8 z 7/4/2019

Deletion 10 7 14 9 16 5 2 6 8 z 7/4/2019

Deletion 10 7 14 9 16 5 X 2 6 8 z 7/4/2019

Deletion 10 z 7 14 9 16 5 2 6 8 7/4/2019

Deletion 10 X z 7 14 X 9 16 5 2 6 8 7/4/2019

Deletion 10 z 7 14 9 16 5 2 6 8 7/4/2019

Deletion 10 z 7 14 9 16 5 2 6 8 Find the successor of z 7/4/2019

Deletion 10 z 8 14 9 16 5 2 6 8 Find the successor y of z Replace z with y Delete y (careful, as y might have a right child) 7/4/2019

1. if z→left=NIL or z→right=NIL then y ← z else y ← Successor(z) 4. if y→left≠NIL then x ← y→left else x ← y→right 7. if x ≠ NIL then x→p ← y→p 9. if y→p = NIL 10. then root[T] ← x else if y = (y→p)→left then (y→p)→left ← x else (y→p)→right ← x 14.if y≠z then key[z] ← key[y] copy y’s data into z 17.return y Deletion 10 z 7 z 14 9 16 5 2 6 8 z 8.5 7/4/2019

1. if z→left=NIL or z→right=NIL then y ← z else y ← Successor(z) 4. if y→left≠NIL then x ← y→left else x ← y→right 7. if x ≠ NIL then x→p ← y→p 9. if y→p = NIL 10. then root[T] ← x else if y = (y→p)→left then (y→p)→left ← x else (y→p)→right ← x 14.if y≠z then key[z] ← key[y] copy y’s data into z 17.return y Deletion 10 z 7 z 14 y 9 16 5 2 6 8 y z y 8.5 7/4/2019

Deletion 10 z 7 z 14 y 9 x 16 5 2 6 8 y z y x 8.5 x=NIL 1. if z→left=NIL or z→right=NIL then y ← z else y ← Successor(z) 4. if y→left≠NIL then x ← y→left else x ← y→right 7. if x ≠ NIL then x→p ← y→p 9. if y→p = NIL 10. then root[T] ← x else if y = (y→p)→left then (y→p)→left ← x else (y→p)→right ← x 14.if y≠z then key[z] ← key[y] copy y’s data into z 17.return y Deletion 10 z 7 z 14 y 9 x 16 5 2 6 8 y z y x 8.5 x=NIL 7/4/2019

Deletion 10 z 7 z 14 y 9 x 16 5 2 6 8 y z y x 8.5 x=NIL 1. if z→left=NIL or z→right=NIL then y ← z else y ← Successor(z) 4. if y→left≠NIL then x ← y→left else x ← y→right 7. if x ≠ NIL then x→p ← y→p 9. if y→p = NIL 10. then root[T] ← x else if y = (y→p)→left then (y→p)→left ← x else (y→p)→right ← x 14.if y≠z then key[z] ← key[y] copy y’s data into z 17.return y Deletion 10 z 7 z 14 y 9 x 16 5 2 6 8 y z y x 8.5 x=NIL 7/4/2019

1. if z→left=NIL or z→right=NIL then y ← z else y ← Successor(z) 4. if y→left≠NIL then x ← y→left else x ← y→right 7. if x ≠ NIL then x→p ← y→p 9. if y→p = NIL 10. then root[T] ← x else if y = (y→p)→left then (y→p)→left ← x else (y→p)→right ← x 14.if y≠z then key[z] ← key[y] copy y’s data into z 17.return y Deletion 10 X z 7 z 14 y X 9 x 16 5 X 2 6 8 y 8.5 x 7/4/2019

1. if z→left=NIL or z→right=NIL then y ← z else y ← Successor(z) 4. if y→left≠NIL then x ← y→left else x ← y→right 7. if x ≠ NIL then x→p ← y→p 9. if y→p = NIL 10. then root[T] ← x else if y = (y→p)→left then (y→p)→left ← x else (y→p)→right ← x 14.if y≠z then key[z] ← key[y] copy y’s data into z 17.return y Deletion 10 z 7 z 14 y 9 x 16 5 2 6 8 y 8.5 x 7/4/2019

1. if z→left=NIL or z→right=NIL then y ← z else y ← Successor(z) 4. if y→left≠NIL then x ← y→left else x ← y→right 7. if x ≠ NIL then x→p ← y→p 9. if y→p = NIL 10. then root[T] ← x else if y = (y→p)→left then (y→p)→left ← x else (y→p)→right ← x 14.if y≠z then key[z] ← key[y] copy y’s data into z 17.return y Deletion 10 z 8 z 14 y 9 x 16 5 2 6 8 y 8.5 x 7/4/2019

1. if z→left=NIL or z→right=NIL then y ← z else y ← Successor(z) 4. if y→left≠NIL then x ← y→left else x ← y→right 7. if x ≠ NIL then x→p ← y→p 9. if y→p = NIL 10. then root[T] ← x else if y = (y→p)→left then (y→p)→left ← x else (y→p)→right ← x 14.if y≠z then key[z] ← key[y] copy y’s data into z 17.return y Deletion 10 z 8 z y 9 x 16 5 2 6 y 8.5 x 7/4/2019