Contents of Chapter 2 Sections –2.1 Stacks and Queues –2.2 Trees –2.3 Dictionaries –2.4 Priority queues –2.5 Sets and disjoint set union –2.6 Graphs –2.7.

Slides:



Advertisements
Similar presentations
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Advanced Data Structures
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
Marc Smith and Jim Ten Eyck
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.
Important Problem Types and Fundamental Data Structures
Priority Queues, Heaps & Leftist Trees
Binary Trees Chapter 6.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
COSC2007 Data Structures II
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Information and Computer Sciences University of Hawaii, Manoa
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 21 Binary Heap.
Starting at Binary Trees
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Trees 2015, Fall Pusan National University Ki-Joune Li.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Discrete Mathematics Chapter 5 Trees.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
CISC220 Fall 2009 James Atlas Dec 07: Final Exam Review.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CHAPTER 11 TREES INTRODUCTION TO TREES ► A tree is a connected undirected graph with no simple circuit. ► An undirected graph is a tree if and only.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Non Linear Data Structure
Trees Chapter 15.
Heaps, Heap Sort and Priority Queues
Week 11 - Friday CS221.
Hashing Exercises.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
ITEC 2620M Introduction to Data Structures
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
Important Problem Types and Fundamental Data Structures
Presentation transcript:

Contents of Chapter 2 Sections –2.1 Stacks and Queues –2.2 Trees –2.3 Dictionaries –2.4 Priority queues –2.5 Sets and disjoint set union –2.6 Graphs –2.7 References and readings One of the basic techniques for improving algorithms –By structuring the data in such a way that the resulting operations can be efficiently carried out

2.1 Stacks and Queues Ordered list (or linear list) –a = (a 1, a 2, …, a n ) where a i is atom (or element) Stack –Ordered list in which all insertions and deletions made at top –LIFO (Last In First Out) Queue –Ordered list in which insertions made at rear and deletions at front –FIFO (First In First Out)

2.1 Stacks and Queues Example (Figure 2.1) EDCBAEDCBA top A B C D E front rear queuestack

2.1 Stacks and Queues Representation of stack –Using one-dimensional array –Class definition of stack (Program 2.1) #include template class Stack { private: int top, MaxSize; Type *stack; public: Stack(int MSize): MaxSize(MSize) { stack = new Type[MaxSize]; top=-1;} ~Stack() { delete [] stack;} inline bool Add(const Type item) // Push an element onto the stack; return // true if successful else return false. { if (StackFull()) {

cout << "Stack is full"; return false; } else { stack[++top] = item; return true; } inline bool Delete(Type& item) // Pop the top element from the stack; return // true if successful else return false. { if (StackEmpty()) { cout << "Stack is empty"; return false; } else { item = stack[top--]; return true; } inline bool StackEmpty() // Is the stack empty? { if (top < 0) return true; else return false; } inline bool StackFull() // Is the stack full? { if (top >= (MaxSize-1)) return true; else return false; } };

Representation of stack –Using linked list (Figure 2.2) –Linked representation of stack (Program 2.2) 2.1 Stacks and Queues stack data link EDCBA 0 #define NULL 0 #include class Stack { private: struct node { Type data; struct node *link; }; struct node *top;

2.1 Stacks and Queues public: Stack() { top = NULL; } ~Stack() { struct node *temp; while (top) { temp=top; top=top->link; delete temp; } bool Add(const Type item); bool Delete(Type& item); inline bool StackEmpty() // Is the stack empty? { if (top) return false; else return true; } };

2.1 Stacks and Queues Representation of stack –Add programs (Programs on page 69) bool Stack::Add(const Type item) { struct node *temp = new node; if (temp) { temp->data = item; temp->link = top; top = temp; return true; } else { cout << “Out of space!” << end; return false; }

2.1 Stacks and Queues Representation of stack –Delete programs (Programs on page 69) bool Stack::Delete(Type& item) { if (StackEmpty()) { cout << “Stack is empty” << endl; return false; } else { struct node *temp; item=top->data; temp = top; top = top->link; delete temp; return true; }

2.1 Stacks and Queues Representation of queue –Using one-dimensional array (Figure 2.3)

2.1 Stacks and Queues Representation of queue –A class definition of queue (Programs 2.3) #define NULL 0 #include class Queue { private: Type* q; int front, rear, MaxSize; public: Queue(int MSize): MaxSize(MSize) { q = new Type[MaxSize]; rear=front=0; } ~Queue() { delete [] q; } bool AddQ(Type item); bool DeleteQ(Type& item); bool QFull(); bool QEmpty(); };

2.2 Trees Definition 2.1 [Tree] –A tree is a finite set of one or more nodes such that there is a special designated node called root and the remaining nodes are partitioned into n  0 disjoint sets T 1, …,T n, where each of these sets is a tree. The sets T 1, …,T n are called the subtrees of the root. Terminologies –Degree of a node and degree of a tree –Leaf node (or terminal node) and nonterminal nodes –Children, parents, siblings, ancestors –Level of a node and height (or depth) of a tree –Forest

2.2 Trees Sample tree (Figure 2.5) List representation of the tree of Figure 2.5 (Figure 2.6) –A node has three fields (tag, data, link) A C G DB E F L K H J I M level A0 BF0CG0DIJ0 EKL0 HM0

2.2.1 Binary Trees Definition 2.2 –A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left and right subtrees. Two sample binary trees –Skewed tree (Figure 2.7(a)) and complete binary tree (Figure 2.7(b)) (a)(b) C B A G D FE H I B A C D E level

2.2.1 Binary Trees Lemma 2.1 –Maximum number of nodes on level i of a binary tree = 2 i-1 –Maximum number of nodes in a binary tree of depth k = 2 k -1, k>0 Sequential representation of binary tree (Figure 2.9) Lemma 2.2 –parent(i) is at floor(i/2) if i  1 –lchild(i) is at 2i if 2i  n –rchild(i) is at 2i+1 if 2i+1  n tree A B- C- - -D - GH I DE F AB C E (1) (2) (3) (4) (5) (6) (7) (8) (9) (16)

2.2.1 Binary Trees Linked representation of binary tree (Figure 2.10) –Three fields (lchild, data, rchild) A0 B0 C0 D0 E00 tree H00I0 0 E00F00 G00 D B C A (a)(b)

2.3 Dictionaries Dictionary –An abstract data type that supports the operations insert, delete, and search –Numerous applications –Essential to support the operations as efficiently as possible –Comparison methods (eg, binary search tree) and direct access methods (eg, hashing)

2.3.1 Binary Search Trees Definition 2.3 [Binary search tree] –A binary search tree is a binary tree. It may be empty. If it is not empty, then it satisfies the following properties: Every element has a key and no two elements have the same key (distinctness) The keys (if any) in the left subtree are smaller than the key in the root The keys (if any) in the right subtree are larger than the key in the root The left and right subtrees are also binary search trees Three operations (insert, delete, and search) –Search by key or by rank

2.3.1 Binary Search Trees Example binary trees (Figure 2.11) –Which are the binary search trees ? (a) (b) (c)

2.3.1 Binary Search Trees Class definition for binary search trees (Program 2.6) class TreeNode{ friend class BSTree; private: TreeNode *lchild, *rchild; Type data; }; class BSTree { private: TreeNode* tree; TreeNode* Search(TreeNode* t, Type x);

public: BSTree() {tree=NULL;} ~BSTree() {delete tree;} TreeNode* Search(Type x); // Recursively search for x. TreeNode* ISearch(Type x); // Iteratively search for x. void Insert(Type x); // Insert x. void Delete(Type x); // Delete x. };

2.3.1 Binary Search Trees Search operation –Recursive search (Program 2.7) #define NULL 0 TreeNode* BSTree::Search(Type x) { return Search(tree, x); } TreeNode* BSTree::Search(TreeNode* t, Type x) { if (t == NULL) return 0; else if (x == t->data) return t; else if (x data) return (Search(t->lchild,x)); else return (Search(t->rchild, x)); }

2.3.1 Binary Search Trees Search operation –Iterative search (Program 2.8) #define NULL 0 TreeNode* BSTree::ISearch(Type x) { bool found = false; TreeNode* t = tree; while ((t) && (!found)) { if (x == t->data) found = true; else if (x data) t = t->lchild; else t = t->rchild; } if (found) return t; else return NULL; }

2.3.1 Binary Search Trees Insert operation –Inserting example (Figure 2.12) –Program (Program 2.10) #define NULL 0 void BSTree::Insert(Type x) // Insert x into the binary search tree. { bool notfound=true; TreeNode *p = tree, *q; (a)(b)(c)

// Search for x. q is parent of p. while ((p) && (notfound)) { q = p; // Save p. if (x == (p->data)) notfound = false; else if (x data)) p = (p->lchild); else p = (p->rchild); } // Perform insertion. if (notfound) { p = new TreeNode; p->lchild = NULL; p->rchild = NULL; p->data = x; if (tree) { if (x data)) q->lchild = p; else q->rchild = p; } else tree = p; }

2.3.1 Binary Search Trees Delete operation –Deleting example (Figure 2.13) –Program for deletion Write for yourself ! (a) (b) (c)

2.3.1 Binary Search Trees Height of a binary search tree –Worst case Height of a BST with n elements can become as large as n The case when inserting the keys [1,2,3,4, …,n] in this order –Average case When the insertions and deletions are made at random O(log n) Balanced search trees –Search trees with a worst-case height of O(log n) –AVL tree, 2-3 tree, Red-Black tree, and B-tree

2.3.1 Binary Search Trees Summary of dictionary implementations (Table 2.1)

2.4 Priority Queues Priority queue –Any data structures supporting the operations of search min (or max), insert, and delete min (or max) –Examples Example 2.2: selling the services of a machine Example 2.3: simulating a large factory Representation of priority queue –The simplest way using an unordered linear list Insert:  (1) Delete:  (n) –Use of ordered linear list Insert:  (n) Delete:  (1) –Use of max heap Insert: O(log n) Delete: O(log n)

2.4.1 Heaps Definition 2.4 [Heap] –A max (min) heap is a complete binary tree with the property that the value at each node is at least as large as (as small as) the values at its children (if they exist). Call this property the heap property. The root has the largest element. Class definition of a max heap (Program 2.11) class Heap { private: Type *array; int MaxSize, Nel; // Max. size of the heap, no. of elements void Adjust(Type a[], int i, int n); public: Heap(int MSize): MaxSize(MSize) { array = new Type[MaxSize+1]; Nel=0; }

2.4.1 Heaps Example of insert (Figure 2.14) ~Heap() { delete []array; } bool Insert(Type item); // Insert item. bool DelMax(Type& item); // Delete the maximum. };

2.4.1 Heaps Program for insert (Program 2.12) Analysis –Best case:  (1) –Worst case:  (log n) bool Heap::Insert(Type item) { // Inserts item. int i = ++Nel; if (i==MaxSize) { cout << "heap size exceeded" << endl; return false; } while ((i>1) && (array[i/2]<item)) { array[i] = array[i/2]; i /= 2; } array[i] = item; return true; }

2.4.1 Heaps Program for delete (Program 2.13) void Heap::Adjust(Type a[], int i, int n) // The complete binary trees with roots 2*i and 2*i+1 are // combined with node i to form a heap rooted at i. No // node has an address greater than n or less than 1. { int j = 2*i, item = a[i]; while (j <= n) { if ((j<n) && (a[j]<a[j+1])) j++; // Compare left and right child // and let j be the larger child. if (item >= a[j]) break; // A position for item is found. a[j/2] = a[j]; j *= 2; } a[j/2] = item; }

2.4.1 Heaps Analysis –Best case:  (1) –Worst case:  (log n) bool Heap::DelMax(Type& item) { if (!Nel) { cout << "heap is empty" << endl; return false; } item=array[1]; array[1]=array[Nel--]; Adjust(array, 1, Nel); return true; }

2.4.1 Heaps Heap sorting algorithm (Program on page 93) –To sort n elements, it suffices to make n insertions followed by n deletions from a heap. Analysis –Since insertion and deletion take O(log n) time each in the worst case, the heap sorting algorithm has a time complexity of O(n log n) void Sort(Type a[], int n) //Sort the elements a[1:n]. { Heap heap(SIZE); for (int i=1; i<=n; i++) heap.Insert(a[i]); Type x; for (i=1; i<=n; i++) { heap.DelMax(x); a[n-i+1] = x; }

2.4.1 Heaps Insert n elements using the program Sort –Example Forming a heap from {40, 80, 35, 90, 45, 50, 70} (Figure 2.15) (a) (b) (e) (c) (d)

(g) (f)

2.4.1 Heaps Insert n elements using the program Sort –Worst-case analysis Worst-case happens when the list is in ascending order O(n log n) –Average case O(n) On the average, each new value only rises a constant number of levels (why?)

2.4.1 Heaps Can we insert n elements faster? –Yes! –Using programs Heapify (Programs 2.14) void Heapify(Type a[], int n) // Readjust the elements in a[1:n] to form a heap. { for (int i=n/2; i; i--) AdjustH(a, i, n); }

Can we insert n elements faster? (Continued) –Example of Heapify (Figure 2.16) –Worst-case analysis (Formula 2.1) O(n) –However, requiring that all the elements be available before heap creation begins Heaps (a) (b) (c) (d)

2.4.2 Heapsort Program Heapsort (Program 2.15) –Worst-case analysis O(n log n) –Storage requirement A few extra variables void HeapSort(Type a[], int n) // a[1:n] contains n elements to be sorted. HeapSort // rearranges them in-place into nondecreasing order. { Heapify(a, n); // Transform the array into a heap. // Interchange the new maximum with the element // at the end of the array. for (int i=n; i>=2; i--) { Type t = a[i]; a[i] = a[1]; a[1]=t; AdjustH(a, 1, i-1); }

2.5 Graphs Introduction –Konigsberg bridge problem (Figure 2.24) –Applications: analysis of electric circuits, finding shortest routes, project planning, identification of chemical compounds, ….

2.5.2 Definitions A graph G –V and E –Terminologies Vertices, edges, directed graph, undirected graph, complete graph, adjacent and incident, subgraph, path, length, simple path, cycle, connected, connected component, strongly connected, strongly connected component, in-degree and out-degree –Examples (Figure 2.25) (c) G 1 (c) G 2 (c) G 3

2.5.2 Graph Representation Adjacency matrix (Figure 2.30)

Adjacency list (Figure 2.31) Graph Representation [1] [2] [3] [4] 2 31 [1] [2] [3] head nodes vertex link (a) G 1 (b) G 3

[1] [2] [3] [4] [5] [6] [7] [8] head nodes (c) G

2.5.2 Graph Representation Class definition of adjacency list (Program on page 120) Class Graph { private: int n; //Number of vertices struct node { int vertex; struct node* link; }; struct node* headnodes[n+1]; public: Graph() { for (int i=1; i<=n; i++) headnodes[i] = NULL; } }