CS6045: Advanced Algorithms

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)
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.
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.
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.
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.
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.
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
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)
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.
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.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Binary Search Trees What is a binary search tree?
Chapter 12 – Data Structures
CS 3013 Binary Search Trees.
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Search Tree.
CS200: Algorithms Analysis
ITEC 2620M Introduction to Data Structures
CS 583 Analysis of Algorithms
CS 3013 Binary Search Trees.
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Elementary Data Structures
ITEC 2620M Introduction to Data Structures
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
Dynamic Sets (III, Introduction)
Chapter 12: Binary Search Trees
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
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:

CS6045: Advanced Algorithms Data Structures

Dynamic Sets Dynamic sets: Dictionary: Can grow, shrink (manipulated by algorithms) Change over time Dictionary: Insert/delete elements Test membership

Operations Search(S, k)  a pointer to element x with key k (query) Insert(S, x) add new element pointed to by x, assuming that we have key(x) (modifying) Delete(S, x) delete x, x is a pointer (modifying) Minimum(S)/Maximum(S)  max/min (query) Prede(suc)cessor(S, x)  the next larger/smaller key to the key of element x (query) Union(S, S’)  new set S = S  S’ (modifying)

Stacks Stack (has top), LIFO (last-in first-out) policy 15 6 2 9 17 insert = push (top(S) = top(S)+1); S[top(S)] = x) O(1) delete = pop O(1) 15 6 2 9 1 3 4 5 7 top = 4 17 top = 5

Stacks More operations Operations running time? 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 15 insert = enqueue (add element to the tail) O(1) delete = dequeue (remove element from the head) O(1) 15 6 2 9 1 3 4 5 7 tail = 6 head = 2 8

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: Example: F B H K D A 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 B H K D A

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 7 5 6 3 2 8 9 4

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? 7 5 6 3 2 8 9 4 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 7 5 6 3 2 8 9 4

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. 15 6 7 3 13 2 18 20 4 17 9

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 B H K D A 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); 3 3 1 8 2 6 7 5 1 8 1 2 8 6 7 5 2 6 2 6 7 5 5 7 5 7

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 7 5 6 3 2 8 9 4

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 7 5 6 3 2 8 9 4 z z successor(z) exchange 7 5 6 2 1 8 9 3 4

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.