Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU.

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.
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.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
COMP 171 Data Structures and Algorithms Tutorial 6 Binary Search Trees.
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.
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.
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.
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 5 1. Binary search tree sort 2.
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.
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.
1 CSC 211 Data Structures Lecture 25 Dr. Iftikhar Azim Niaz 1.
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.
Recursive Objects (Part 4)
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
CS200: Algorithms Analysis
Design and Analysis of Algorithms
Binary Trees, Binary Search Trees
Binary Search Trees.
Ch. 12: Binary Search Trees Ming-Te Chi
Ch. 12: Binary Search Trees Ming-Te Chi
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Topic 6: Binary Search Tree Data structure Operations
Binary SearchTrees [CLRS] – Chap 12.
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees
Presentation transcript:

Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU

Operations on a binary search tree SEARCH(S, k) MINIMUM(S) MAXIMUM(S) SUCCESSOR(S, x) PREDECESSOR(S, x) INSERT(S, x) DELETE(S, x) O(h) h is the height of the tree O(h)

What is a binary search tree? A binary tree Binary-search-tree property – For each node, all the nodes in its left sub tree is smaller than to or equal to this node; all the nodes in its right sub tree is larger than or equal to this node What the difference between “binary search tree” and a “max-heap”? Not a complete binary tree With a different tree property

What is a binary search tree? Yes

What is a binary search tree? Yes

What is a binary search tree? No

Elements in a binary search tree Can we use an array to represent a binary search tree? – No – So some tree structure information has to be stored

Elements in a binary search tree binary search tree node { –K–Key –S–Satellite data –L–Left node (left) –R–Right node (right) –P–Parent node (p) }

p left right p left right p left right p left right p left right p left right p left right p left right NIL Operations are based on this structure

Print all keys in sorted order Preorder tree walk Inorder tree walk Postorder tree walk

Print all keys in sorted order Preorder-tree-walk (node x){ – If(x==NIL) return; – Access(x); – Preorder-tree-walk(x.left); – Preorder-tree-walk(x.right) }

Print all keys in sorted order Preorder-tree-walk ( );

Print all keys in sorted order Inorder-tree-walk (node x){ – If(x==NIL) return; – Inorder-tree-walk( x.left); – Access(x); – Inorder-tree-walk( x.right) }

Print all keys in sorted order Inorder-tree-walk ( );

Print all keys in sorted order Postorder-tree-walk (node x){ – If(x==NIL) return; – Postorder-tree-walk( x.left); – Postorder-tree-walk( x.right) – Access(x); }

Print all keys in sorted order Postorder-tree-walk ( );

Print all keys in sorted order Preorder tree walk Inorder tree walk Postorder tree walk

Print all keys in sorted order Preorder tree walk Inorder tree walk Postorder tree walk Sorted order from inorder tree walk!

Print all keys in sorted order

Searching in a binary search tree TREE-SEARCH Input: root pointer (x), key (k) Output: a element whose key is the same as the input key; NIL if no element has the input key 1.if(x==NIL or x.key==k) return x; 2.if(k<x.key) return TREE-SEARCH(x.left, k); 3.Return TREE-SEARCH(x.right, k);

11 Searching in a binary search tree TREE-SEARCH(, 11) 11

Searching in a binary search tree TREE-SEARCH(, 11) 11

8 Searching in a binary search tree TREE-SEARCH(, 8) 11 TREE-SEARCH(, 8) 9 8

Searching in a binary search tree TREE-SEARCH(, 20) 11 TREE-SEARCH(, 20) 12 TREE-SEARCH(, 20) 12 NIL TREE-SEARCH( NIL, 20) NIL Means there is no such a node in the tree

Searching in a binary search tree TREE-SEARCH(, 20) 30 Illegal, but never happen if start from the root

Searching in a binary search tree TREE-SEARCH(, 2) 11 What’s the worst case? Worst successful search Worst unsuccessful search TREE-SEARCH(, 1) 11 O(h)

Searching in a binary search tree Iterative code could be more efficient than recursive code TREE-SEARCH (x, k) 1.if(x==NIL or x.key==k) return x; 2.if(k<x.key) return TREE-SEARCH(x.left, k); 3.Return TREE-SEARCH(x.right, k); TREE-SEARCH (x, k) 1.current=x; 2.While (current!=NIL and current.key!=k){ if(x.key<k) current=x.left; else current=x.right; } 3.return current;

8 Searching in a binary search tree TREE-SEARCH(, 8) 11 TREE-SEARCH (x, k) 1.current=x; 2.while (current!=NIL and current.key!=k){ if(x.key<k) current=x.left; else current=x.right; } 3.return current;

Searching in a binary search tree TREE-SEARCH(, 20) 11 NIL

Minimum and maximum As a human, can you tell where is the minima and maxima?

Minimum and maximum The minima of the tree rooted at x is: the minima of x.left if x.left is not NIL; x if x.left is NIL TREE-MINIMUM( x ) //the recursive one 1. if (x.left==NIL) return x; 2. return TREE-MINIMUM(x.left); 0. if(x==NIL) return NIL; It has some cost, so if x is guaranteed not NIL we can remove it

Minimum and maximum TREE-MINIMUM( ) //recursive 11 2

Minimum and maximum The minima of the tree rooted at x is: the leftmost node TREE-MINIMUM( x ) //the iterative one 1.current = x; 2.while(current.left!=NIL) current = current.left; 3.return current; 0. if(x==NIL) return NIL;

2 Minimum and maximum TREE-MINIMUM( ) //iterative 11

Minimum and maximum TREE-MINIMUM( x ) //the iterative one 0. if(x==NIL) return NIL; 1.current = x; 2.while(current.left!=NIL) current = current.left; 3.return current; TREE-MINIMUM( x ) //the recursive one 0. if(x==NIL) return NIL; 1. if (x.left==NIL) return x; 2. return TREE-MINIMUM(x.left); TREE-MAXIMUM( x ) //the recursive one 0. if(x==NIL) return NIL; 1. if (x.right==NIL) return x; 2. return TREE-MAXIMUM( x.right); TREE-MAXIMUM( x ) //the iterative one 0. if(x==NIL) return NIL; 1.current = x; 2.while(current.right!=NIL) current = current.right; 3.return current; O(h) Time complexity?

Successor and predecessor What is a successor of x? What is a successor of x if there is another node has the same key in the binary search tree?

Successor and predecessor TREE-SUCCESSOR( ) 15 The minimum of the right sub tree TREE-SUCCESSOR( ) 13 There is no right sub tree The lowest ancestor whose left child is also an ancestor of or 13

Successor and predecessor TREE-SUCCESSOR( x ) // When x has a right sub tree 1.if(x.right!=NIl) return TREE-MINIMUM(x); // When x does not have a right sub tree 2. current = x 3. currentParent = x.p 4. while( currentParent!=NIL and currentParent.left!=current ){ current = currentParent; currentParrent = currenParent.p; } 5. return currentParent;

17 Successor and predecessor TREE-SUCCESSOR( ) TREE-MINIMUM

Successor and predecessor TREE-SUCCESSOR( ) current currentParent current == currentParent.left is true Has no right sub tree 13

Successor and predecessor TREE-SUCCESSOR( ) current currentParent current == currentParent.left is false currentParent == NIL is false No right subtree current == currentParent.left is false currentParent == NIL is false current == currentParent.left is true 15

Successor and predecessor TREE-SUCCESSOR( ) current currentParent current == currentParent.left is false currentParent == NIL is false No right subtree current == currentParent.left is false currentParent == NIL is false currentParent == NIL is true NIL

TREE-SUCCESSOR( x ) // When x has a right sub tree 1.if(x.right!=NIl) return TREE-MINIMUM(x); // When x does not have a right sub tree 2. current = x 3. currentParent = x.p 4. while( !(currentParent==NIL or currentParent.left==current) ){ current = currentParent; currentParrent = currenParent.p; } 5. return currentParent; TREE-PREDECESSOR( x ) // When x has a left sub tree 1.if(x.left!=NIl) return TREE-MAXIMUM(x); // When x does not have a left sub tree 2. current = x 3. currentParent = x.p 4. while( !(currentParent==NIL or currentParent.right==current) ){ current = currentParent; currentParrent = currenParent.p; } 5. return currentParent; Time complexity? O(h)

Insertion and deletion As a human, how to insert a element to a binary search tree?

Insertion and deletion TREE-INSERT( T, z ) if(T.root==NIL){ T.root = z; z.p = NIL; } else { INSERT(t.root, z); } INSERT(x, z) if(z.key<x.key) if(z.left==NIL){ z.p=x; x.left=z; } else INSERT(x.left, z); else if(z.right==NIL){ z.p=x; x.right=z; } else INSERT(x.right, z);

Insertion and deletion TREE-INSERT( T, ) //recursive 3 Not NIL NIL 3

Insertion and deletion TREE-INSERT( T, z ) // iterative 1.posParent = NIL; 2.pos = T.root; // try to find a position, start from T.root 3.while(pos!=NIL){ 4. posParent = pos; 5. if(z.key < pos.key) 6. pos = pos.left; 7. else 8. pos = pos.right; 9.} 10.z.p = posParent; 11.if(posParent==NIL); // T is empty 12. T.root = z; 13.else if(z.key<posParent.key) 14. posParent.left = z; 15.else 16. posParent.right = z; Find a position Modify z Modify posParent

NIL Insertion and deletion TREE-INSERT( T, ) //iterative 3 pos posParent ….. 3.while(pos!=NIL){ 4. posParent = pos; 5. if(z.key < pos.key) 6. pos = pos.left; 7. else 8. pos = pos.right; 9.} … 3 NIL

Insertion and deletion As a human, how to delete a element from a binary search tree? The element has less than 2 children The element has two children and its successor is the right child The element has two children and its successor is not the right child

Insertion and deletion The element has less than one child TREE-DELETE( ) 22 // no child TREE-DELETE( ) 8 // no child TRANSPLANT( T, u, v ) Replace a tree with another tree

Insertion and deletion TRANSPLANT( T, u, v ) //in T, replace u tree with v tree //modify v 1. if(v!=NIL) v.p = u.p; //modify u’s parent 2. if( u.p==NIL) 3. T.root = v; 4. else if (u == u.p.left) 5. u.p.left = v; 6.else 7. u.p.right=v;

Insertion and deletion TRANSPLANT( T, u, v ) //modify v 1. if(v!=NIL) v.p = u.p; //modify u’s parent 2. if( u.p==NIL) 3. T.root = v; 4. else if (u == u.p.left) 5. u.p.left = v; 6.else 7. u.p.right=v; TRANSPLANT( T,, ) 82 p left right p left right

Insertion and deletion TRANSPLANT( T, u, v ) //modify v 1. if(v!=NIL) v.p = u.p; //modify u’s parent 2. if( u.p==NIL) 3. T.root = v; 4. else if (u == u.p.left) 5. u.p.left = v; 6.else 7. u.p.right=v; TRANSPLANT( T,,.right) 222 p left right NIL

Insertion and deletion TRANSPLANT( T, u, v ) //modify v 1. if(v!=NIL) v.p = u.p; //modify u’s parent 2. if( u.p==NIL) 3. T.root = v; 4. else if (u == u.p.left) 5. u.p.left = v; 6.else 7. u.p.right=v; TRANSPLANT( T,, ) 18 9 p left right p left right

Insertion and deletion TRANSPLANT( T, u, v ) //modify v 1. if(v!=NIL) v.p = u.p; //modify u’s parent 2. if( u.p==NIL) 3. T.root = v; 4. else if (u == u.p.left) 5. u.p.left = v; 6.else 7. u.p.right=v; TRANSPLANT( T,, ) 18 9

TREE-DELETE( T, z) // When z has less than two children 1. if(z.left==NIL) TRANSPLANT(T, z, z.right) 2. else if (z.right==NIL) TRANSPLANT(T, z, z.left) // When z has two children 3. else{ //Get the successor of z 4. y= TREE-MINIMUM(z.right); // it is TREE-SUCCESSOR(z) // if the successor is not z’s right child 5. if(z.right != y){ // upgrade the succesor’s right 6. TRANSPLANT(T, y, y.right); // assign z’right to the successor 7. y.right = z.right; 8. y.right.p = y; 9. } // replace z with the successor 8. TRANSPLANT(T, z, y); // assign z’left to the successor 9. y.left = z.left; 10. y.left.p = y.left; 11.}

Insertion and deletion TREE-DELETE( T, ) Only one child

Insertion and deletion TREE-DELETE( T, ) Find the successor Replace with the successor tree 6 Assign.left to the successor 6 7

Insertion and deletion TREE-DELETE( T, ) Find the successor Replace the successor with its right tree Assign.left to the successor Replace with the successor 15 Assign.right to the successor 15

Insertion and deletion Time complexity TREE-INSERT(T, x) TREE-DELETE(T, x) Similar to TREE-SEARCH, O(h) Because of TREE-SUCCESSOR, O(h)

How to build a binary search tree? By insertion When it is done it randomly, the expected height is O(lgn) What is the worst case? – There is only 1 leaf How to avoid the worst case? – Randomly insert – Variations of binary search tree like RBT