1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

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.
Introduction to Algorithms Jiafen Liu Sept
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)
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 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.
Binary Search Trees Comp 550.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order.
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.
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.
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.
CS 3343: Analysis of Algorithms Lecture 16: Binary search trees & red- black trees.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
Binary Search Tree Qamar Abbas.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
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.
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)
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.
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.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Binary Search Trees What is a binary search tree?
Binary Search Trees.
CS200: Algorithms Analysis
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Elementary Data Structures
ძებნის ორობითი ხეები 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
Chapter 12: Binary Search Trees
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
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
Presentation transcript:

1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees

2 Binary Trees A Binary tree is a linked data structure. Each node contains data (including a key and satellite data), and pointers left, right and p. Left points to the left child of the node. Right points to the right child of the node. p points to the parent of the node. If a child is missing, the pointer is NIL. If a parent is missing, p is NIL. The root of the tree is the only node for which p is NIL. Nodes for which both left and right are NIL are leaves.

3 Binary Search Trees A binary search tree has keys that 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 key[y] <= key[x]. If y is a node in the right subtree of x, then key[y] >= key[x]. Examples: A. B

4 Traversing a Binary Tree In-order traversal means that we first traverse the left subtree, then print the key of the current node, and finally traverse the right subtree. In-Order-Tree-Walk(x) if x != NIL then In-Order-Tree-Walk(left[x]) Print key[x] In-Order-Tree-Walk(right[x]) Note that the in-order traversal of a binary search tree prints out the keys in sorted order (see trees A and B on previous slide).

5 Other tree traversal orders Pre-order traversal: print key[x] traverse left subtree traverse right subtree Post-order traversal: traverse left subtree traverse right subtree print key[x]

6 Running time of Traversal For a tree of size n (that has n nodes): If the left subtree has k nodes, then the right subtree has n-k-1 nodes. T(n) = T(k) + T(n-k-1) + d Substitution Method: Assume T(n) = (c+d)n + c T(0) = c For n > 0 T(n) = ? = T(k) + T(n-k-1) + d = ((c+d)k + c) + ((c+d)(n - k - 1) + c) + d = (c + d) n + c Therefore, T(n) =  (n)

7 Searching a binary search tree Tree-Search(x, k){Search from node, x, for key, 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) Example: a) Search for 7 b) Search for 11 Running time?

8 Finding the minimum and maximum To find the minimum key value, we follow the leftmost path from root to NIL. Similarly, to find the maximum key value, we follow the rightmost path from root to NIL. Tree-Minimum while left[x] != NIL do x  left[x] return x Tree-Maximum while right[x] != NIL do x  right[x] return x Running Time?

9 Finding the Successor to a node Idea: We want to find the node with the smallest key greater than key[x]. We can do this without comparing keys! Two cases for finding the successor: Case 1: The right subtree of x is not empty. The successor of x is the minimum of the right subtree. Case 2: The right subtree of x is empty. The successor of x (if it exists) is the lowest ancestor of x whose left child is also an ancestor x. (i.e. x is in the rightmost path of the left subtree of successor of x)

10 Successor Pseudocode Example: Case 1: Successor of 6 is 7 Case 2: Successor of 8 is 10 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

11 Inserting into a Binary Search Tree Problem: Insert a new node, z, with key[z] = v, left[z]=right[z]=p[z] = NIL, into its proper position on the tree. Idea: Follow the tree from root to leaf, going right or left depending on whether key[z] is greater than or less than the key of the current node Example: Insert node with v = 9 Running time?

12 Pseudocode for Tree-Insert Tree-Insert(T, z) y  NIL x  root(T){Pointer to root node} while x != NIL do{Find Position} y  x{Store current node before moving on} {what goes here?} p[z]  y{Previous node, y, is now parent} if y = NIL then{Tree was empty} root[T]  z else if key[z] < key[y]{Insert in left branch of parent} left[y]  z else{Insert in right branch of parent} right[y]  z