Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Tree representation and tree search - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 10.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
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.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Information and Computer Sciences University of Hawaii, Manoa
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Pointers & Dynamic Data Structures Chapter Dynamic Data Structures t Arrays & structs are static (compile time) t Dynamic expand as program executes.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Programming Abstractions Cynthia Lee CS106X. Topics:  Finish up heap data structure implementation › Enqueue (“bubble up”) › Dequeue (“trickle down”)
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
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.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
CSCS-200 Data Structure and Algorithms Lecture
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Chapter 12 – Data Structures
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Lecture No.15 Data Structures Dr. Sohail Aslam
Stacks Stack: restricted variant of list
Chapter 20: Binary Trees.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Chapter 21: Binary Trees.
Lecture No.16 Data Structures Dr. Sohail Aslam.
Operations on Binary Tree
Iterators Professor Hugh C. Lauer CS-2303, System Programming Concepts
Stacks with Dynamic Memory
Pointers & Dynamic Data Structures
Queues Jyh-Shing Roger Jang (張智星)
By C. Shing ITEC Dept Radford University
Chapter 20: Binary Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Presentation transcript:

Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University

Slide 2 Objectives Understand how to use template structures for linked list, stack, queue, binary search tree Understand how to use STL (Standard Template Library) for vector, list, stack, queue and tree Understand how to use algorithms

Slide 3 Data Structure Using Template Class A Node class: Member data: Info field Next field: link to another Node Member functions: Accessors: getInfo getNext Mutators: setInfo setNext

Slide 4 Data Structure Using Template Class (Cont.) A Node class Implementation: template class Node { public: Node(): next(NULL){} Node(const T &i, Node *n): info(i), next(n) {} T getInfo() const {return info;} Node * getNext() const {return next;} void setInfo(T i) {info=i;} void setNext(Node *n) {next=n;} ~Node() {}

Slide 5 Data Structure Using Template Class (Cont.) A Node class Implementation: (Cont.) private: T info; Node *next; };

Slide 6 Iterator Iterator is a class that allows you to cycle through any element in a data structure I contains Member data Pointer to the current node: current Member functions ++ (prefix and postfix) == != *

Slide 7 Iterator (Cont.) Iterator Implementation: class Iterator { public: Iterator(): current(NULL) {} Iterator(Node * nd): current(nd) {} Iterator operator++(){current=current->getNext(); return *this;}; // prefix form // postfix form Iterator operator++(int){Iterator old=current; current=current->getNext(); return old;} bool operator == (const Iterator &rs) const {return current==rs.current;} bool operator != (const Iterator &rs) const {return current!=rs.current;} const T operator *() const {return current->getInfo();} ~Iterator() {}

Slide 8 Iterator (Cont.) Iterator Implementation: (Cont.) private: Node *current; };

Slide 9 Linked List Linked nodes using Iterator: template class List { public: List(): head(NULL), count(0) {} void insertInfo(const T &i); void deleteInfo(const T &i); T getCount() const {return count;} Iterator begin() {return Iterator (head);} Iterator end() {return Iterator ();} ~List() {}

Slide 10 Linked List (Cont.) Linked nodes using Iterator: (Cont.) template private: Node *head; int count; };

Slide 11 Linked List (Cont.) Linked nodes using Iterator: (Cont.) Operations: Insert item: template void List ::insertInfo(const T &i) Delete item: template void List ::deleteInfo(const T &i)

Slide 12 Linked List (Cont.) Example: list.cpp

Slide 13 Data Structure - stack Stack: insert (push) and delete (pop) at the same place (top).

Slide 14 Stack Implementation Write a program that implements a stack of integers using a linked list. template class Stack { public: Stack(): top(NULL) {} void push(const T &i); T pop(); Iterator begin() {return Iterator (top);} Iterator end() {return Iterator ();} bool isEmpty() const; ~Stack();

Slide 15 Stack Implementation (Cont.) Write a program that implements a stack of integers using a linked list. (Cont.) private: Node *top; };

Slide 16 Stack Implementation (Cont.) Stack operations: Push Pop isEmpty

Slide 17 Stack Implementation (Cont.) Stack operations: (Cont.) template void Stack ::push(const T &i) { Node *newnode=new Node ; newnode->setInfo(i); newnode->setNext(top); top=newnode; }

Slide 18 Stack Implementation (Cont.) Stack operations: (Cont.) template T Stack ::pop() { if (isEmpty()) { cout <<" Error: Stack is empty, cannot pop out.\n"; exit(1);} T item=top->getInfo(); Node *deleted=top; top=top->getNext(); delete deleted; return item; }

Slide 19 Stack Implementation (Cont.) Stack operations: (Cont.) template bool Stack ::isEmpty() const { return top==NULL; }

Slide 20 Stack Implementation (Cont.) Example: stack.cpp

Slide 21 Data Structure - Queue Insert (enqueue) at the tail and Delete (dequeue) at the head.

Slide 22 Example - Queue Write a program that implements a queue of integers using a linked list. template class Queue { public: Queue(): head(NULL), tail(NULL) {} void enqueue(const T &i); T dequeue(); Iterator begin() {return Iterator (head);} Iterator end() {return Iterator ();} bool isEmpty() const; ~Queue();

Slide 23 Example – Queue (Cont.) Write a program that implements a queue of integers using a linked list. (Cont.) private: Node *head, *tail; };

Slide 24 Example – Queue (Cont.) Queue operations: enqueue dequeue isEmpty

Slide 25 Example – Queue (Cont.) Queue operations Implementation: template void Queue ::enqueue(const T &i) { if (isEmpty()) head=tail=new Node (i, NULL); else { tail->setNext(new Node (i, NULL)); tail=tail->getNext(); }

Slide 26 Example – Queue (Cont.) Queue operations Implementation: (Cont.) template T Queue ::dequeue() { if (isEmpty()) { cout <<" Error: Stack is empty, cannot pop out.\n"; exit(1);} T item=head->getInfo(); Node *deleted=head; head=head->getNext(); delete deleted; return item; }

Slide 27 Example – Queue (Cont.) Queue operations Implementation: template bool Queue ::isEmpty() const { return (head==NULL) && (tail==NULL); }

Slide 28 Queue Implementation Example: queue.cpp

Slide 29 Data Structure – Binary Tree Each (parent) node has at most 2 children Binary search tree: right child node value > parent node value > left child value

Slide 30 Example – Binary search tree Write a program that implements a binary search tree of integers using a linked list.

Slide 31 Example – Binary search tree (Cont.) First define a TreeNode: Member data: Info field Left field: link to left TreeNode Right field: link to right TreeNode Member functions: Accessors: getInfo getLeft getRight Mutators: setInfo setLeft setRight

Slide 32 Example – Binary search tree (Cont.) TreeNode implementation: template class TreeNode { public: TreeNode(): left(NULL), right(NULL){} TreeNode(const T &i, TreeNode *l,TreeNode *r): info(i), left(l), right(r) {} T getInfo() const {return info;} TreeNode * &getLeft() {return left;} TreeNode * &getRight() {return right;} void setInfo(T i) {info=i;} void setLeft(TreeNode *l) {left=l;} void setRight(TreeNode *r) {right=r;} ~TreeNode() {}

Slide 33 Example – Binary search tree (Cont.) TreeNode implementation: (Cont.) private: T info; TreeNode *left; TreeNode *right; };

Slide 34 Example – Binary search tree (Cont.) Binary Search Tree operations: Insert Delete isEmpty Inorder traversal

Slide 35 Example – Binary search tree (Cont.) Binary Search Tree specification: template class BST { public: BST(): root(NULL) {} void insert (T i); void deleteInfo (T i); void inOrder() const; bool isEmpty() const {return root==NULL;} ~BST();

Slide 36 Example – Binary search tree (Cont.) Binary Search Tree specification: (Cont.) private: void insert(TreeNode *&r, T i); void deleteInfo(TreeNode *&r, T i); void deleteInfo(TreeNode *&r); void getPredecessor(TreeNode *&r, T &i); void deleteTree(TreeNode *r); void inOrder(TreeNode *r) const; TreeNode *root; };

Slide 37 Example – Binary search tree (Cont.) Binary Search Tree Implementation: template void BST ::insert(T i) { insert(root,i); }

Slide 38 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::insert(TreeNode * &r, T i) { if (r==NULL) r=new TreeNode (i, NULL, NULL); else { if (i> r->getInfo()) { insert(r->getRight(),i); } else { insert(r->getLeft(),i);} }

Slide 39 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::deleteInfo(T i) { deleteInfo (root,i); }

Slide 40 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (cont.) template void BST ::deleteInfo (TreeNode * &r, T i) { if (i > r->getInfo()) { deleteInfo (r->getRight(),i); } else if (i getInfo()) { deleteInfo (r->getLeft(),i); } else deleteInfo(r); }

Slide 41 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::deleteInfo(TreeNode *&r) { T item; TreeNode *temp=r; if (r->getLeft() == NULL) { r=r->getRight(); delete temp; } else if (r->getRight() == NULL) { r=r->getLeft(); delete temp; } else { getPredecessor(r->getLeft(), item); r->setInfo(item); deleteInfo (r->getLeft(), item); }

Slide 42 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::getPredecessor (TreeNode *&r, T &item) { while (r->getRight() != NULL) r=r->getRight(); item=r->getInfo(); }

Slide 43 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::deleteTree(TreeNode *r) { if (r != NULL) { deleteTree(r->getRight()); deleteTree(r->getLeft()); delete r; }

Slide 44 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::inOrder() const { inOrder(root); }

Slide 45 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template void BST ::inOrder(TreeNode *r) const { if (r != NULL) { inOrder(r->getLeft()); cout getInfo()<<'\n'; inOrder(r->getRight()); }

Slide 46 Example – Binary search tree (Cont.) Binary Search Tree Implementation: (Cont.) template BST ::~BST() { deleteTree(root); }

Slide 47 Binary search tree Implementation Example: tree.cpp

Slide 48 Standard Template Library STL: library that contains useful template data structures Container adapter: implemented on top of deque template class stack queque Container: use iterator to traverse items Sequential container: vector list Associative container: set map

Slide 49 STL - stack Put #include Has member functions: size empty top push pop

Slide 50 STL – stack (Cont.) Example: stack.cpp

Slide 51 STL - queue Put #include Has member functions: size empty front back push pop

Slide 52 STL – queue (Cont.) Example: queue.cpp

Slide 53 STL – sequential container Has member functions: size begin: for iterator end: for iterator rbegin: for reverse iterator rend: for reverse iterator push_front push_back front insert clear erase

Slide 54 STL - vector Put #include Self-grown array

Slide 55 STL – vector (Cont.) Example: vector.cpp vector_data.txt vector_iterator.cpp

Slide 56 STL – list Put #include Example: list_iterator.cpp

Slide 57 STL – deque Put #include Example: deque_iterator.cpp

Slide 58 STL – associative container Associate a key with a value Has member functions: size empty find insert erase

Slide 59 STL - set Put #include Item can be duplicated

Slide 60 STL – set (Cont.) Example: set_iterator.cpp set_data.txt

Slide 61 Generic Algorithm Basic template functions in STL Put #include

Slide 62 Generic Algorithm (Cont.) Sorting algorithm: has function binary_search Set algorithm: has template functions include set_union set_intersection set_difference set_symmetric_difference

Slide 63 Sort Algorithm Example: vectorsort_iterator.cpp vector_data.txt

Slide 64 Set Algorithm Example: setop_iterator.cpp setop_data.txt

Slide 65 References Deitel & Deitel: C How to Program, 4th ed., Chapter 21, Prentice Hall Deitel & Deitel: C++ How to Program, 4th ed., Chapter 21, Prentice Hall