Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Similar documents
Cpt S 122 Data Structures. Data Structures

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Cpt S 122 Data Structures. Data Structures Trees

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

12 Abstract Data Types

Program Design (II): Quiz2 May 18, 2009 Part1. True/False Questions (30pts) Part2. Multiple Choice Questions (40pts)

(1-2) Introduction to C Data Structures & Abstract Data Types. Instructor - Andrew S. O Fallon CptS 122 (June 6, 2018) Washington State University

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Data Structure. IBPS SO (IT- Officer) Exam 2017

Trees. (Trees) Data Structures and Programming Spring / 28

CP2 Revision. theme: dynamic datatypes & data structures

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Name CPTR246 Spring '17 (100 total points) Exam 3

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Binary Trees, Binary Search Trees

Largest Online Community of VU Students

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

CSE 373 Spring Midterm. Friday April 21st

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Course Review. Cpt S 223 Fall 2009

Stacks, Queues and Hierarchical Collections

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

3137 Data Structures and Algorithms in C++

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

Computer Science 1 Study Union Practice Problems. What are the arguments to malloc? calloc? realloc? What do they do?

Why Do We Need Trees?

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Data Structures Question Bank Multiple Choice

Data Structure Advanced

CS301 - Data Structures Glossary By

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

(3-2) Basics of a Stack. Instructor - Andrew S. O Fallon CptS 122 (January 26, 2018) Washington State University

TREES. Trees - Introduction

Data Structure (CS301)

DYNAMIC MEMORY ALLOCATION AND DEALLOCATION

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Linked List. April 2, 2007 Programming and Data Structure 1

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Course Review. Cpt S 223 Fall 2010

The questions will be short answer, similar to the problems you have done on the homework

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

CS 241 Data Organization Binary Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Course Review for Finals. Cpt S 223 Fall 2008

Binary Trees and Binary Search Trees

CS24 Week 4 Lecture 2

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Chapter 20: Binary Trees

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

STACKS AND QUEUES. Problem Solving with Computers-II

Data Structures and Algorithms for Engineers

Summer Final Exam Review Session August 5, 2009

Postfix (and prefix) notation

Trees, Binary Trees, and Binary Search Trees

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

This examination has 11 pages. Check that you have a complete paper.

Object Oriented Programming COP3330 / CGS5409

CSC 1600 Memory Layout for Unix Processes"

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

CMSC 341 Lecture 10 Binary Search Trees

Recursive Data Structures and Grammars

In Java we have the keyword null, which is the value of an uninitialized reference type

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CS106X Handout 39 Autumn 2012 November 28 th, 2012 CS106X Midterm Examination II

CSCI 171 Chapter Outlines

Department of Computer Science and Technology

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

malloc(), calloc(), realloc(), and free()

Object-Oriented Programming

Programming II (CS300)

3137 Data Structures and Algorithms in C++

DAY 3. CS3600, Northeastern University. Alan Mislove

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Abstract Data Types. Definitions, Implementations, and Uses

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

COP 3502 Section 2 Exam #2 Version A Spring /23/2017

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Draw a diagram of an empty circular queue and describe it to the reader.

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Transcription:

Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where: In Class Closed book, Closed notes Comprehensive Material for preparation: Lecture Slides Quizzes, Labs and Programming assignments Deitel & Deitel book (Read and re-read Chapter 12)

Course Overview Functions (Chapter 5) Function Call Stack and Stack Frames Pass-by-value and Pass-by-reference Pointers (Chapter 7) Pointer Operators Passing Arguments to Functions by Reference const qualifiers with Pointers Characters and Strings (Chapter 8) Fundamentals of Strings and Characters

Course Overview Data Structures (Chapter 12) Self Referential Structures Dynamic Memory Allocation Linked Lists, Stacks and Queues Insert, delete, isempty, printlist Binary Trees, Binary Search Trees Tree Traversals inorder, preorder, postorder

Functions Review (Chapter 5) The data structure working behind the scenes supports the function call/return mechanism. Stack Supports the creation, maintenance and destruction of each called function s automatic variables. Stack overflow error If more function calls occur since the amount of memory is finite.

Functions Review (Chapter 5) Two ways to pass arguments pass-by-value and pass-by-reference. passed by value a copy of the argument s value is made and passed to the called function. changes to the copy do not affect an original variable s value in the caller. Pass-by-value is used whenever the called function does not need to modify the value of the caller s original variable. passed by reference the caller allows the called function to modify the original variable s value.

Pointers Review (Chapter 7) Enables programs to simulate pass-by-reference. pass functions between functions. For creating and manipulating dynamic data structures (i.e., data structures that can grow and shrink at execution times). Linked lists, queues, stacks, trees.

Pointer Variable Definitions and Initialization Pointers are variables whose values are memory addresses. Normally, a variable directly contains a specific value. A variable directly references a value. A pointer contains an address of a variable that contains a specific value. A pointer indirectly references a value. Indirection means referencing a value through a pointer. count countptr Memory Location of 7 7 count 7 count directly references a variable that contains the value 7. Pointer countptr indirectly references a variable that contains the value 7.

Using the const Qualifier with Pointers The const qualifier enables you to inform the compiler that the value of a particular variable should not be modified. Four ways to pass a pointer to a function Non-constant pointer to non-constant data Non-constant pointer to constant data Constant pointer to non-constant data Constant pointer to constant data

Using the const Qualifier with Pointers Non-constant pointer to non-constant data The data can be modified through the dereferenced pointer, and the pointer can be modified to point to other data items. Non-constant pointer to constant data The non-constant pointer to constant data can be modified to point to any data item of the appropriate type, but the data to which it points cannot be modified.

Using the const Qualifier with Pointers Constant pointer to non-constant data A constant pointer to non-constant data always points to the same memory location, and the data at that location can be modified through the pointer. Constant pointer to constant data Such a pointer always points to the same memory location, and the data at that memory location cannot be modified.

Void, Null, Dangling Pointer The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type. A pointer to void cannot be dereferenced. A null pointer has a value reserved for indicating that the pointer does not refer to a valid object. A dangling pointer is a pointer to storage that is no longer allocated.

Characters and Strings (Chapter 8) A char * needs to point to some allocated memory this memory may be allocated dynamically or via another automatic local variable A str[] has memory associated with it str refers to the address of the 0 th element in the array A char *str[] is an array of pointers each pointer could refer to dynamically allocated memory or automatic local variable memory A str[][] may be considered an array of strings or a 2-D array of characters all required memory is allocated upfront; str[] (one index specified) refers to the start of the corresponding row

Data Structures (Chapter 12) Dynamic data structures with sizes that grow and shrink at execution time Linked lists are collections of data items lined up in a row insertions and deletions are made anywhere in a linked list. Stacks are important in compilers and operating systems insertions and deletions are made only at one end of a stack its top. Queues represent waiting lines insertions are made only at the back (also referred to as the tail) of a queue and deletions are made only from the front (also referred to as the head) of a queue. Binary trees facilitate high-speed searching and sorting of data efficient elimination of duplicate data items, representing file system directories and compiling expressions into machine language.

Abstract Data Type Data abstraction and abstract data types (ADT). notion of an object (from object-oriented programming) is an attempt to combine abstractions of data and code. ADT is a set of objects together with a set of operations e.g., List, Operations on a list: Insert, delete, search, sort C++ class are perfect for ADTs Enable us to build the data structures in a dramatically different manner designed for producing software that s much easier to maintain and reuse.

Self Referential Structures A self-referential structure contains a pointer member that points to a structure of the same structure type. For example, the definition struct node { int data; struct node *nextptr; }; // end struct node defines a type, struct node. A structure of type struct node has two members integer member data and pointer member nextptr.

Dynamic Memory Allocation (Cont.) Function malloc takes as an argument the number of bytes to be allocated returns a pointer of type void * (pointer to void) to the allocated memory. Function malloc is normally used with the sizeof operator. A void * pointer may be assigned to a variable of any pointer type.

Dynamic Memory Allocation (Cont.) For example, the statement newptr = malloc( sizeof( struct node ) ); evaluates sizeof(struct node) to determine the size in bytes of a structure of type struct node, allocates a new area in memory of that number of bytes and stores a pointer to the allocated memory in variable newptr. The allocated memory is not initialized. If no memory is available, malloc returns NULL.

Dynamic Memory Allocation (Cont.) Function free deallocates memory the memory is returned to the system so that it can be reallocated in the future. To free memory dynamically allocated by the preceding malloc call, use the statement free( newptr ); C also provides functions calloc and realloc for creating and modifying dynamic arrays. calloc allocates multiple blocks of storage, each of the same size. realloc changes the already allocated memory size.

Memory Leak When using malloc test for a NULL pointer return value. Memory Leak: Not returning dynamically allocated memory when it s no longer needed can cause system to run out of memory prematurely. This is known as memory leak. Use free to return the memory to system.

Memory Allocation Process C programming language manages memory statically, automatically, or dynamically. Local Variables, Automatic variables, Function Calls Free/Dynamic Memory Stack Heap Global & Static Variables C Program Instructions Permanent Storage Area Conceptual view of storage of a C program in memory

Linked Lists A linked list is a linear collection of self-referential structures known as nodes, connected by pointer links. A linked list is accessed via a pointer to the first node of the list. Subsequent nodes are accessed via the link pointer member stored in each node. The link pointer in the last node of a list is set to NULL to mark the end of the list. Data is stored in a linked list dynamically each node is created as necessary.

Linked Lists Functions The primary functions of linked lists are insert and delete. Function isempty If the list is empty, 1 is returned; otherwise, 0 is returned. Function printlist prints the list.

Insert Example

Function insert

Function insert

delete Example

Function delete

Function delete

Function printlist

Linked List Practice Exercise Printing the elements of a Linked List in reverse: Write a C program to print the linked list elements in reverse order Either using a data structure discussed in the class Or using recursive function calls Inserting into an Ordered List: Write a C program to insert 25 random integers from 0 to 100 in descending order in a linked list.

Stacks A stack can be implemented as a constrained version of a linked list. New nodes can be added to a stack and removed from a stack only at the top. For this reason, a stack is referred to as a last-in, first-out (LIFO) data structure. A stack is referenced via a pointer to the top element of the stack. The link member in the last node of the stack is set to NULL to indicate the bottom of the stack.

Stack Functions The primary functions of stacks are push and pop. Function isempty tests if stack is empty. Function printlist prints the stack elements.

push operation

Function push

pop operation

Function pop

Applications of Stacks Balancing symbols Compiler checks for program syntax errors Every right brace, bracket, and parenthesis must correspond to its left counterpart The sequence [()] is legal, but [(]) is wrong Infix to Postfix Conversion Postfix Expressions Evaluations

Stack Exercise Think of using Stack to implement Linked List function printlistreverse(): printing linked list elements in reverse order Think of using Stack to implement Queue function enqueue and dequeue. Write a C program to implement push and pop.

Queues Queue is another common data structure. A queue is similar to a checkout line in a grocery store the first person in line is serviced first, and other customers enter the line only at the end and wait to be serviced. Queue nodes are removed only from the head of the queue and are inserted only at the tail of the queue. For this reason, a queue is referred to as a first-in, first-out (FIFO) data structure. The insert and remove operations are known as enqueue and dequeue, respectively.

Queue Functions The primary functions of queues are enqueue and dequeue. Function isempty tests if queue is empty. Function printlist prints the queue elements.

Operation enqueue The dotted arrows in part (b) illustrate Steps 2 and 3 of function enqueue that enable a new node to be added to the end of a queue that is not empty.

Function enqueue

Operation dequeue Part (b) shows tempptr pointing to the dequeued node, and headptr pointing to the new first node of the queue. Function free is used to reclaim the memory pointed to by tempptr.

Function dequeue

Queue Exercise Implement the level order binary tree traversal using queue data structure. Write the pseudo code of this algorithm. Write a C program to implement enqueue and dequeue.

Level Order Binary Tree Traversal Use the Queue data structure to control the output of the level order binary tree traversal. Algorithm Insert/enqueue the root node in the queue While there are nodes left in the queue, Get/dequeue the node in the queue Print the node s value If the pointer to the left child of the node is not null Insert/enqueue the left child node in the queue If the pointer to the right child of the node is not null Insert/enqueue the right child node in the queue

Implementing linked list, stack, queue functions

Trees Binary Trees Binary Search Tree insertnode, deletenode inorder, preorder, postorder Applications Duplicate elimination, Searching etc

Trees Linked lists, stacks and queues are linear data structures. A tree is a nonlinear, two-dimensional data structure with special properties. Tree nodes contain two or more links.

Trees

Binary Search Tree (BST) Binary Search Tree (BST) Binary search tree (BST) is a tree where: For any node n, items in left subtree of n item in node n items in right subtree of n Average depth of a BST is O(logN) Operations: Insert, Delete, Search, FindMin, FindMax, traversals Which one is a BST?

Function insertnode

Function insertnode

Tree Traversals Binary Tree Traversals Pre-order traversal: root, left, right The work at a node is performed before (pre) its children are processed In-order traversal: left, root, right Post-order traversal: left, right, root The work at a node is performed after (post) its children are evaluated Constructing a Binary Tree form pre-order, in-order and post-order traversals

Tree Exercise: Construct a Binary Tree from given in-order and preorder tree traversals output: The inorder traversal of the tree is: left, root, right 6 13 17 27 33 42 48 The preorder traversal of the tree is: root, left, right 27 13 6 17 42 33 48

Tentative Midterm Exam#1 Structure Part I: Conceptual Questions Short answer, Fill-in-the-blank, Multiple-choice, and True/False ( 30 pts ) Part II: Programming Questions Write C code ( 70 pts ) 4 programming questions mainly on data structures

Tips: Deitel & Deitel book (Read and re-read Chapter 12). Practice the Self-Review Exercises and Exercises from Chapter 12 in the textbook. Good Luck!