1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: 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.
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)
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.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
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.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
Lecture 11: Binary Search Trees Shang-Hua Teng. Data Format KeysEntryKeysSatellite data.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
CS 2133: Data Structures Binary Search 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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
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.
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
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.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
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.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
1 CSC 211 Data Structures Lecture 25 Dr. Iftikhar Azim Niaz 1.
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.
Tree.
Binary Trees, Binary Search Trees
Ch. 12: Binary Search Trees Ming-Te Chi
Elementary Data Structures
ძებნის ორობითი ხეები BST (Binary Search Trees)
Red Black Tree Essentials
Ch. 12: Binary Search Trees Ming-Te Chi
Binary Search Trees (13.1/12.1)
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Red Black Tree Essentials
Topic 6: Binary Search Tree Data structure Operations
Design and Analysis of Algorithms
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Presentation transcript:

1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees

1.2 Data Structure and Algorithm subtree Trees Node Edge parent root leaf interior node child path Degree? Depth/Level? Height?

1.3 Data Structure and Algorithm Binary Tree Representation Node  data  left child  right child  parent (optional) Parent Data Left Right Tree root

1.4 Data Structure and Algorithm Full Binary Tree  Replace each missing child in the binary tree with a node having no children (black nodes) which are called leaf nodes.  Each node is either a leaf or has degree exactly 2  All leaves are at level h (height)  All interior nodes are full Full Binary Tree Binary Tree

1.5 Data Structure and Algorithm Complete Binary Tree  All leaves have the same depth  All interior nodes have degree 2 depth 0 depth 1 depth 2 depth 3 height=3

1.6 Data Structure and Algorithm Complete Binary Tree The number of internal nodes of a complete binary tree of height h is: ……2 h-1 = 2 h -1/2-1 = 2 h -1 What is the number of total nodes in a complete binary tree of height h? What is the number of leaf nodes in a complete binary tree of height h?

1.7 Data Structure and Algorithm Applications - Expression Trees * (5*3)+(8-4) To represent infix expressions

1.8 Data Structure and Algorithm Applications - Parse Trees Used in compilers to check syntax statement ifcondthen statementelsestatement ifcondthen statement

1.9 Data Structure and Algorithm Binary Search Trees Binary Search Trees (BSTs) are an important data structure for dynamic sets In addition to data, elements have:  key: an identifying field inducing a total ordering  left: pointer to a left child (may be NULL)  right: pointer to a right child (may be NULL)  p: pointer to a parent node (NULL for root)

1.10 Data Structure and Algorithm Binary Search Trees BST property: key[leftSubtree(x)]  key[x]  key[rightSubtree(x)] Example: F BH KDA

1.11 Data Structure and Algorithm Traversals for a Binary Tree Pre order  visit the node  go left  go right In order  go left  visit the node  go right Post order  go left  go right  visit the node

1.12 Data Structure and Algorithm Traversal Examples A B D G C E H F I Pre order A B D G H C E F I In order G D H B A E C F I Post order G H D B E I F C A

1.13 Data Structure and Algorithm Traversal Implementation recursive implementation of preorder  pre-order ( node )  visit node  pre-order ( node.left )  pre-order ( node.right ) What changes need to be made for in-order, post-order?

1.14 Data Structure and Algorithm Inorder Tree Walk n in-order(x) in-order (x.left); print(x); in-order(x.right); Prints elements in sorted (increasing) order

1.15 Data Structure and Algorithm Postorder Tree Walk n post-order(x) print(x) post-order (x.left); post-order(x.right);

1.16 Data Structure and Algorithm Evaluating an expression tree Walk the tree in postorder When visiting a node, use the results of its children to evaluate it *

1.17 Data Structure and Algorithm BST Operations Search  Key  Minimum  Maximum  Successor  Predecessor Insert node Delete node

1.18 Data Structure and Algorithm BST Search: Key Search for D and C: F BH KDA

1.19 Data Structure and Algorithm BST Search: Key Given a key and a pointer to a node, returns an element with that key or NULL: TreeSearch(x, k) if (x = NULL or k = key[x]) return x; if (k < key[x]) return TreeSearch(left[x], k); else return TreeSearch(right[x], k); Cost: O(lg 2 n) Where height, h=lg 2 n

1.20 Data Structure and Algorithm BST Search: Minimum TreeMinimum(x)  While left[x] <> NIL do  x = left[x]  return x

1.21 Data Structure and Algorithm BST Search: Maximum TreeMaximum(x)  While right[x] <> NIL do  x = right[x]  return x

1.22 Data Structure and Algorithm BST Search: Successor Inorder traversal: Case1:x has a right subtree successor is minimum node in right subtree Case2: x has no right subtree successor is first ancestor of x whose left child is also ancestor of x

1.23 Data Structure and Algorithm BST Search: Successor if right[x] <> NIL then  return TreeMinimum (right[x]) y = p[x] while y <> NIL and x = right[y] do  x = y  y = p[y] return y Predecessor: similar algorithm

1.24 Data Structure and Algorithm BST: Insert Node Example: Insert C F BH KDA C

1.25 Data Structure and Algorithm BST: Insert Node Adds an element x to the tree so that the binary search tree property continues to hold TreeInsert (T, z)  y = NIL  x = root[T]  While x <> NILL 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  else if key[z] <key[y] then left[y] = z  else right[y] = z Cost: O(lg 2 n) Where height, h=lg 2 n

1.26 Data Structure and Algorithm BST: Delete Node Deletion is a bit tricky 3 cases:  x has no children:  Remove x  x has one child:  Splice out x  x has two children:  Swap x with successor  Perform case 1 or 2 to delete it F BH KDA C Example: delete K or H or B