Chapter 12: Binary Search Trees

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)
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
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.
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:
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
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)
The Linked List (LL) Each node x in a linked list contains: Key(x) head key[x]- The value stored at x. next[x]- Pointer to left child of x. The while list.
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.
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,
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Binary Search Trees What is a binary search tree?
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Data Structures Review Session 2
Binary Search Tree Chapter 10.
Analysis of Algorithms
Section 8.1 Trees.
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Ch. 12: Binary Search Trees Ming-Te Chi
Elementary Data Structures
ძებნის ორობითი ხეები BST (Binary Search Trees)
ძებნის ორობითი ხეები BST (Binary Search Trees)
Ch. 12: Binary Search Trees Ming-Te Chi
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
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.
Chapter 20: Binary Trees.
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
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:

Chapter 12: Binary Search Trees

Binary Search Trees

Binary Search Trees Tree representation: Node representation: Left child Right child L R parent key data Tree representation: A linked data structure in which each node is an object Node representation: Key field Satellite data Left: pointer to left child Right: pointer to right child p: pointer to parent (p [root [T]] = NIL) Satisfies the binary-search-tree property

Binary Search Tree Example Binary search tree property: If y is in left subtree of x, then key [y] ≤ key [x] If y is in right subtree of x, then key [y] ≥ key [x] 2 3 5 7 9

Traversing a Binary Search Tree Inorder tree walk: Prints the keys of a binary tree in sorted order Root is printed between the values of its left and right subtrees: left, root, right Preorder tree walk: root printed first: root, left, right Postorder tree walk: left, right, root root printed last Inorder: 2 3 5 5 7 9 2 3 5 7 9 Preorder: 5 3 2 5 7 9 Postorder: 2 5 3 9 7 5

Traversing a Binary Search Tree Alg: INORDER-TREE-WALK(x) if x  NIL then INORDER-TREE-WALK ( left [x] ) print key [x] INORDER-TREE-WALK ( right [x] ) E.g.: Running time: (n), where n is the size of the tree rooted at x 2 3 5 7 9 Output: 2 3 5 5 7 9

Searching for a Key Given a pointer to the root of a tree and a key k: Return a pointer to a node with key k if one exists Otherwise return NIL Idea Starting at the root: trace down a path by comparing k with the key of the current node: If the keys are equal: we have found the key If k < key[x] search in the left subtree of x If k > key[x] search in the right subtree of x 2 3 4 5 7 9

Searching for a Key Alg: TREE-SEARCH(x, k) if x = NIL or k = key [x] then return x if k < key [x] then return TREE-SEARCH(left [x], k ) else return TREE-SEARCH(right [x], k ) 2 3 4 5 7 9 Running Time: O (h), h – the height of the tree

Example: TREE-SEARCH Search for key 13: 15  6  7  13 3 2 4 6 7 13 18 17 20 9 Search for key 13: 15  6  7  13

Iterative Tree Search Alg: ITERATIVE-TREE-SEARCH(x, k) while x  NIL and k  key [x] do if k < key [x] then x  left [x] else x  right [x] return x

Finding the Minimum in a Binary Search Tree Goal: find the minimum value in a BST Following left child pointers from the root, until a NIL is encountered Alg: TREE-MINIMUM(x) while left [x]  NIL do x ← left [x] return x Running time: O(h), h – height of tree 3 2 4 6 7 13 15 18 17 20 9 Minimum = 2

Finding the Maximum in a Binary Search Tree Goal: find the maximum value in a BST Following right child pointers from the root, until a NIL is encountered Alg: TREE-MAXIMUM(x) while right [x]  NIL do x ← right [x] return x Running time: O(h), h – height of tree 3 2 4 6 7 13 15 18 17 20 9 Maximum = 20

Successor Def: successor (x ) = y, such that key [y] is the smallest key > key [x] E.g.: successor (15) = successor (13) = successor (9) = Case 1: right (x) is non empty successor (x ) = the minimum in right (x) Case 2: right (x) is empty go up the tree until the current node is a left child: successor (x ) is the parent of the current node if you cannot go further (and you reached the root): x is the largest element 17 3 2 4 6 7 13 15 18 17 20 9 15 13

Finding the Successor Alg: TREE-SUCCESSOR(x) if right [x]  NIL then return TREE-MINIMUM(right [x]) y ← p[x] while y  NIL and x = right [y] do x ← y y ← p[y] return y Running time: O (h), h – height of the tree 3 2 4 6 7 13 15 18 17 20 9 y x

Predecessor Def: predecessor (x ) = y, such that key [y] is the biggest key < key [x] E.g.: predecessor (15) = predecessor (9) = predecessor (13) = Case 1: left (x) is non empty predecessor (x ) = the maximum in left (x) Case 2: left (x) is empty go up the tree until the current node is a right child: predecessor (x ) is the parent of the current node if you cannot go further (and you reached the root): x is the smallest element 13 3 2 4 6 7 13 15 18 17 20 9 7 9

Binary Search Trees

Insertion else move to the left child of x Goal: Idea: Insert value v into a binary search tree Idea: If key [x] < v move to the right child of x, else move to the left child of x When x is NIL, we found the correct position If v < key [y] insert the new node as y’s left child else insert it as y’s right child Begining at the root, go down the tree and maintain: Pointer x : traces the downward path (current node) Pointer y : parent of x (“trailing pointer” ) Insert value 13 2 1 3 5 9 12 18 15 19 17 13

Example: TREE-INSERT x, y=NIL y Insert 13: 2 1 3 5 9 12 18 15 19 17 2 x = NIL y = 15

Alg: TREE-INSERT(T, z) y ← NIL x ← root [T] while x ≠ NIL do y ← x if key [z] < key [x] then x ← left [x] else x ← right [x] p[z] ← y if y = NIL then root [T] ← z Tree T was empty else if key [z] < key [y] then left [y] ← z else right [y] ← z 2 1 3 5 9 12 18 15 19 17 13 Running time: O(h)

Binary Search Trees

Binary Search Trees

Binary Search Trees

Binary Search Trees

Binary Search Trees Otherwise z has two children. Find z’s successor y. y must lie in z’s right subtree and have no left child (Exercise 12.2-5).

Binary Search Trees

Binary Search Trees

Binary Search Trees