Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Advertisements

1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
Connecting with Computer Science, 2e
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
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.
Important Problem Types and Fundamental Data Structures
Binary Trees Chapter 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.
Copyright © Wondershare Software Introduction to Data Structures Prepared by: Eng. Ahmed & Mohamed Taha.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
Connecting with Computer Science, 2e Chapter 8 Data Structures.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
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.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Chapter 8 Data Abstractions. © 2005 Pearson Addison-Wesley. All rights reserved 8-2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
Pointers and Linked Lists
Data Structure By Amee Trivedi.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Pointers and Linked Lists
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Top 50 Data Structures Interview Questions
12 C Data Structures.
Chapter 15 Lists Objectives
Objectives In this lesson, you will learn to: Define stacks
Stack and Queue APURBO DATTA.
Chapter 8: Data Abstractions
Chapter 20: Binary Trees.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Pointers and Linked Lists
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
Chapter 21: Binary Trees.
Pointers and Linked Lists
Chapter 8: Data Abstractions
Binary Tree Traversals
Important Problem Types and Fundamental Data Structures
Heaps Chapter 6 Section 6.9.
LINEAR DATA STRUCTURES
Presentation transcript:

Chapter 16 – Data Structures and Recursion

Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1

Programmer-defined Linked List Class u Data members (could be different types) stored in (usually) contiguous block of memory –One of the data members is a pointer variable t Address stored points to the next object u Data member that has address creates link between objects u Each object referred to as a node Lesson 16.1

Linked List Representation Lesson 16.1 Head node Object 2 Object 3 Object 4 Objects in linked list link Pointer variable value Fact that address points to next object

Actions on Linked List u Must go node to node from head node u Can delete node by making address that points to one to be deleted to next object u Can insert node by changing address stored in pointer variable for node preceding location of insertion u Can move node from one location to another u Must keep track of current node and head node Lesson 16.1

Linked List Classes u Use two classes to create linked list –Node class t Holds only the data t All data members public because no function members –Second used to manipulate nodes t One variable holds address of first object in list t Another variable holds current node address Lesson 16.1

Node Class u General form: Lesson 16.1 class Node { public: type member1; type member2; … Node* next_node; }; Class name Data types (not necessarily all the same) Identifiers representing node data Name for storing address of next node in list

Second Class  Used to manipulate the nodes  Data only pointer variables used to hold addresses Lesson 16.1 class Llist { private: Node* head; Node* current; public: void make_list ( ); void show_list ( ); }; Address of node being manipulated Address of first object

Stack u Data structure created using linked list model u With stack can perform only two fundamental operations –Push t Insert node immediately after the head –Pop t Retrieve and delete node immediately after head u Only work with nodes at top Lesson 16.2

Stack Classes u Two classes create stack –One class holds only data t Same form as linked list t one member must be pointer variable –Second class used to manipulate nodes t Data only pointer variables used to hold addresses of nodes t Nodes of interest for stack are head and tail t Function members initialize, push and pop items Lesson 16.2

Stack Class Lesson 16.2 class Stack { private: Node* head; Node* tail; public: Stack ( ); void push (int); int pop ( ); }; Address of head node and last node

Stack Classes u Create empty stack by initializing a head and tail node –Allow finding top and bottom of stack u Should not pop from an empty stack u LIFO –Last In First Out t Last node pushed is first node popped off Lesson 16.2

Queue Class u Can create with linked list form –Must have a head and tail node u FIFO –First node inserted into queue is first node removed u Nodes are inserted at tail of queue and removed from head of queue Lesson 16.3

Queue Class Lesson 16.3 class Queue { private: Node* head; Node* tail; public: Queue ( ); void insert (int); int remov ( ); }; Class Node { public: type member; Node* next_node; };

Types of Queues u Dequeue - "double-ended queue" –Nodes can be inserted at either end and removed from either end u Output restricted dequeue –Insertion allowed at both ends but removal allowed at only one end u Input restricted dequeue –Removal allowed at both ends, insertion allowed at only one end u Priority queue –Priority for each node – highest priority processed first Lesson 16.3

Binary Tree u Tree is particular type of graph –Binary tree is particular type of tree u Graph is data structure that includes nodes –each node can have more than one pointer Lesson 16.4 Linked List Graph

Graph Terminology u Nodes can also be called vertices or points u Connections between nodes called edges or arcs u Two nodes considered adjacent of neighbors if edge connects them u Path between one node and another indicated by list of connect nodes between two u Simple path is path with no repeated nodes Lesson 16.4

Graphs u Weighted graph –Assign length or cost of each edge u Tree –graph with characteristic that there is only one path between any two nodes –Rooted tree t Tree with one node specified to be root t Root traditionally shown at top of diagram –Binary tree t Tree in which no node has more than two children Lesson 16.4

Tree Class  Similar to linked list and stack classes  Two classes –One class holds content of nodes –Second class manipulates the nodes Lesson 16.4 class Tree_node { public: type member; Tree_node* left_child; Tree_node* right_child; };

Recursion u Within function body there is call to function with identical name and signature u Basic action which is repeated until it reaches the final iteration of the basic action Lesson 16.5

Summary u Create a linked list u Create a stack u Create a queue u Create a binary tree u Identify recursive functions Learned how to: