ADS Logs
dequeue() → 5, dequeue() → 3, first() → 7, dequeue() → 7, and the last dequeue() → null because the queue is empty.
* dequeue always takes the oldest element still waiting. *
Operation
Returns
Queue (front … rear)
enqueue(5)
—
(5)
enqueue(3)
—
(5, 3)
dequeue()
5
(3)...
Q What do ancestor, descendant, sibling and subtree mean in a tree?
Ancestors are a node's parent, grandparent and so on up to the root; descendants are its children, grandchildren and so on; siblings share the same parent; a subtree is a node together with all of its descendants.
Take the tree where A has children B, C, D; B has children E, F; a...
Q How is a binary tree stored with linked nodes, and how does it differ from a general tree's nodes?
Each node stores four references: its element, its parent, its left child and its right child. The general tree's sequence of children is replaced by exactly two fixed fields.
* Solid arrows point down to the children, dashed ones back up to the parent; ∅ marks a missing child....
Q In the tree where A has children B and C, B has children D and E, and C has only a right child F, wh...
Preorder A B D E C F · inorder D B E A C F · postorder D E B F C A · breadth-first A B C D E F.
* C has only a right child, so F comes after C in inorder. *
* Same tree, four orders; the leaves D, E, F keep their relative order in the first three. *
Work each one out from its r...
Q What is an abstract data type (ADT), and what does its specification contain?
An ADT describes a data structure by what it does, not how: it specifies the data it stores, the operations on that data, and the error conditions of those operations, while leaving the implementation open.
An ADT specification has three parts:
Data — what is stored (the attribu...
Q Where are queues used?
Wherever things must be handled in arrival order: waiting lines, shared resources such as a printer, and scheduling in multiprogramming and multithreading; indirectly as helper structures in other algorithms and data structures.
Direct applications — all of them "first come, firs...
Q What is the depth of a node, and how is it computed?
The depth of a node is the number of its ancestors (not counting itself), which equals the number of edges on the path up to the root; the root has depth 0, and every other node has depth 1 + the depth of its parent.
The definition gives a recursive algorithm directly:
Algorithm...
Q How is a binary tree stored in an array, and how are a node's children and parent found?
Each node is stored at the index given by its rank: the root has rank 1, the left child of the node at $k$ has rank $2k$, the right child $2k + 1$. The parent of the node at $k$ is therefore at $\lfloor k/2 \rfloor$. Index 0 is left empty.
* The rank of each node is its array in...
Q How is a fully parenthesised arithmetic expression printed from its expression tree?
With a specialised inorder traversal: print "(" before traversing a node's left subtree, print the node's operator or operand when visiting it, and print ")" after traversing its right subtree. The tree for $((2 × (a − 1)) + (3 × b))$ prints exactly that string.
* Operators are...
Q What is the Stack ADT, and what are its operations?
A stack stores objects in last-in, first-out (LIFO) order: push(o) puts an element on top, pop() removes and returns the top element. Helper operations are top() (look without removing), size() and isEmpty().
Picture a stack of plates: you can only put a plate on top and only tak...