0% found this document useful (0 votes)
11 views23 pages

Lect 13 - 2024

FoDSA lecture slides BITS Pilani, Hyderabad Campus By Sameera Muhamed Salam
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)
11 views23 pages

Lect 13 - 2024

FoDSA lecture slides BITS Pilani, Hyderabad Campus By Sameera Muhamed Salam
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/ 23

BITS F232

aft
Foundations of
Data Structures and Algorithms
Lect 13
Sameera Muhamed Salam
Dept of CSIS, BITS Pilani, Hyderabad Campus

Dr
QUEUE

Tree

BITS F232 Sameera M S 2


Practice Questions

1. Given a sorted doubly linked list and a target value, determine if there are two
distinct nodes in the doubly linked list whose values add up to the target value. It
is guaranteed that the values of the nodes in the doubly linked list are unique (i.e.,
no two distinct nodes have the same value). Note that positive as well as negative
integers and zero can be present as the elements of the linked list. Your solution
must use only constant extra space. The expected time complexity of your
solution should be O(n), where n is the number of nodes in the doubly linked list.

BITS F232 Sameera M S 3


Practice Questions

2. Pearl ′ 24 is here and lots of students from all over the country are visiting your
college. Suppose you have been tasked with choosing the colors for each college.
Unfortunately, there is no list of colleges participating in the fest, only a list of
students participating in the fest. To make your task easier, your friends have
assigned an integer to every student such that no two students belonging to
different colleges have the same integer and no two students belonging to the
same college have different integers. They have then sorted this sequence of
integers and provided it to you as an array. Given this array, write a program to
count the number of different colleges participating in the fest so that you can
assign colors to them. The time complexity of your solution should be
O(k ∗ log (n)), where k is the number of different colleges and n is the size of the
array, respectively.

BITS F232 Sameera M S 4


Queue: Linked List Implementation

Figure: [Ref:Log2Base2]

BITS F232 Sameera M S 5


Queue: Linked List Implementation

▶ Empty Queue:
▶ Front=Rear = NULL
▶ Enqueue
▶ If Queue is empty
▶ Front = Rear = Newnode
▶ Otherwise
▶ Rear→ next = Newnode
▶ Rear=Newnode
▶ Dequeue
▶ If queue is not empty
▶ Front=Front→ next

BITS F232 Sameera M S 6


Queue: Application

BITS F232 Sameera M S 7


Queue: Application

▶ Video streaming
▶ Process scheduling
▶ Ticket booking system
▶ Printer Spooler

BITS F232 Sameera M S 8


Deque: Double ended Queue

▶ insertion and deletion at both ends.

Figure: [Ref:GeeksforGeeks]

▶ There are input restricted Queues and output restricted queues also.
▶ Applications?

BITS F232 Sameera M S 9


Lists

▶ linear sequence that supports access to its elements by their indices.


▶ we define the index (or rank) of an element e in S to be the number of elements
that are before e in S.
▶ “index” is different from indices of cells in an array.
▶ ”index” of an element changes in the list when we enter an element before it or
removes an element before it.
▶ index is Dynamic

BITS F232 Sameera M S 10


List Operations

BITS F232 Sameera M S 11


List implementations

BITS F232 Sameera M S 12


List implementations

▶ Array

BITS F232 Sameera M S 13


List implementations

▶ Array
▶ Linked list

BITS F232 Sameera M S 14


Tree: Introduction
▶ Non linear data structure.
▶ A tree is an abstract data type that stores elements hierarchically.
▶ Each element in the tree except the top element has a parent
▶ Each element in the tree has zero or more children nodes
▶ Each node is represented as an oval or rectangle
▶ Parent is connected to the children nodes using straight line
▶ Top element (element with no parent) is called root of the tree

Tree
A Tree T is a set of nodes storing elements in a parent-child relationship with the
following properties:
▶ T has a special node r, called the root of T.
▶ Each node y of T different from r bas a parent node u.

BITS F232 Sameera M S 15


Nodes in the Tree
▶ If node u is the parent of node y, then we say that y is a child of u
▶ Two nodes that are children of the same parent are siblings.
▶ A node is external if it has no children
▶ External nodes are also known as leaves.
▶ A node is internal if it has one or more children.

BITS F232 Sameera M S 16


Nodes in the tree
▶ An ancestor of a node e is either the node itself or an ancestor of the parent of
the node.
▶ We say that a node y is a descendent of a node u f u is an ancestor of y.
▶ The subtree of T rooted at a node v is the, tree consisting of all the descendents
of v in T (including v itself)

BITS F232 Sameera M S 17


Tree:Application 1

▶ In most operating systems, files are organized hierarchically into nested directories
(also called folders), which are presented to the user in the form of a tree.

BITS F232 Sameera M S 18


Tree: Application 2

▶ A tree is ordered if there is a linear ordering defined for the chi1dren of each node;
that is, we can identify children of a node as being the first, second, (third, and so
on.
▶ Ordered trees typically indicate the linear order relationship existing between
siblings by listing them in a sequence or iterator in the correct order.
▶ A structured document, such as a book, is hierarchically organized as a tree whose
internal nodes are chapters, sections, and subsections, and whose external nodes
are paragraphs, tables, figures, the bibliography, and so on.

BITS F232 Sameera M S 19


Expression Tree
▶ A binary tree is an ordered tree in which every node has at most two children.
▶ A binary tree is proper if each internal node has two children.
▶ For each internal node in a binary tree, we label each child as either being a left
child or a right child.
▶ The subtree rooted at a left or right child of an internal node y is called a left sub
tree or right sub tree, respectively, of y.
▶ An arithmetic expression can be represented by a tree whose external nodes are
associated with variables or constants, and whose internal nodes are associated
with one of the operators +, −, ∗, /.

BITS F232 Sameera M S 20


References:

▶ Michael T. Goodrich and Roberto Tamassia, “ Algorithm Design Foundations,


Analysis, and Internet Examples”, Wiley Student Edition.
▶ Jon Kleinberg and Eva Tardos, “ Algorithm Design”, Pearson Publishers.
▶ Thomas H. Chorman, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein, “
Introduction to algorithms”, MIT Press Publishers.
▶ Sanjoy Dasgupta, Umesh Vazirani, Christos Papadimitriou, “ Algorithms”,
McGraw-Hill Education Publishers.

BITS F232 Sameera M S 21


Discussion Time!

BITS F232 Sameera M S 22


BITS F232 Sameera M S 23

You might also like