Alg DS1 Example Test 2
Alg DS1 Example Test 2
In exercises 1-2 do not write code. In exercises 3-4 try to write ecient code
(structograms). Deletion includes explicit deallocation. Do not forget the
types and modes of the formal parameters.
1.b Illustrate the operation of the insertion of 10 into a priority queue on max-
heap h15; 13; 9; 5; 12; 8; 7; 4; 0; 6; 2; 1i. Then redraw the result and illustrate
the operation of the insertion of 18 into it.
We suppose that the physical array containing the heap is long enough.
3.a Write recursive function size(t) calculating the size of binary tree t:Node*
(M T (n) ∈ O(n)). Do not use loops in the structogram.
3.c Write recursive procedure insert(t, k) inserting key k into binary sort tree
t:Node* (M T (h) ∈ O(h)). Do not use loops in the structogram.
3.d Write procedure del1(t) deleting all the internal nodes with a single
child from binary tree t:Node* (M T (n) ∈ O(n)). Do not use loops in the
structograms.
Name: . . . . . . . . . . . . . . . . . . . . . . . . . . . Neptun code: . . . . . . . . . . . . . . . . . . . . .
3
/ \
2 6
/ / \
1 4 7
\
5
The smart textual representation is similar, but we use curly brackets i.e.
{} at level 0 of the tree, square brackets i.e. [] at level 1, normal brackets
i.e. () at level 2, and so on; curly brackets if level mod 3 == 0, square
brackets if level mod 3 == 1, normal brackets if level mod 3 == 2. For
example the tree above is { [ (1) 2 ] 3 [ (4 {5} ) 6 (7) ] } in smart textual
representation.
4.a Give recursive structogram which prints the simple textual form of the
binary tree identied by pointer t. T (n) ∈ Θ(n) where n = n(t).
4.b Give recursive structogram which prints the smart textual form of the
binary tree identied by pointer t. T (n) ∈ Θ(n) where n = n(t).
4.c* Given the simple textual form of a nonempty binary tree in sequential
le F. Write recursive structogram building the linked representation of the
tree in Θ(n) time where n is the number of lexemes of the textual form. We
suppose that we have a read statement that can read a lexeme x in time
0 0 0 0
Θ(1).And we can also check whether x == ( or x == ) in constant time.
Name: . . . . . . . . . . . . . . . . . . . . . . . . . . . Neptun code: . . . . . . . . . . . . . . . . . . . . .
5.b Suppose that the type of the nodes of a binary search tree is
struct NodeS{ key: T; left, right, s: NodeS* }
where the s pointers are undened. Give recursive structogram to ensure
that the attribute s in each node refers to the successor of the node accord-
ing to the inorder traversal where M T (n) ∈ O(n). Attribute s of the last
node (containing the greatest key) must be .
5.c Write boolean function BST(t) deciding whether binary tree t:Node* is
binary search tree (BST) or not (M T (n) ∈ O(n)). We can suppose that
the keys in the nodes of the tree are numbers. Do not use loops in the
structograms.
5.d Write boolean function BSortT(t) deciding whether binary tree t:Node*
is binary sort tree (BSortT) or not (M T (n) ∈ O(n)). Do not use loops in
the structograms.