CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,

Slides:



Advertisements
Similar presentations
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Advertisements

Chapter 12 Binary Search Trees
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.
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,
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 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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Design and Analysis of Algorithms Binary search trees Haidong Xue Summer 2012, at GSU.
CS 2133: Data Structures Binary Search Trees.
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.
Elementary Data Structures Data Structures and Algorithms A. G. Malamos.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
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.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
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.
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)
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.
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.
Binary Search Trees What is a binary search tree?
CS 3013 Binary Search Trees.
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
CS200: Algorithms Analysis
CS 583 Analysis of Algorithms
CS 3013 Binary Search Trees.
Lecture 7 Algorithm Analysis
Elementary Data Structures
Ch. 12: Binary Search Trees Ming-Te Chi
Lecture 7 Algorithm Analysis
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
Binary SearchTrees [CLRS] – Chap 12.
Design and Analysis of Algorithms
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Presentation transcript:

CS6045: Advanced Algorithms Data Structures

Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular, structures for dynamic sets –Elements have a key and satellite data –Dynamic sets support queries such as: Search(S, k), Minimum(S), Maximum(S), Successor(S, x), Predecessor(S, x) –They may also support modifying operations like: Insert(S, x), Delete(S, x)

Stacks Stack (has top), LIFO (last-in first-out) policy –insert = push ( top(S) = top(S)+1); S[top(S)] = x ) O(1) –delete = pop O(1) top = top = 5

Stacks More operations –Empty stack: contain no elements –Stack underflow: pop an empty stack –Stack overflow: S.top >= n Operations running time?

Queues Queue (has head and tail), FIFO (first-in first-out) policy –insert = enqueue (add element to the tail) O(1) –delete = dequeue (remove element from the head) O(1) tail = 6head = tail = 6head = 2

Circular Queue

Linked List Linked List (objects are arranged in a linear order) –The order is determined by a pointer in each object –Array: the order determined by array indices Doubly Linked List (has head) –Key, prev, and next attributes x

Linked List Operations: –Search –Insert to the head –Delete x

Linked List Running time of linked list operations: –Search –Insert –Delete

Binary Tree Binary Tree has root –Each node has parent, left, and right attributes

Binary Search Trees Binary Search Trees (BSTs) are an important data structure for dynamic sets 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)

Binary Search Trees BST property: –all keys in the left subtree of key[leftSubtree(x)]  key[x] –all keys in the right subtree of key[x]  key[rightSubtree(x)] Example: F BH KDA

In-Order-Tree Walk What does the following code do? Prints elements in sorted (increasing) order This is called an in-order-tree walk –Preorder tree walk: print root, then left, then right –Postorder tree walk: print left, then right, then root

In-Order-Tree Walk Time complexity? –O(n) Prove that in-order-tree walk prints in monotonically increasing order? –By induction on size of tree

Operations on BSTs: Search Given a key and a pointer to a node, returns an element with that key or NULL: Iterative Tree-Search(x,k) while k  key[x] do if k < key[x] then x  x.left else x  x.right return x Which one is more efficient? The iterative tree search is more efficient on most computers. The recursive tree search is more straightforward.

Operations on BSTs: Min-Max MIN: leftmost node MAX: rightmost node Running time? –O(h) where h = height of tree

Operations on BSTs: Successor-Predecessor Successor: the node with the smallest key greater than x.key –x has a right subtree: successor is minimum node in right subtree –x has no right subtree: successor is first ancestor of x whose left child is also ancestor of x –Intuition: As long as you move to the left up the tree, you’re visiting smaller nodes

Operations of BSTs: Insert Adds an element x to the tree so that the binary search tree property continues to hold The basic algorithm –Insert node z, z.key = v, z.left = z.right = NIL –Maintain two pointes: x: trace the downward path y: “trailing pointer” to keep track of parent of x –Traverse the tree downward by comparing x.key with v –When x is NIL, it is at the correct position for node z –Compare v with y.key, and insert z at either y’s left or right, appropriately

BST Insert: Example Example: Insert C F BH KDA C

BST Search/Insert: Running Time What is the running time of TreeSearch() or TreeInsert()? A: O(h), where h = height of tree What is the height of a binary search tree? A: worst case: h = O(n) when tree is just a linear string of left or right children –Average case: h=O(lg n)

Sorting With BSTs It’s a form of quicksort! for i=1 to n TreeInsert(A[i]); InorderTreeWalk(root);

Sorting with BSTs Which do you think is better, quicksort or BSTSort? Why? A: quicksort –Sorts in place –Doesn’t need to build data structure

BST Operations: Delete Deletion is a bit tricky 3 cases: –x has no children: Remove x

BST Operations: Delete 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 zz z successor(z) exchange

BST Operations: Delete Why will case 2 always go to case 0 or case 1? A: because when x has 2 children, its successor is the minimum in its right subtree, and that successor has no left child (hence 0 or 1 child). Could we swap x with predecessor instead of successor? A: yes. Would it be a good idea? A: might be good to alternate to avoid creating unbalanced tree.