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.

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)
Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.
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 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.
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.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
Balanced Trees Balanced trees have height O(lg n).
Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
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.
Data Structure & Algorithm 09 – Binary Search Tree JJCAO.
Elementary Data Structures Data Structures and Algorithms A. G. Malamos.
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 Search Tree Qamar Abbas.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Binary Search Trees Algorithms and Data Structures.
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.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
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 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
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.
Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk.
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?
Insertion/Deletion in binary trees
Balancing Binary Search Trees
Binary Search Trees.
Red Black Trees
Tree.
CS200: Algorithms Analysis
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Ch. 12: Binary Search Trees Ming-Te Chi
Ch. 12: Binary Search Trees Ming-Te Chi
Lecture 7 Algorithm Analysis
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.
Binary Search Trees Comp 122, Spring 2004.
Binary Trees, Binary Search Trees
Presentation transcript:

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 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. Particular kinds of nodes: –Root –Leaves

What is a binary search tree ? 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 a node in a binary search tree. If y is a node in the left subtree of x, then y.key = x.key.

Example – Binary search tree

What can be done on a BST ? Tree walks 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

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 of a subtree 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 and after this print subtrees

Inorder

Example – Tree walks Inorder: Postorder: Preorder:

Querying a binary search tree Search Minimum Maximum Successor Predecessor

Example – Search Search k=6, return pointer x to node containing k 6<10 left 6>4 right

Search

Example – Minimum and Maximum Search nodes with minimum and maximum key values left right left right

Example – Successor Find the node y which contains the successor of a given node 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 x

Example – Successor Find the node y which contains the successor of a given node x Successor of x = the smallest key which is bigger than x.key Case 2: x has no right subtree 3 x y

Case1 Case2

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 !

Example – Insert Insert node z z.key=7 Step 1 7 x y 7<10 z

Example – Insert Insert node z z.key=7 Step 2 x y 7>4 7 z

Example – Insert Insert node z z.key=7 Step 3 x y 7>6 7 z

Example – Insert Insert node z z.key=7 Step 4 x y 7<8 7 z

Example - delete z Delete z. Case 1: z has no left child

Example - delete z Delete z. Case 2: z has no right child

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

Example - delete z Delete z. Case 4: z has 2 children and z’s successor, y, is not the right child of z 6 8 y

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

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

Tree delete – case 3 z T y 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

Tree delete – case 4 z T y T 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

Tree-Delete Case1 Case 2 Case3 Case 4

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 –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

Height of a BST – best case The best case corresponds to a balanced tree In this case the height is log n

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)