Binary SearchTrees [CLRS] – Chap 12.

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.
Trees Types and Operations
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.
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 Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
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.
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.
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.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
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.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
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
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
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)
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
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,
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Binary Search Trees What is a binary search tree?
DAST Tirgul 7.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Data Structures – LECTURE Balanced trees
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Summary of General Binary search tree
CS200: Algorithms Analysis
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Ch. 12: Binary Search Trees Ming-Te Chi
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Ch. 12: Binary Search Trees Ming-Te Chi
Ch. 12: Binary Search Trees
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
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
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
Presentation transcript:

Binary SearchTrees [CLRS] – Chap 12

Context and Problem We need a dynamic set where all operations are equally important: insert, delete, search, maximum, minimum, ordered list This is a kind of dynamic set that generalizes both the Dictionary structure as well as the Priority Queue structure Binary Search Trees are such a data structure

What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following attributes: a key and satellite data left, pointing to its left child right, pointing to its right child p, pointing to its parent If a child or the parent is missing, the appropriate attribute contains the value NIL. A binary tree is a recursive structure Particular kinds of nodes: Root Leaves Height of a tree: longest path from the root to one of the leaves; max(heights of subtrees) + 1

Shapes of binary trees A full binary tree is a binary tree in which every node other than the leaves has two children. A perfect binary tree is a binary tree in which all interior nodes have two children and all leaves have the same level. A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

Kinds of binary trees General binary tree: no conditions regarding key values and shape Min-heap tree: is a binary tree such that: the key contained in each node is less than (or equal to) the key in that node’s children. the binary tree is complete Max-heap tree: is a binary tree such that: the key contained in each node is bigger than (or equal to) the key in that node’s children. Binary search tree

What is a Binary Search Tree (BST) The keys in a binary search tree are always stored in such a way as to satisfy the binary-search-tree property: Let x be any node in a binary search tree. If y is any node in the left subtree of x, then y.key <=x.key. If y is any node in the right subtree of x, then y.key >= x.key. X <=X >=X

Example – BST 10 15 4 6 1 8 5 23 18 20 2

Counterexample – NOT a BST ! 10 15 1 4 2 8 6 23 20 18 5

What can be done on a BST ? Dynamic-set operations Tree walks Queries List in order Queries Search Minimum Maximum Successor Predecessor Modifying Insert Delete These are Dynamic-set operations BST generalize both the Dictionary structure as well as the Priority Queue structure

Example – List in order 10 15 4 6 1 8 5 23 18 20 2 List in order: 1 2 4 5 6 8 10 15 18 20 23

Tree walks The binary-search-tree property allows us to print out all the keys in a binary search tree in sorted order by a simple recursive algorithm, called an inorder tree walk. This algorithm is so named because it prints the key of the root in between printing the values in its left subtree and printing those in its right subtree. Postorder: print subtrees, after this print root Preorder: print root first and then print subtrees

Example – Tree walks 10 15 4 6 1 8 5 23 18 20 2 Inorder: 1 2 4 5 6 8 10 15 18 20 23 Postorder: 2 1 5 8 6 4 18 23 20 15 10 Preorder: 10 4 1 2 6 5 8 15 20 18 23

Inorder [CLRS, chap 12, pag 288]

Complexity of tree walks We have a BST with n nodes Intuitively: every node is visited exactly once and a constant time operation (print) is done on it => O(n) Formal: Suppose that the BST with n nodes has k nodes in the left subtree and n-k-1 in the right subtree T(n)=c, if n=0 T(n)=T(k)+T(n-k-1)+d, if n>0 We assume that T(n)=(c+d)*n+c => easy proof by induction (verify basecase for n=0, assume true for all smaller than n, replace in T(n) and prove that it is according to the assumed formula)

Querying a binary search tree Minimum Maximum Successor Predecessor

return pointer x to node Example – Search 6<10 Search for k=6, return pointer x to node containing k left 10 15 4 6 1 8 5 23 18 20 2 6>4 right

Search – recursive version [CLRS, chap 12, pag 290]

Search – iterative version [CLRS, chap 12, pag 291]

Example – Minimum and Maximum Search nodes with minimum and maximum key values left 10 15 4 6 1 8 5 23 18 20 2 right left right right

[CLRS, chap 12, pag 291]

Example – Successor Find the node y which contains the successor of a given node x 10 15 4 6 1 8 5 23 18 20 2 x Successor of x = the smallest key which is bigger than x.key Case 1: x has a right subtree : the successor y is the minimum in the right subtree

Example – Successor Find the node y which contains the successor of a given node x 10 y 4 15 1 6 20 2 5 8 18 23 x 3 Successor of x = the smallest key which is bigger than x.key Case 2: x has no right subtree

Case1 Case2 [CLRS, chap 12, pag 292]

Modifying a binary search tree The operations of insertion and deletion cause the dynamic set represented by a binary search tree to change. The data structure must be modified to reflect this change, but in such a way that the binary-search-tree property continues to hold !

Operation: Insert Insert a node z into a binary search tree T After insertion, T must remain a binary search tree

Example – Insert x y 10 4 15 1 6 20 2 5 8 18 23 z 7 Insert node z z.key=7 Step 1 10 7<10 4 15 1 6 20 2 5 8 18 23 z 7

Example – Insert y 10 4 15 x 1 6 20 2 5 8 18 23 z 7 Insert node z z.key=7 Step 2 y 10 4 15 x 7>4 1 6 20 2 5 8 18 23 z 7

Example – Insert 10 4 15 y 1 6 20 x 2 5 8 18 23 z 7 Insert node z z.key=7 Step 3 10 4 15 y 1 6 20 7>6 x 2 5 8 18 23 z 7

Example – Insert 10 4 15 1 6 20 y x 2 5 8 18 23 z 7 Insert node z z.key=7 Step 4 10 4 15 1 6 20 y x 2 5 8 18 23 7<8 z 7

[CLRS, chap 12, pag 294]

Operation: Delete Delete a node z from a binary search tree T After delete, T must remain a binary search tree Cases: Cases 1+2: node z has only 1 child -> the only child takes the place of its deleted parent z Case 3+4: node z has 2 children. The successor of z will take its place

Delete z. Case 1: z has no left child Example - delete z 10 10 15 6 20 13 4 15 6 13 20 Delete z. Case 1: z has no left child

Delete z. Case 2: z has no right child Example - delete z 10 10 15 2 20 13 4 15 2 13 20 Delete z. Case 2: z has no right child

Example - delete z 10 10 15 6 2 20 13 8 1 3 4 15 y 6 2 13 20 3 1 8 Delete z. Case 3: z has 2 children and z’s successor, y, is the right child of z

Example - delete z 10 10 15 5 2 20 13 6 8 1 5.5 2.5 4 15 y 6 2 13 20 5 1 2.5 8 5.5 Delete z. Case 4: z has 2 children and z’s successor, y, is not the right child of z

BST delete – shaping the general solution Suppose that in order to move subtrees around within the BST, we define the operation TRANSPLANT, which replaces one subtree u as a child of its parent with another subtree v. [CLRS, chap 12, pag 296]

Transplant(T, z, z.right) Tree delete – case 1 T T z z.right If (z.left==nil) Transplant(T, z, z.right)

Tree delete – case 2 T T z z.left If (z.right==nil) Transplant(T, z, z.left)

Tree delete – case 3 T T If (z.left<>nil and z.right<>nil) Y=TREE-MINIMUM(z.right) If (y=z.right) TRANSPLANT(T,z,y) y.left=z.left y.left.p=y z y

Tree delete – case 4 T T z If (z.left<>nil and z.right<>nil) Y=TREE-MINIMUM(z.right) If (y<>z.right) TRANSPLANT(T,y,y.right) y.right=z.right y.right.p=y TRANSPLANT(T,z,y) y.left=z.left y.left.p=y y

Tree-Delete Case1 Case 2 Case 4 Case3 [CLRS, chap 12, pag 298]

BST Delete The previous implementation of BST Delete considered replacing the deleted node z with its successor Another similar solution is to replace the deleted node z with its predecessor

Exercise Draw the Binary Search Tree that results from following sequence of operations: Insert 29, 37, 1, 3, 7, 20, 89, 75, 4, 2, 6, 30, 35 Delete 3, 29, 37

Analysis Tree walks Θ(n) Queries Modifying Search O(h) Minimum O(h) Maximum O(h) Successor O(h) Predecessor O(h) Modifying Insert O(h) Delete O(h)

The Height of Binary Search Trees Each of the basic operations on a binary search tree runs in O(h) time, where h is the height of the tree => It is desirable that h is small The height of a binary search tree of n nodes depends on the order in which the keys are inserted The height of a BST with n nodes: Worst case: O(n) => BST operations are also O(n) !!! Best case: O(log n) Average case O(log n)

Height of a BST – worst case If the n keys are inserted in strictly increasing order, the tree will be a chain with height n-1. 1 2 3 4 5

Height of a BST – best case The best case corresponds to a balanced tree In this case the height is log n 1 2 3 4 5 6 7 4 2 6 1 3 5 7

Height of a BST – random case It can be proved that: The expected height of a randomly built binary search tree on n distinct keys is O (lg n)

Keeping the height of BST small Different techniques are used in order to keep the height of BST small – after an insertion or deletion some operations are done in order to redo the balance: AVL trees (Adelson-Velskii and Landis) Red-black trees (symmetric binary B-trees, 2-3 trees)