MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push) -----------------------------------------



Similar documents
DATA STRUCTURES USING C

Data Structure [Question Bank]

Binary Search Trees (BST)

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Data Structures. Level 6 C Module Descriptor

Data Structures and Data Manipulation

Ordered Lists and Binary Trees

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

10CS35: Data Structures Using C

Binary Search Trees CMPSC 122

Data Structures and Algorithms

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

TREE BASIC TERMINOLOGIES

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

7.1 Our Current Model

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Converting a Number from Decimal to Binary

Analysis of a Search Algorithm

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Data Structures Using C++

PES Institute of Technology-BSC QUESTION BANK

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Algorithms and Data Structures

G. H. RAISONI COLLEGE OF ENGG NAGPUR-16 Session DEPARTMENT CSE Semester IV SUBJECT DSPD

Common Data Structures

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Chapter 3: Restricted Structures Page 1

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Stacks. Linear data structures

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Data Structures UNIT III. Model Question Answer

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

DATA STRUCTURE - STACK

Module 2 Stacks and Queues: Abstract Data Types

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Section IV.1: Recursive Algorithms and Recursion Trees

Algorithms and Data Structures Written Exam Proposed SOLUTION

Data Structure with C

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Linked Lists Linked Lists, Queues, and Stacks

Introduction to Data Structures

Glossary of Object Oriented Terms

Lecture 12 Doubly Linked Lists (with Recursion)

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Unordered Linked Lists

Chapter 14 The Binary Search Tree

Java Software Structures

Data Structures and Algorithms Stacks and Queues

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Any two nodes which are connected by an edge in a graph are called adjacent node.

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Binary Trees and Huffman Encoding Binary Search Trees

Parallelization: Binary Tree Traversal

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Exam study sheet for CS2711. List of topics

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Sequential Data Structures

The ADT Binary Search Tree

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

JAWAA: Easy Web-Based Animation from CS 0 to Advanced CS Courses

Analysis of Algorithms I: Binary Search Trees

Chapter 8: Bags and Sets

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

PROBLEMS (Cap. 4 - Istruzioni macchina)

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Introduction to Data Structures and Algorithms

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Stacks. Data Structures and Data Types. Collections

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

D06 PROGRAMMING with JAVA

Sample Questions Csci 1112 A. Bellaachia

Data Structures and Algorithms(5)

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

AP Computer Science AB Syllabus 1

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Binary Heap Algorithms

Lecture Notes on Binary Search Trees

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Symbol Tables. Introduction

Algorithms and Abstract Data Types

Transcription:

=============================================================================================================================== DATA STRUCTURE PSEUDO-CODE EXAMPLES (c) Mubashir N. Mir - www.mubashirnabi.com =============================================================================================================================== Note: The Data Structures is a fairly complicated topic with lots of small things to take care of. As such it is quite difficult not to include these small things as they make quite a difference in operating these data structures. I have tried to keep things as simple as possible, leaving difficult things out where possible. However, you can choose for yourself which portions you want discard and which ones to use in your lectures. One example of this could be the "finding of required Node" in the Deletion section of Linked List. The entire "finding" section could be replaced with: Find the required Node If Node is found...... ================================================================================= Stacks ====== We need two variables. One for the maximum limit of the Stack (MAX) and the other for keeping track of the current element to be manipulated (Current). We also declare an array for the Stack. MAX = 5 Current = 0 Array Stack[MAX] 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push) ----------------------------------------- Procedure Push (Value) If Current is equal to MAX then Output an error that the Stack is full 'This means no more space for data. Stack is full. 'Display an error message that there is no more space on the Stack.

Else Add 1 to Current Put data in Value into current position Displaying a value from the Stack (Pop) ----------------------------------------- Procedure Pop if Current is equal to Zero then Output error that Stack is empty Else Output value from Stack at Current position Subtract 1 from Current 'Go to the next available element in the Stack. 'Put (Push) the data sent in Value parameter in the new element. 'If there are no values in the Stack. 'Nothing to output. 'Output (POP) a value in the Current element (LIFO). =============================================================================================== QUEUES ======= Prerequsites: End = 0 MAX = 5 Array Queue[MAX] To track the End element of the Queue. Maximum number of elements (or whatever value you like as the number of elements in the Queue) Declares an array of 5 elements for the Queue. Adding a Value -------------------- Procedure Insert(Value) If End is equal to MAX then Output an error message that the Queue is full Add 1 to End Store data in Value into Queue at End position 'No more space in Queue 'Go to next available element in Queue

Displaying value from Queue ---------------------------------- Procedure Display If End is equal to Zero then Output an error message that the Queue is empty Display value at position Queue[1] Start Loop from position 1 to End position Copy data from Next element to Current element Move to Next element EndIf 'No values to display 'Always display first value (FIFO) 'Looping through all elements with values 'Moving data is step on its left to fill the gap in the 1 position ===================================================================================================== Linked List ========== Declaring a Linked List ----------------------------- Integer : Data Pointer: Next Declaring variables ----------------------- Pointer : Start Start = NULL 'Nothing in the Linked List Creating a New Node with above structure. --------------------------------------------------- Procedure Create(Value) If there is enough memory available then Create New Node in memory Point its Next pointer to NULL If this is the first Node in the List then Point Start to the Node 'Only create a Node if memory is available 'Creating a New Node for the Linked List 'Making a termination point for the Node 'There must be a starting point for the Linked List 'Point Start to New Node only if it is the very first one

Go to memory location pointed by Start pointer Start Loop and run until Next points to NULL Go to the Node pointed by Next pointer Point Next of last Node to the newly created Node Put data in Value in new Node's Data Output error message that there is no more memory available EndIf 'If other Nodes exist then find the last Node in the List 'Begin at the Start (first Node) 'Check each Node until Next pointer points to NULL 'Move from Node to Node 'When last Node found, point its Next pointer to New Node 'Put the required data (Value) in New Node's Data variable 'Out of memory. Can't create a New Node Deleting a Node from the Linked List (assuming values in Data are unique) ----------------------------------------------------------------------------------------- Procedure Delete(Value) Pointer : Found Found = NULL 'Pointer for finding required Node to Delete 'Assuming required Node not found If there are no Nodes in the List then Output error message that can't delete a Node Go to memory location pointed by Start pointer Start Loop and run until Next points to NULL If value in Data of current Node is equal to Value then Found = current Node Exit Loop Go to Node pointed by Next pointer of current Node If the required Node is the first Node in the List then Point the Start pointer to NULL 'Begin at the Start (first Node) 'Check each Node until Next pointer points to NULL 'If we find our required Node 'Mark the current Node with Found pointer 'No need to finish the loop 'Node deleted Point Next of previous Node to the Node pointed by Next of Found 'Node deleted (Previous.Next = Found.Next)

Displaying the entire Linked List ------------------------------------------ Procedure Display If Start points to NULL then Output error message that the List is empty Start Loop at Start position and run until Next points to NULL Output current Node's Data Go to Node pointed by Next pointer of current Node 'List is empty ==================================================================================================== Binary Trees ========== Structure of Binary Tree Node Integer : Data Pointer : Left Pointer : Right Pointer : Start Pointer : Current Start = NULL Current = NULL Insertion in a Binary Tree ------------------------------ Procedure Insertion(Value) Create a New Node Point its Left and Right pointers to NULL If Start is pointing to NULL then Point Start to the New Node Current = Start Start Loop at Current and run until Left and Right pointers are NULL 'Tree was empty so insert at Start 'Search for appropriate position

If data in Value is less than or equal to Data in Current Node then Go to the left of the Current Node Go to the right of the Current Node If data in New Node is less than data in Current Node then Point the Left pointer of Current Node to the New Node Point the Right pointer of Current Node to the New Node Insert data in Value in the New Node Traversals of Binary Tree --------------------------------- Procedure InOrder(Current) If Left pointer of Current is not NULL then Call InOrder with Left pointer of Current Output Data in Current Node If Right pointer of Current is not NULL then Call InOrder with Right pointer of Current 'This is a recursive call from InOrder to itself 'This is a recursive call from InOrder to itself Procedure PreOrder(Current) Output Data in Current Node If Left pointer of Current is not NULL then Call InOrder with Left pointer of Current If Right pointer of Current is not NULL then Call InOrder with Right pointer of Current 'This is a recursive call from PreOrder to itself 'This is a recursive call from PreOrder to itself

Procedure PostOrder(Current) If Left pointer of Current is not NULL then Call InOrder with Left pointer of Current 'This is a recursive call from PostOrder to itself If Right pointer of Current is not NULL then Call InOrder with Right pointer of Current 'This is a recursive call from PostOrder to itself Output Data in Current Node ===========================================================================================================================