Elementary Data Structures

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)
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.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 6 Dynamic data structures Motivation Common dynamic ADTs Stacks, queues, lists:
10.Elementary data structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Stacks and queues Stacks and queues are dynamic set in which element.
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,
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
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.
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.
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.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 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.
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.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Chapter 10 Elementary data structures Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
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?
Elementary data structures
DAST Tirgul 7.
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
CPSC 311 Section 502 Analysis of Algorithm
Data Structures Review Session 2
CS200: Algorithms Analysis
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Algorithms Part III. Data Structures
ძებნის ორობითი ხეები BST (Binary Search 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
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
Binary SearchTrees [CLRS] – Chap 12.
EE Data Structures and Algorithms
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:

Elementary Data Structures Jeff Chastine

Stacks and Queues The element removed is prespecified A stack implements last-in, first-out (LIFO) A queue implements first-in, first-out (FIFO) What about FOFI, or LILO? Jeff Chastine

Stacks Implemented using an array Use PUSH and POP operations Has attribute top[S] Contains elements S[1..top[S]] When top[S] = 0, the stack is empty If top[S] > n, then we have an overflow Jeff Chastine

1 2 3 4 5 6 7 15 6 2 9 top[S] = 4 1 2 3 4 5 6 7 15 6 2 9 17 3 top[S] = 4 1 2 3 4 5 6 7 15 6 2 9 17 3 top[S] = 4 Jeff Chastine

STACK-EMPTY (S) 1 if top[S] = 0 2 then return TRUE 3 else return FALSE PUSH (S, x) 1 top[S]  top[S] + 1 2 S[top[S]]  x POP (S) 1 if STACK-EMPTY (S) 2 then error “underflow” 3 else top[S]  top[S] – 1 4 return S[top[S]+1] Note: All operations performed in O(1) Jeff Chastine

Queues Supports ENQUEUE and DEQUEUE operations Has a head and tail New elements are placed at the tail Dequeued element is always at head Locations “wrap around” When head[Q] = tail[Q], Q is empty When head[Q] = tail[Q] + 1, Q is full Jeff Chastine

1 2 3 4 5 6 7 8 9 10 11 12 15 6 9 8 4 head[Q] = 7 tail[Q] = 7 1 2 3 4 5 6 7 8 9 10 11 12 3 5 15 6 9 8 4 17 tail[Q] = 7 head[Q] = 7 1 2 3 4 5 6 7 8 9 10 11 12 3 5 15 6 9 8 4 17 tail[Q] = 7 head[Q] = 7 Jeff Chastine

ENQUEUE (Q, x) 1 Q[tail[Q]]  x 2 if tail[Q] = length[Q] 3 then tail[Q]  1 4 else tail[Q]  tail[Q] + 1 DEQUEUE (Q) 1 x  Q[head[Q]] 2 if head[Q] = length[Q] 3 then head[Q]  1 4 else head[Q]  head[Q] + 1 5 return x Jeff Chastine

Linked Lists A data structure where the objects are arranged linearly according to pointers Each node has a pointer to the next node Can be a doubly linked list Each node has a next and previous pointer Could also be circularly linked If prev[x] = NIL, we call that node the head If next[x] = NIL, we call that node the tail The list may or may not be sorted! Jeff Chastine

Example head[L] / 9 16 4 1 / Jeff Chastine

Searching a Linked List LIST-SEARCH (L, k) 1 x  head[L] 2 while x  NIL and key[x]  k 3 do x  next[x] 4 return x Worst case, this takes (n) Jeff Chastine

Inserting into a Linked List LIST-INSERT (L, x) 1 next[x]  head[L] 2 if head[L]  NIL 3 then prev[head[L]]  x 4 head[L]  x 5 prev[x]  NIL Note: runs in O(1) Jeff Chastine

Example head[L] / 9 16 4 1 / head[L] / 25 9 16 4 1 / Jeff Chastine

Deleting from a Linked List LIST-DELETE (L, x) 1 if prev[x]  NIL 2 then next[prev[x]]  next[x] 3 else head[L]  next[x] 4 if next[x]  NIL 5 then prev[next[x]]  prev[x] Jeff Chastine

Example head[L] / 9 16 4 1 / head[L] / 25 9 16 4 1 / head[L] / 25 9 16 Jeff Chastine

Sentinel Nodes Used to simplify checking of boundary conditions Here is a circular, doubly-linked list 9 16 4 1 nil[L] Jeff Chastine

Binary Search Trees BSTs Very important data structure Can support many dynamic-set operations: SEARCH MINIMUM/MAXIMUM PREDECESSOR/SUCCESSOR INSERT/DELETE Operations take time proportional to the height of the tree, often O(lg n) Jeff Chastine

Binary Search Trees Each node has a key, as well as left, right and parent pointers Thus, the root has a parent = NIL Binary search tree property: Let x be a node. If y is a node in the left subtree, key[y]  key[x]. If y is a node in the right subtree, key[y] > key[x]. Jeff Chastine

INORDER-TREE-WALK (x) 1 if x  NIL 2 then INORDER-TREE-WALK(left[x]) 5 3 7 2 5 8 INORDER-TREE-WALK (x) 1 if x  NIL 2 then INORDER-TREE-WALK(left[x]) 3 print key[x] 4 INORDER-TREE-WALK(right[x]) In-Order: 2, 3, 5, 5, 7, 8 Pre-Order: 5, 3, 2, 5, 7, 8 Post-Order: 2, 5, 3, 8, 7, 5 Jeff Chastine

Proof: In-Order Walk Takes (n) Let c be the time for the test x  NIL, and d be the time to print Suppose tree T has k left nodes and n-k right nodes. Using substitution, we assume T(n)=(c+d)n+c T(n) = T(k) + T(n-k-1) + d = ((c+d)k+c) + ((c+d)(n-k-1)+c) + d = (c+d)n + c – (c+d) + c + d = (c+d)n + c Jeff Chastine

Querying a Binary Search Tree Tree should support the following: SEARCH – determine if an element exists in T MINIMUM – return the smallest element in T MAXIMUM – return the largest element in T SUCCESSOR – given a key, return the next largest element, if any PREDECESSOR – given a key, return the next smallest element, if any Jeff Chastine

TREE-SEARCH (x, k) 1 if x = NIL or k = key[x] 2 then return x 3 if k < key[x] 4 then return TREE-SEARCH(left[x], k) 5 else return TREE-SEARCH(right[x], k) Note: run time is O(h), where h is the height of the tree Jeff Chastine

Tree Minimum/Maximum TREE-MINIMUM(x) 1 while left[x]  NIL 2 do x  left[x] 3 return x TREE-MAXIMUM (x) 1 while right[x]  NIL 2 do x  right[x] Note: binary search tree property guarantees this is correct Jeff Chastine

Successor/Predecessor Successor is the node with the smallest key greater than key[x]. Not necessary to compare keys! Two cases: If right subtree is non-empty, find its minimum If right subtree is empty, find lowest ancestor of x whose left child is also an ancestor of x       Jeff Chastine

TREE-SUCCESSOR (x) 1 if right[x]  NIL 2 then return TREE-MINIMUM(right[x]) 3 y  p[x] 4 while y  NIL and x = right[y] 5 do x  y 6 y  p[y] 7 return y Note: simply go up the tree until we encounter a node that is the left child. Runs in O(h) Jeff Chastine

Inserting into a BST 50 56 30 71 27 43 88 Jeff Chastine

Inserting into a BST 50 30 71 56 27 43 88 Jeff Chastine

Inserting into a BST 50 74 30 71 27 43 56 88 Jeff Chastine

Inserting into a BST 50 30 71 74 27 43 56 88 Jeff Chastine

Inserting into a BST 50 30 71 27 43 56 88 74 Jeff Chastine

Inserting into a BST 50 30 71 27 43 56 88 74 Jeff Chastine

Deleting Three cases, given node z: z has no children. Delete it, and update the parent. If z has only one child, splice it out by making a link between its child and parent. If z has two children, splice out z’s successor y and replace z with y Jeff Chastine

15 15 5 16 5 16 3 12 20 3 12 20 10 13 18 23 10 18 23 6 6 7 7 Case 1: z has no children Jeff Chastine

15 15 20 5 16 5 3 12 20 3 12 18 23 10 13 18 23 10 13 6 6 7 7 Case 2: z has one child Jeff Chastine

Case 3: z has two children 15 15 20 5 16 6 3 12 20 3 12 18 23 10 13 18 23 10 13 6 7 y 7 Case 3: z has two children Jeff Chastine