0% found this document useful (0 votes)
23 views33 pages

005 Allied III - Data Structure - III Sem

Data structure topics

Uploaded by

Pavani akumarthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views33 pages

005 Allied III - Data Structure - III Sem

Data structure topics

Uploaded by

Pavani akumarthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

STUDY MATERIAL FOR BCA

DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

UNIT CONTENT PAGE Nr

I PSEUDOCODE 02

II LINEAR LIST CONCEPTS 09

III STACK 16

IV BASIC TREE CONCEPTS 24

V GENERAL SORT CONCEPTS 29

Page 1 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

UNIT - I
PSEUDOCODE
 Pseudocode is an English like representation of the code required for an algorithm
 Although several tools are used to define algorithms, one of the most common is pseudo code.
 The code pat consists of an extended version of the basic algorithms constructs- sequence,
selection and iteration.
 In this text we use pseudocode for both data structures and code
 The basic format for data types consists of the name of the data and its type enclosed in pointed
brackets as shown below
count <integer>
 The structure of the data is indicated by indenting the data items as shown below
node
data <data type>
link <pointer to node>
end node
 Each algorithm begins with a header that names it, describes its parameters and lists any pre
and post conditions.
Statement numbers:
 Statements are numbered by using an abbreviated decimal notation in which only the last of the
number sequence in shown on each statement.
Variables:
 It is not necessary to define every variable used in the algorithm, especially when the context of
the data is indicated by its name.
 Do not use single character names. Even the traditional for loop variables of i and j should be
avoided, although we sometimes use them in C++ for loops.
Algorithm analysis:
 We follow the algorithm with an analysis section that explains some of its salient points.
 Not every line of code is explained.
 It also often introduces style or efficiency considerations
 Statement Constructs:
 Any algorithm could be written with only three programming constructs: sequence, selection
and loop.
Sequence:
 A sequence is a series of statements that do not alter the execution path within an algorithm
Selection:
 Selection statements evaluate one or more alternatives.
 If the alternatives are true, one path is taken. If the alternatives are false, a different path is
taken.
Loop:
 Loop iterates a block of code.
 The loop that we use in our pseudocode most closely resembles the while loop.

Page 2 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

 It is a pretest loop, that is, the condition is evaluated before the body of the loop is executed.
 If the condition is true, the body is executed. If the condition is false, the loop terminates.
The Abstract Data Type
 In the history of programming concepts, we started with nonstructured linear programs, known
as spaghetti code, in which the logic flow could through the program like spaghetti on a plate.
 Next came the concept of modular programming, in which programs were organized in
functions each of which still used a linear coding technique.
 A data type consists of two parts, a set of data and the operations that can be performed on the
data.
 The latest development in the theory of program design is object-oriented programming.
 In an object-oriented approach, the functions are developed around an object, such as a linked
list.
 One part of the object-oriented concept is encapsulation.
Atomic and Composite Data:
 Atomic data are data that we choose to consider as a single, nondecomposable entity.
 For example, the integer 4562 may be considered as a single integer value.
 An atomic data type is a set of atomic data with identical properties. These properties
distinguish one atomic data type from another.
For example:
integer
Values: -1,-2, 0, 1,2,….
Operations : +,-, *,….
Floating point
Values: 0.0, 0.2, …..
Operations: *, +, …..
Character
Values : ‘A’, ‘a’,…..
Operations: <, >, …
 The opposite of atomic data is composite data.
 Composite data can be broken out into subfields that have meaning.
 As an example of a composite data item, consider your telephone number.
Data Structure:
 A data structure is an aggregation of atomic and composite data types into a set with defined
relationships.
 In this definition, structure means a set of rules that hold the data together.
Abstract Data Type:
 When we first started programming there were no abstract data types.
 If we wanted to read a file, we wrote the code to read the physical file device.
 It did not take long to realize that we were writing the same code over and over again.
 So we created what is known today is an abstract data type.
 The code to read the keyboard is a ADT.

Page 3 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

 It has a data structure, character, and a set of operations that can be used to read that data
structure.
 In other words, the ADT consists of a set of definitions that allow programmers to use the
functions while hiding the implementation.
 This generalization of operations with unspecified implementations is known as abstraction.
 The concept of abstraction means:
 We know what a data type can do
 How it is done is hidden
 An abstract data type is a data declaration packaged together with the operations that are
meaning ful for the data type. In other words, we encapsulate the data and the operations on
data and we hide them from the user.
A model for and Abstract Data Type
The ADT model is shown in below

 The dark shaded are with an irregular outline represents the model.
 Inside the abstract area are two different aspects of the models: the data structure and the
operational functions
ADT Operations:
 Data are entered, accessed, modified, and deleted through the operational interface drawn a
rectangles partially in and partially out the structure.
 For each operation header there is an algorithm that performs its specific operation.
 Only the operation name and its parameters are visible to the user, and they provide the only
interface to the ADT.
ADT Data Structure:
 Each ADT class object will have a defined type that the users must use in their programs just as
you must use the stream object names cin and cout
 When the ADT class object is created, it contains any required structures.
 The exact format and type of the structures are not a concern of the application code.
 As a matter of practice, we will implement the structures used in this text in dynamic memory.
 In this text, we develop ADT classes for linked lists, stacks, queues, AVL tress, B trees, and
graphs.
ADT Class Templates:

Page 4 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

 All of the ADT classes in this text use the same template format.
 Two general components of a structure are the data and optionally a key identifier.
 The data structure is given a template identifier of TYPE, which the application programmer
must use when the ADT class is defined.
 Also associated with the template is a key type identifier, KTYPE.
 Templete ADT class template components
template <class TYPE>
struct NODE
{
-----------
};
template <class TYPE, class TYPE>
class list
{
-------
};
List Searches
The algorithm used to search a list depends to a large extent on the structure of the list.
The two basic searches for arrays are the sequential search and the binary search.
Sequential Search:
 The sequential search is used whenever the list is not ordered.
 In the sequential search, we start searching for the target at the beginning of the list and
continue until we find the target or we are sure that it is not in the list
 Sequential search algorithm
 The sequential search algorithm needs to tell the calling algorithm two things.
 First, did it find the data it was looking for? And second, if it did at what index are the target
data found?
 To answer these questions. The search algorithms requires four parameters: (1) the list we are
searching (2) an index to the last element in the list (3) the target and (4) the address where the
found element’s index location is to be stored.

Page 5 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

Sequential Search Algorithms

Binary search:
 The sequential search algorithm is very slow.
 If we have an array of 1000 elements, we must do 1000 comparisons in the worst case.
 If the array is not sorted, the sequential search is the only solution.
 However, if the array is sorted, we can use a more efficient algorithm called the binary search.

Page 6 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

 The binary search starts by testing the data in the element at the middle of the array to
determine if the target is in the first or second half of the list.
 If it is in the first half, we do not need to check the second half.
 If it is in the second half, we do not need to test the first half.
 In other words, we eliminate half the list further consideration.
 We repeat this process until we find the target or determine that it is not in the list.
 To find middle of the list, we need tree variables, one to identify the beginning of the list, one to
identify the middle of the list, and one to identify the end of the list.
 We analyze two cases here: the target is in the list and the target is not in the list.

Target found:
 Figure below shows how we find 22 in a sorted array.
 We descriptively call our three indexes first, mid and last.
 Given first as 0 and last as 11, we can calculate mid as follows:
Mid = (first +last)/2

Binary search algorithms

Page 7 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

Analyzing search algorithms:


Sequential search
 The efficiency of the sequential search is 0(n).
Binary search
 The efficiency of the binary search is 0(log2n).

Page 8 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

UNIT - II
LINEAR LIST CONCEPTS

 The sequential property of a linear list is basic to its definition and use.
 The simplest linear list structure, the array, is found in virtually all programming languages.
 Linear list can be divided into two categories: general and restricted.
General list:
 In a general list, data can be inserted and deleted any where and there are no restrictions on the
operations that can be used to process the list.
 General structures can be further described by their data as either random or ordered lists, the data
are arranged according to a key.
 A key is one or more fields within a structure that are used to identify the data or otherwise control
their use.
 The key is a filed, such as employee number, that identifies the record.
Restricted list:
 In a restricted list, data can only be added or deleted at the node of the structure and processing is
restricted to operations on the data at the ends of the list.
 We describe two restricted list structure: the first in-first out (FIFO) list and the last in-first out (LIFO)
list.
 The FIFO list is generally called a queue; the LIFO list is generally called a stack.

Four operations are generally associated with linear lists


 Insertion
 Deletion
 Retrieval
 Traversal
Insertion:
 Depending on the type of general list, an insertion can be made at the beginning of the list, in
the middle of the list, or at the end of the list.
 Although there are no restrictions on inserting data into a random list.
 Computer algorithms generally insert data at the end of the list.
 Thus, random lists are sometimes called chronological lists.
 Data must be entered into ordered lists so that the ordering of the list is maintained.
 Maintaining the order may require inserting the data at the beginning or at the end of the list,
but most of the time data are inserted somewhere in the middle of the list.
Deletion
 Deletion from a general list requires that the list be searched to locate the data being deleted.
 Any sequential search algorithm can be used to locate the data.
 Once located, the data are removed from the list.
 When data are deleted from a random array, the data following the deleted item must be
shifted or replace the empty element.

Page 9 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

Retrieval:
 List retrieval requires that data be located in a list and resented to the calling module without
changing the contents of the list.
 As with both insertion and deletion, any sequential search algorithm can be used to locate the
data to be retrieved from a general list.
 Retrieval from a restricted list depends on the particular list being used.
Traversal:
 List traversal is a special case of retrieval in which all elements are retrieved in sequence.
 List traversal requires a looping algorithm rather than a search.
 Each execution of the loop process one element in the list.
 The loop terminates when all elements have been processed.
Linked List Concepts
 A linked list is an ordered collection of data in which each element contains the location of the
next element; that is, each element contains two parts: data and link.
 The data part holds the useful information, the data to be processed.
 The link is used to chain the data together.
 It contains a pointer that identifies the next element in the list.
 In addition, a pointer variable identifies the first element in the list.
 The name of the list is the same as the name of this pointer variable.
 The simple linked we describe here is commonly known as a singly linked list because it contains
only one link to a single successor.
Nodes:
 The elements in a linked list traditionally called node.
 A node in a linked list is a structure that has at least two fields: one contains the data, the other
the address of the next node in the sequence.
 In below figure shows three different node structures

 The first node contains a single field, number and a link.


 The second node is more typical.
 It contains there data fields, a name, id, and grade points (grdPts) and a link.
 The third example is the one we recommend.

Page 10 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

 The fields are defined in their own structure, which is then put into the definition of a node
structure.
 The one common element in all examples is the link field.
 The nodes in a linked list are called self-referential structures.
Linked list Data Structure:
 One of the attributes of a linked list is that there is not a physical relationship between nodes;
that is, they are not stored contiguously.
 When a linear list is stored in an array, we know from the array structure where the list begins.
 The successor to each element is simply the next element in the array.
 But with a linked list, there is no physical relationship between the nodes.
Head Node Structure:
 Although only a single pointer is need to identify the list, we often find it convenient to create a
structure that stores the head pointer and other data about the list itself.
 When a node contains data about a list, the data re known as metadata; that is they are data
about data in the list.
 Data Node Structure:
 The data type for the list depends entirely on the application.
 A typical data type shown below
datatype
key <keytype>
Field1 <…>
------
fieldN <…>
end datatype
 We include a key field for applications that require searching by key.
 The key type defined above is a generic type that must be changed for each application.
Linked List Algorithms
 We define ten operations for a linked list, which should be sufficient to solve any sequential list
problem
Create List:
 Create list receives the head structure and initializes the metadata for the list.
 At this time there are only two metadata entries.
 The pseudocode for create list is shown in
 Algorithm createList (ref list <metadata>)
 Initializes metadata for a linked list
 Pre list is metadata structure passed by reference
 Post meta data initialized
list.head = null
list.count = 0
return
end createlist

Page 11 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

Insert Node:
 Insert node adds data to a linked list.
 We need only its logical predecessor to insert a node into the list.
 Given the predecessor, there are three steps to the insertion:
 Allocate memory for the new node and insert data
 Point the new node to tis successor
 Point the new node’s predecessor to the new node
Insert into Empty List:
 When the head pointer of the list is null, then the list is empty.
 All that is necessary to add a node to an empty list is to assign the list head pointer the address
of the new node and make sure that tis link field is a null pointer
 The pseudocode satemetns to insert a node into an empty list are shown below
pNewlink = list.head //set link to null pointer
list.head = pNew // point list to first node
Insert in Beginning:
 We add at the beginning of the list anytime we need to insert a node before the first node of the
list.
 We determine that we are adding at the beginning of the list by testing the predecessor pointer.
 If it is a null pointer, then there is no predecessor, so we are at the beginning of the list.
 The first node’s address is stored in the head pointer.
 The pseudocode statements to insert at the beginning of the list are show below:
pNew link = list.head
list.head = pNew
Insert in Middle:
 When we add a node anywhere in the middle of the list, the predecessor contains an address.
 To insert a node between two nodes.
 We point the new node to its successor and then point its predecessor to the new node.
 The address of the new node’s successor can be found in the predecessor’s link field.
 The pseudocode statements to insert a node in the middle of the list are shown below
pNewlink = pPrelink
pPrelink = pNew
Insert at End:
 When we are adding at the end of the list, we only need to point the predecessor to the new
node.
 There is no successor to point to.
 It is necessary, however, to set the new node’s link filed to a null pointer.
 The statements to insert a node at the end of a list are shown below
pNewlink = null pointer
pPrelink = pNew
Delete Node:

Page 12 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

 The delete node algorithm logically removes a node from the linked list by changing various link
pointers and then physically deleting the node from dynamic memory.
 To logically delete a node, we must first locate the node itself.
 A delete node is located by knowing its address and its predecessor’s address.
 The delete situations parallel those for add.
 We can delete the only node, the first node, a node in the middle of the list, or the last node of a
list.
Delete First Node:
 When we delete the first node, we must rest the head pointer to point to the first node’s
successor and then recycle the memory for the deleted node.
 The statements to delete the first node.
 Recycle is the pseudocode command to return a node’s space to dynamic memory
List.head = pLoclink
Recycle (pLoc)
General Delete Case:
 We call deleting any node other than the first node a general case because the same logic
applies to deleting any anode in either the middle or at the end of the list.
 For both of these cases, we simply point the predecessor node to the successor of the node
being deleted.
 We delete the last node automatically.
 When the node being deleted is the last node of the list, its null pointer is moved to the
predecessor’s link field, making the predecessor the new logical end of the list.
 After the pointers have been adjusted, the current node is recycled.
pPrelink = pLoclink
recycle (pLoc)

Search List:
 A search list is used by several algorithms to locate data in a list.
 To insert data, we need to know the logical predecessor to the new data.
 To delete data, we need to find the node to be deleted and identify its logical predecessor.
 To retrieve data from a list, we need to search the list and find the data.
 In addition, many user applications require that lists be searched o locate data.
 To search a list on a key, we need a key field.
 For simple lists, the key and the data can be the same field.
datatype
key <keytype>
field1 <…>
------
fieldN <…>
end datatype
The predecessor and current pointers are set according to the rules in below table

Page 13 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

List search results


Condition pPre pLoc Return
Target < first node Null First node False
Target equal first node Null First node True
First < target < last Largest node < target First node > target False
Target equal middle Node’s predecessor Equal node True
node
Target equal last node Last’s predecessor Last node True
Target > last node Last node NULL False

Retrieve Node:
 Now that we know how to locate a node in the list, we are ready to study retrieve node.
 Retrieve node uses search node to locate the data in the list.
 If the data are found, it moves the data to the output area in the calling module and returns
true.
 If they are not found, it returns false.
 The pseudocode is shown below
1 found = searchList (List, pPre, pLoc, key)
2 if (found)
dataOut = pLoc->data
end if
return found
end retrieveNode
Traverse List:
 To traverse the list we need a walking pointer, a pointer that moves from node to node as each
element is processed.
 Assuming a linked list with a head structure, the following pseudocode uses a walking pointer to
reverse the list.
 Each loop modifies the pointer to move to the next node in sequence as we traverse the list
pWalker = List.head
loop (pWalker not null)
process (pWalkerdata)
pWalker = pWalkerlink
Destroy List:
 When a list is no longer needed but the applications is not done, the list should be destroyed.
 Destroy list deletes any anodes still in the list and recycles their memory.
 If then sets the metadata to a null list condition.
 The code for destroy list is shown in algorithm
1 loop (list.count not zero)
Dltptr = list.head

Page 14 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

List.head = dltPrlink
List.count = list.count – 1
Recycle (dltPtr)
end loop
// no data left in list. Reset metadata
List.pos = null
Return
End destroyList

Page 15 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

UNIT - III
STACK
 A stack is a linear list in which all additions and deletions are restricted to one end, called the
top.
 If you insert a data series into a stack and then removed it, the order of the data would be
reversed.
 The reversing attribute is why stacks are known as the last in-first out (LIFO) data structure.
Basic stack operations:
 The three basic stack operations are push, pop, and stack top.
 Push is used to insert data into h stack.
 Pop removes data from a stack and returns the data to the calling module.
 Stack returns the data at the top of the stack without deleting the data from the user.
Push:
 Push adds an item at the top of the stack.
 After the push, the new item becomes the top.
 If there is not enough room, then the stack in an overflow state and the item cannot be added.
Pop:
 When we pop a stack, we remove the item at the top of the stack and return it to the user
 Because we have removed the top item, the next older item in the stack becomes the top.
 When the last item in the stack is deleted, the stack must be set to its empty state.
 In pop is called when the stack is empty, then it is in an underflow sate.
Stack Top:
 The third stack operations is stack top.
 Stack top copies the item at the top of the stack, that is, it returns the data in the top element to
the user but does not delete it.
 Stack top can also result in underflow if the stack is empty.

Page 16 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

Stack Linked List Implementation


Data Structure:
 The implement the linked list stack, we need two different structures, a head and a data link.
 The head structure contains metadata and a pointer to the top of the stack.
 The data structure contains data and a next pointer to the next node in the stack.
Stack Data Node:
 The rest of the data structure is a typical linked list data node.
 Although the application determines the data that are stored in the stack, the stack data node
looks like any linked list node.
 In addition to the data, it contains a next-pointer to other data nodes, making it self-referential
data structure.
Stack Algorithms:
 The eight stack operations we define should be sufficient to solve any basic stack problem.
 If an application requires additional stack operations, they can be easily added.
 For each operations, we give its name, a brief description, and its calling sequence.
 The four most common stack operations: create stack, push stack top stack and destroy stack.
Create Stack:
 Create stack initializes the metadata for the stack structure.
Push Stack:
 Push stack inserts an element into the stack.
 The first thing we need to do when we push data into a stack is find memory for the node.
 We must therefore allocate memory from dynamic memory.
 Once the memory is allocated, we simply assign the data to the stack node and then set the next
pointer to point to the node currently indicated as the stack top.
 We also need to update the stack top pointer and add 1 to the stack count field.
 To develop the insertion algorithm, we need to analyze three different stack conditions:
 Insertion into an empty stack.
 Insertion into a stack with data
 Insertion into a stack when the available memory is exhausted.
The pseudocode for a stack push:
Algorithm pushstack (ref stack <metadata>, val data <data type>)
Insert (push) one item into the stack
Pre stack is metadata structure to a valid stack data contain data to be pushed into stack
Post data have been pushed in stack
Return true if successful; false if memory overflow
1 if (stack full)
1 Success = false
2 else
1 allocate (new(Ptr)
2 newPtrdata = data

Page 17 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

3 newPtrnext = stack.top
4 stack.top = newPtr
5 stack.count = stack.count + 1
6 success = true
3 end if
4 return success
End pushStack
Pop Stack:
 Pop stack send the data in the node at the top of the stack back to the calling algorithms.
 If then deletes and recycles the node-that is, returns it to memory.
 After the count is adjusted by subtracting 1, the algorithm returns to the caller.
 If the pop was successful, it returns true; if the stack the empty when pop is called, it returns
false.
The pseudocode for a pop stack:
Algorithm popStack ( ref stack <metadata>, ref dataOut <dataType>)
This algorithm pops the item on the top of the stack and returns it to the user.
Pre stack is metadata structure to a valid stack
dataOut is a reference variable to receive the data
Post Data have been returned to calling algorithm
Return true is successful; false if underflow
1 if (stack empty)
1 success = false
2 else
1 dltPtr = stack.top
2 dataOut = stack.topdata
3 stack.top = stack.topnext
4 stack.count = stack.count – 1
5 recycle (dltPtr)
6 success = true
3 end if
4 return success
End popStack

Stack Top:
 The stack top algorithm sends the data at the top of the stack back to the calling module
without deleting the top node.
 It allows the user to see what will be deleted when the stack is popped
The pseudocode for the stack top:
Algorthim stackTop (val stack <metdata>, ref dataOut <dataType>)
This algorithm retrieves the data from the top of the stack without changing the stack.

Page 18 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

Pre stack is metadata structure to a valid stack


DataOut is a reference variable to receive the data
Post data have been returned, to calling algorithm
Return true if data returned, false if underflow
1 if (stack empty)
1 succcess = false
2
Else
1 dataOut = stack.topdata
2 success = true
3 end if
4 return success
End stackTop
Empty Stack:
 Empty stack is provided to implement the structured programming concept of data hiding.
Full Stack:
 Full stack is another structured programming implementation of data hiding.
Stack Count:
 Stack count returns the number of elements currently in the stack.
 It is another implementation of the data hiding principle of structured programming.
Destroy Stack:
 Destroy stack deletes all data in a stack.
 The function to return memory to the heap is language dependent.
 We use the pseudocode keyword recycle.
Queue
 A queue is a linear list in which data can be inserted at one end, called the rear, and deleted
from the other end, called the front.
 It is first in-first out (FIFO) data structure.
Queue Operations:
 There are four basic queue operations.
 Data can be inserted at the rear, deleted from the front, retrieved from the first, and retrieved
from the rear.
Enqueue:
 The queue insert is known as enqueue.
 After the data have been inserted into the queue, the new element becomes the rear.
 If there is not enough room for another element in the queue, the queue is in an overflow state.
Dequeue:
 The queue delete operation is known as dequeue.
 The data at the front of the queue are returned to the user and removed from the queue.

Page 19 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

 If there are no data in the queue when a dequeue is attempted, the queue is in an underflow
state.
Queue Front:
 Data at the front of the queue can be examined the queue front.
 It retruns the data at the front of the queue without changing the contents of the queue.
 If there are no data in the queue, then the queue is an underflow state.
Queue Rear:
 A parallel operation to queue front examines the data at the rear of the queue.
 It is known as queue rear.
 As with queue front, if there are no data in the queue, the queue is in an underflow state.

Queue Linked List Design


 As with a stack, we implement our queue as a linked list in dynamic memory.
 As we have stressed before, the actual implementation may be different.
Data Structure:
 We need two different structures to implement the queue, a queue head structure and a data
node structure.
 After it is created, the queue will have one head node and zero or more data nodes, depending
on tis current state.
Queue Head:
 The queue requires two pointers and a count.
 These fields are stored in the queue head structure.

Page 20 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

 Other queue attributes, such as the maximum number of items ever present in the queue and
the total number that have been processed through the queue, could be stored in the head
node if such data where relevant to an applications.
Queue Data Node:
 The queue data node contains the user data and a link field pointing to the next node, if any.
 These nodes are stored in the heap and are inserted and deleted as requested by the using
program.
 Queue Operations:
 The four basic queue operations are shown below:
Create Queue:
 The create queue operation is rather sample.
 All we have to do is set the metadata pointers to null and the count to 0.
The pseudocode for create queue is show below:
Queue.ront = null
Queue.rear = null
Queue.count = 0
Enqueue:
 The enqueue is a little more complex than inserting data into a stack.
 To develop the insertion algorithm, we need to analyze three different queue conditions:
 Insertion into an empty queue
 Insertion into a queue with data
 Insertion into a queue when there is no memory left in the heap.
The pseudocode for an enqueue:
Algorithm enqueue (ref queue <metadata>,
Val dataIn <dataType>)
This algorithms inserts data into a queue
Pre queue is a metadata structure
Post dataIn has been inserted
Return true if successful, false if overflow
1 if (queue full)
1 return false
2 End if
3 allocate (newPtr)
4 newPtrdata = dataIn
5 newPtrnext = null pointer
6 if (queue.count zero)
Inserting into null queue
1 queue.front = newPtr
7 else
Insert data and adjust metadata

Page 21 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

1 queue.rearnext = new Ptr


8 End if
9 Queue.rear = newPtr
10 queue.count = queue.count + 1
11 return true
End queue
Dequeue:
 Although dequeue is also a little more complex than deleting data from a stack, it starts out
much the same.
 We must first ensure that the queue contains data.
 If the queue is empty, we have underflow and we return false, indicating that the delete was not
successful.
 There are several ways to determine whether the only element in the queue is being deleted.
 If the queue count is 1, indicating that we are deleting the only item in the queue, we assign a
null pointer to the rear pointer.
 Finally we recycle the node that we just deleted
The pseudocode for a dequeue:
Algorithm dequeue (ref queue <metadata>.
Ref item <dataType>)
This algorithm deletes a node from a queue
Pre queue is a metadata structure
Post data at front of queue returned to user through item and front element deleted and recycled
Return true if successful, false if underflow
1 if (queue.count is 0)
1 return false
2 end if
3 item = queue.frontdata
4 deleteLoc = queue.front
5 if (queue.count 1)
Deleting only item in queue.
Queue.rear = null pointer
6 end if
7 queue.front = queue.frontnext
8 queue.cout = queue.count – 1
Recyle (deleteLoc)
Return true
End dequeue
Empty Queue:
 Empty queue returns true if the queue is empty and false if the queue contains data.

Page 22 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

Full Queue:
 Full queue returns true if the queue contains data and false if the queue is empty.
Queue Count:
 Queue count returns the number of elements currently in the queue.
Destroy Queue:
 Destroy queue deletes all data in the queue and recycles their memory.

Page 23 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

UNIT - IV
BASIC TREE CONCEPTS
 A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called
branches, that connect the nodes.
 The number of branches associated with a node is the degree of the node.
 When the branches is directed toward the node, it is an indegree branch; when the branch is
directed away from the node, it is an outdegree branch.
 The sum of the indegree and outdegree branches is the degree of the node.
 If the tree is not empty, then the first node is called the root.
Terminology:
 A node that is not a root or a leaf is known as an internal node because it is found in the middle
portion of a tree.
 A node is a parent if it has successor nodes- that is, if it has an outdegree greater than zero.
 Conversely, a node with a predecessor is a child.
 Two or more nodes with the same parent are siblings.
 An ancestor is any node, in the path from the root to the node.
 A descendent is any node in the path below the parent node; that is, all nodes in the paths from
a given node to a leaf are descendents of the node.
 A path is a sequence of nodes in which each node is adjacent to the next one.
 Every node in the tree can be reached by following a unique path starting from the root.
 The level of a node is its distance from the root.
 Because the root has a zero distance from itself, the root is at level 0.
 The children of the root are at level 1, their children are level 2, and so forth.
 The height of the tree is the level of the leaf in the longest path from the root plus.
 A tree may be divided into subtrees.
 A subtree is any connected structure below the root.
 The first node in a substree is known as the root of the subtree and is used to name the subtree.
 Furthermore, subtrees can be subdivided into subtrees.
 The concept of subtrees leads us to a recursive definition of a tree.
 A tree is a set of nodes that either
 Is empty
 Has a designated node, called the root, form which hierarchically descend zero or more
subtrees, which are also trees.
Binary Tree
 A binary tree is a tree in which no node can have more than two subtrees.
 In other words, a node can have zero, or two subtrees.
 These subtrees are designated as the left subtree and right subtree.
 A null tree is a tree with no nodes.
Properties:
 We now define several properties for binary trees that distinguished them form general trees.
Height of Binary trees:

Page 24 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

 The height of binary trees can be mathematically predicted.


 Given a height of the binary tree, H, the minimum and maximum number of nodes in the tree
are given as
Nmin = H and Nmax = 2n – 1
Complete Binary Tree:
 A complete tree has the maximum number of entries for its height.
 The maximum number is reached when the last level is full.
 A tree is considered nearly complete if it has the minimum height for tis nodes and ll nodes in
the last level are found on the left.
Binary Tree Structure:
 The representation of a binary tree structure is relatively straightforward.
 Each node in the structure must contain the data to be stored and two pointers, one of the left
subtree and one of the right subtree.
 For example, in a typical binary tree, the node would contain data and two pointers, are shown
below
Node
leftSubTree <pointer to node>
data <dataType>
rightSubTree <pointer to node>
End Node
Binary Tree Traversals
 A binary tree traversal requires that each node of the tree be processed once and only once in a
predetermined sequence.
 The two general approaches to the traversal sequence are depth first and breadth first.
 In the depth-first traversal, the processing proceeds along a path from the root through one
child to the most distant descendent of that first child before processing a second child.
 In other words, in the dept-first traversal you process all of the descendents of a child before
going on to the next child.
 In a breadth-first traversal, the processing proceeds horizontally from the root to all of its
children, then to tis childrens’s children, and so forth until all nodes have been processed.
 In other words, in the breadth-first traversal, each level is completely processed before the next
level is started.
Depth-First Traversals:
 Given that a binary tree consists of a root, a left substree, and a right subtree, we can define six
different depth-first traversal sequences.
 Computer scientists have assigned three of these sequences standard names in the literature;
 Preorder Traversal (NLR):
 In the preorder traversal, the root node is processed first, followed by the left subtree, and then
the right subtree.
 The code for the preorder traversal is shown in algorithm
Algorithm preorder (val root <node pointer>)
Traverse a ninary tree in node-left-right sequence

Page 25 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

Pre root is the entry node of a tree or subtree


Post each node has been processed in order
1 if (root is not null)
1 process (root)
2 preOrder (rootleftSubTree)
3 preOrder (rootrightSubTree)
2 end if
3 return
End preorder
Inorder Traversal (LNR):
 The inorder traversal process the left subtree first, then the root, and finally the right subtree.
 The meaning of the prefix in is that the root is process in between the subtrees.
 The code for the inorder traversal is shown in algorithm
Algorithm inOrder (val root <node pointer>)
Traverse a binary tree in left-node-right sequence
Pre root is the entry node of a tree or subtree
Post each node has been processed in order
1 if (root is not null)
2 process (root)
3 inOrder (root rightSubTree)
2 end if
3 return
End inOrder
Postorder Traversal (LRN):
 The last of the standard traversals is the postorder traversals.
 It process the root node after (post) the left and right subtrees have been processed.
 It starts by locating the leftmost leaf and processing it.
 It then processes its right sibling including its substrees.
 Finally, it processes the root node.
 The postorder traversal logic is shown in algorithm
Algorithm post Order (val root <node pointer>)
Traverse a binary tree in left-right-node sequence
Pre root is the entry node of a tree or subtree
Post each node has been processed in order
1 if (root is not null)
1 postOrder (rootleftSubTree)
2 postOrder(rootrightSubTree)
3 process (root)
2 end if

Page 26 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

3 return
End postOrder
Breadt-First Traversals:
 In the bradth-first traversal of a binary tree, we process all of the children of a node before
proceeding with the next level.
 In other words, given a root at level n, we process all nodes at level n before proceeding with
the nodes at level n+1
 The code for a breadth-first traversal of out binary tree is shown in algorithm
Algorithm breadthFirst (val root <node pointer>)
Process tree using breadth-first traversal
Pre root is a pointer to a tree node
Post tree has been processed
1 pointer = root
2 loop ( pointer not null)
1 processs (pointer)
2 if (pointer left not null)
1 enqueue (pointerleft)
3 End if
4 if (pointerright not null)
1 enqueue (right pointer)
5 end if
6 if (not emptyQueue)
1 dequeue (pointer)
7 else
1 pointer = null
8 end if
3
End loop
4 return
End breadthFirst
Heap Definition
 A heap is a binary tree structure with the following properties
 The tree is complete or nearby complete
 The key value of each node is greater than or equal to the key value in each of its descendents.
 Sometimes this structure is called a max-heap.
 The second property of a heap, the key value is greater than the keys of the subtrees, can be
reversed to create a min-heap.
 That is, we can create a minimum heap in which the key value in a node is less than the key
values in all of its subtrees.
 Generally speaking, whenever the term heap is used by itself, it refers to max-heap.
Heap Structure

Page 27 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

 To better understand the structure of a heap.


 Note that the left node of two siblings can be either larger or smaller than the right node.
 Compare this ordering with the binary search tree.
 Finally, in the tree-level heap, note that the third level is being filled from the left.
 This is the definition of a nearly complete tree and is a requirement for a heap.
 To complete out understanding of a heap.
 Let’s look at some structures that are not heaps.
 The first two structures are not heaps because they are not complete or nearly complete trees.
 Although the third and fourth examples are nearly complete, the keys of the nodes are not
always greater than the keys of their descendents.

Basic Heap Algorithms


 Two basic maintenance operations are performed on a heap: insert a node and delete a node.
 To implement the insert and delete operations, we need two basic algorithms: reheap yp and
reheap down.
ReheapUp:
 Imagine that we have a nearly complete binary tree with N elements who first N – 1 elements
satisfy the order property of heaps, but the last element does not.
 In other words, the structure would be a heap if the last element were not there.
 The heapUp Operation repairs the structure so that it is a heap by floating the last element up
the tree until that element is in its correct location in the tree.
ReheapDown:
 Now let’s examine the reverse situation.
 Imagine we have a nearly complete binary tree that satisfies the heap order property except in
the root position.
 This situation occurs when the root is deleted from the tree, leaving two disjointed heaps.
 To correct the situation, we move the data in the last tree node to the root.
 Obviously this action destroys the tree’s heap properties.
 To restore the heap property we need an operation that will sink the root down until it is in a
position where the heap-ordering property is satisfied. We call this operation reheapDown.

Page 28 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

UNIT - V
GENERAL SORT CONCEPTS
 Sorting is one of the most common data-processing applications.
 Sorts are generally classified as either internal or external sorts.
 An internal sort is a sort in which all of the data are held in primary memory during the
sorting process.
 An external sort uses primary memory for the data currently being sorted and
secondary storage for any data that will not fit in primary memory.
 Internal sorting algorithms have been grouped into several different classifications
depending on their general approach to sorting Knuth identified five different
classifications: insertion, selection, exchanging, merging, and distribution sorts.
Sort Order:
 The sort order identifies the sequence of the sorted data, ascending or descending.
 If the order of the sort is not specified, it is assumed to be ascending.
Sort Stability:
 Sort stability is an attribute of a sot indicating that data with equal keys maintain their
relative input order in the output.
Sort Efficiency:
 Sort efficiency is a measure of the relative efficiency of a sort.
 It is usually an estimate of the number of comparisons and moves required to order an
unordered list.
Passes:
 During the sorting process, the data are traversed many times. Each traversal of the
data is referred to as a sort pass.
Quick Sort
 Quick sort is an exchange sort developed by C.A.R. Hoare in 1962.
 Each iteration of the quick sort selects an element, known as pivot and divides the list
into three groups:
 A partition of elements whose keys are less than the pivot’s key
 The pivot element that is placed in its ultimately correct location in the list.
 And a partition of elements greater than or equal to the pivot’s key.
 The sorting then continues by quick sorting the left partition followed by quick sorting
the right partition.
Quick Sort Algorithm:
Algorithm quicksort (ref list <array>,
Val left <index>,
Val right <index>)
An array, list (left…right) is sorted using recursion
Pre List is an array of data to be sorted

Page 29 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

Left and right identify the first and last elements of the list, respectively
Post list is sorted
1 if ( right – left ) > minSize )
Quick sort
1 medianLeft (List, left, right)
2 pivot = list (left)
3 sortLeft = left + 1
4 sortRight = right
5 loop (sortleft <=sortRight)
Find key on let that belongs on right
1 loop (list(sortLeft).key < pivot.key)
1 sort Left = sort Left + 1
2 end loop
Find key on eight that belongs on left
3 loop (list[sortRight].key >= pivot.key)
1 sortRight = sortRight – 1
4 end loop
5 if (sortLeft <= sortRight)
1 exchange(list,softLeft, sortRight)
2 sortLeft = sortLeft + 1
3 sortRight = sortRight – 1
6 end if
6 end loop
Prepare for next phase
7 list [left] = list [sortLeft – 1]
8 list [sortleft – 1] = pivot
9 if (left<sortRight)
1 quickSort (list, left, sorRight – 1)
10 end if
11 if (sortLeft < right)
1 quickSort (List, sortLeft, right)
12 end if
2 else
1 insertionSort (list, left, right)
3 end if
4 end quicksort
Graphs

Page 30 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

Terminology
 A graph is a collection of nodes called vertices, and a collection of line segments, called
lines, connecting pairs of vertices.
 In other words, a graph consists of wo set, a set vertices and set of lines.
 Graphs may be either directed or undirected.
 A directed graph, or digraph for short, is a graph in which each line has a direction
(arrow head) to its successor.
 The lines in a directed graph are known as arcs.
 In a directed graph, the flow along the arcs between two vertices can follow only the
indicated direction.
 An undirected graph is a graph in which there is no direction (arrow head) on any of the
lines, which are known as edges.
 In an undirected graph, the flow between two vertices can go in either direction.
 Two vertices in a graph are said to be adjacent vertices (or neighbors) if an edge directly
connects them.
 A path is a sequence of vertices in which each vertex is adjacent to the next one.
 A cycle is a path consisting of at least three vertices that starts and ends with the same
vertex.
 A loop is a special case of a cycle in which a single are begins and ends with the same
vertex.
 In a loop, the end points of the line are the same.
 Two vertices are said to be connected if there is a path between them.
 A directed graph is strongly connected if there is a path from each vertex to every other
vertex in the digraph.
 A directed graph is weekly connected if at least two vertices are not connected.
 A graph is disjoint if it is not connected.
 The degree of a vertex is the number of lines incident to it.
 The outdegree of a vertex in a diagraph is the number of arcs leaving the vertex.
 The indegree is the number of arcs entering the vertex.
Operations
 We defined six primitive graph, operations that provide the basic modules needed to
maintain a graph:
 Add a vertex
 Delete a vertex
 Add an edge
 Delete an edge
 Find a vertex
 Traverse a graph

Page 31 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

Add a vertex:
 Add vertex inserts a new vertex into a graph.
 When a vertex is added it is disjoint; that is, it is not connected to any other vertices in
the list.
Delete vertex:
 Delete vertex removes a vertex from the graph.
 When a vertex is deleted, all connecting edges are also removed.
Add Edge:
 Add edge connects a vertex to a destination vertex.
 If a vertex requires multiple edges, then add an edge must be called once for each
adjacent vertex.
Delete Edge:
 Delete edge removes one edge from a graph.
Find Vertex:
 Find vertex traverses a graph looking for a specified vertex.
 If the vertex is found, its data are returned.
 If it is not found, en error is indicated.
Traverse Graph:
 There is always at least one application that requires that all vertices in a given graph be
visited; that is, there is at least one application that requires that the graph be
traversed.
 The two standard graph traversals are depth first and breadth first. Both used the
visited flag.
Depth-First Traversal:
 In the depth-first traversal, we process all of a vertex’s descendents before we move to
an adjacent vertex.
 The depth-first traversal of a graph starts by processing the first vertex of the graph.
 After, processing the first vertex, we select any vertex adjacent to the first vertex and
process it.
 As we process each vertex, we select an adjacent vertex until we reach a vertex with no
adjacent entries.
 This is, similar to reaching a leaf in a tree.
 We then back out of the structure, processing adjacent vertices as we go.
 It should be obvious that this logic requires a stack (or recursion) to complete the
traversal.
Breadth-First Traversal:
 In the breadth-first traversal of a graph, we process all adjacent vertices of a vertex
before going to the next level.
 The breadth-first traversal of a graph follows the same concepts.

Page 32 of 33
STUDY MATERIAL FOR BCA
DATA STRUCTURE
SEMESTER - III, ACADEMIC YEAR 2020 - 21

 We begin by picking a staring vertex; after processing it, we process all of its adjacent
vertices.
 After we process all of the first vertex’s adjacent vertices, we pick the first adjacent
vertex and process all of its vertices, then the second adjacent vertex and process all of
its vertices, and so forth until we are finished.
Graph Storage Structures
 To represent a graph we need to store two sets.
 The first set represents the vertices of the graphs, and the second set represents the
edges or arcs.
 The two most common structures used to store these sets are arrays and linked lists.
Adjacency Matrix:
 The adjacency matrix uses a vector (one-dimension array) for the vertices and a matrix
(two-dimensional array) to store the edges.
 If two vertices are adjacent-that is, if there is an edge between them-the matrix
intersect has a value of 1; if there is no edge between them, the intersect is set to 0.
 If the graph is directed, then the intersection in the adjacency matrix indicates the
direction.
 In the adjacency matrix representation, we use a vector to store the vertices and a
matrix to store the edges.
Adjacency List:
 The adjacency list uses a two-dimensional ragged array to store the edges.
 The vertex list is a singly linked list of the vertices in the list.
 Depending on the application, it could also be implemented using doubly linked lists or
circularly linked lists.
 The pointer at the left of the list links the vertex entries.
 The pointer at the right in the vertex is a head pointer to a linked list of edges from
vertex.
 In the adjacency list, we use a linked list to store the vertices and a two-dimensional
linked list to store the arcs.

Page 33 of 33

You might also like