Algorithms CSCI 235, Spring 2019 Lecture 21 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.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Binary Search Trees Comp 550.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU.
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:
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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 Trees (10.1) CSE 2011 Winter November 2015.
Binary Search Tree Qamar Abbas.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
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.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
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.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
1 CSC 211 Data Structures Lecture 25 Dr. Iftikhar Azim Niaz 1.
Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk.
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.
Binary Search Trees What is a binary search tree?
Red Black Trees
Binary Search Tree (BST)
Design & Analysis of Algorithm n-ary Tree & Binary Tree
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Summary of General Binary search tree
Binary Search Tree.
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
Red Black Trees.
Ch. 12: Binary Search Trees Ming-Te Chi
Ch. 12: Binary Search Trees
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
Binary Search Trees.
CS6045: Advanced Algorithms
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Topic 6: Binary Search Tree Data structure Operations
Binary SearchTrees [CLRS] – Chap 12.
Design and Analysis of Algorithms
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Properties of Red-black trees
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Binary Trees, Binary Search Trees
Binhai Zhu Computer Science Department, Montana State University
Presentation transcript:

Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees

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.

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 y.key <= x.key If y is a node in the right subtree of x, then y.key >= x.key Examples: A. B. 3 10 5 6 12 8 4 8 14 10 6 5 7 13

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 In-Order-Tree-Walk(x.left) Print x.key In-Order-Tree-Walk(x.right) 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).

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

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) = Q(n)

Searching a binary search tree Tree-Search(x, k) //Search from node, x, for key, k if x == NIL or k == x.key return x if k < x.key return Tree-Search(x.left, k) else return Tree-Search(x.right, k) Example: a) Search for 7 b) Search for 11 10 Running time? 6 12 4 8 14 5 7

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 x.left != NIL x = x.left return x Tree-Maximum while x.right != NIL x = x.right 10 6 12 4 8 14 5 7 Running Time?

Finding the Successor to a node Idea: We want to find the node with the smallest key greater than x.key. 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)

Successor Pseudocode 10 Example: Case 1: Successor of 6 is 7 12 4 8 14 Tree-Successor(x) if x.right != NIL return Tree-Minimum(x.right) y = x.p while y != NIL and x == y.right x = y y = y.p return y 5 7

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

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