Lect 14 - 2024

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

BITS F232

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

Dr
Trees

BITS F232 Sameera M S 2


Impelementing Stack using queues

How to implement Stack using 2 queues (q1 , q2 )?


▶ Push(s,x): Steps to push x to stack s (We discussed a different method during the
lecture hour. Both are correct)
▶ Enqueue x to q2
▶ One by one dequeue everything from q1 and enqueue to q2
▶ Swap the queues q1 and q2
▶ Pop
▶ Dequeue an element from q1 .

BITS F232 Sameera M S 3


Tree ADT
▶ The positions in a tree are its nodes.
▶ neighboring positions satisfy the parent-child relationships.
▶ Acessor Methods
▶ root(): Return the root of the tree.
▶ parent(v): Return the parent of node v; an error occurs if v is root.
▶ children(v): Return an iterator of the children of node v
▶ The iterator children (v) provides access to the children of v in order. If v is an
external node, then children(v) is an empty iterator.
▶ Query Methods
▶ islnternal(v): Test whether node v is internal.
▶ isExternal(v): Test whether node v is external.
▶ isRoot(v): Test whethér node v is the root.
▶ Generic Methods
▶ size(): Return the number of nodes in the tree.
▶ elements(): Return an iterator of all the elements stored at nodes of the tree.
▶ positions(): Return an iterator of all the nodes of the tree.
▶ swapElements(v,w): Swap the elements stored at the nodes v and w.
▶ replaceElement(v,e): Replace with e Sameera
and return the element stored at node v
BITS F232 MS 4
Depth of a tree
▶ Let y be a node of a tree T. The depth of y is the number of ancestors of y,
excluding y itself.
▶ Depth of root of a tree is 0
▶ For all other nodes y, the depth of y is one plus the depth of the parent of y.
▶ Pseudocode ?

BITS F232 Sameera M S 5


Depth of a tree
▶ Let y be a node of a tree T. The depth of y is the number of ancestors of y,
excluding y itself.
▶ Depth of root of a tree is 0
▶ For all other nodes y, the depth of y is one plus the depth of the parent of y.
▶ Pseudocode ?

▶ Running Time?

BITS F232 Sameera M S 6


Depth of a tree
▶ Let y be a node of a tree T. The depth of y is the number of ancestors of y,
excluding y itself.
▶ Depth of root of a tree is 0
▶ For all other nodes y, the depth of y is one plus the depth of the parent of y.
▶ Pseudocode ?

▶ Running Time?
▶ The running time of algorithm depth(T,v) is O(1 + dv ), where dv denotes the
depth of the node v in the tree T.
BITS F232 Sameera M S 7
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 8


Discussion Time!

BITS F232 Sameera M S 9


BITS F232 Sameera M S 10

You might also like