Suppose that d ≥ 2 is an integer constant. In a
d-ary tree, each node has at most d nonempty
subtrees. For example, the trees discussed along with heaps had
d = 2. We can represent a nearly complete d-ary
tree with n nodes using an array whose indexes range from
0 to n−1. (This is different from Cormen’s arrays, whose
indexes range from 1 to n.)
Suppose that i is the index
of a node in the array. Then CHILD(i, j) is the
index of the jth child of the node at i, where 1
≤ j ≤ d. If there is no such child, then
CHILD(i, j) ≥ n. Also,
PARENT(i) is the index of the parent of the node at
i. If there is no such parent, then PARENT(i)
< 0.
1a. Show a short algorithm for CHILD. Your algorithm must run in Θ(1) time.
1b. Show a short algorithm for PARENT. Your algorithm must run in Θ(1) time.
Answer:
in the same way if j is the index of a node, afterward index of its parent will be ( j-1) / d.
a. The short algorithm for child run time consider by:
CHILD( i, j)
1. child_index = i*d + j
2. Return (child_index) //If the child do exist then value returned will be >= n
b.The short algorithm for parent run time consider by:
PARENT(i )
1. If i == 0 then return -1; //as root node do not have parent
2. Return (i-1)/d //Return correct index
Both algorithm take time \Theta(1).
Suppose that d ≥ 2 is an integer constant. In a d-ary tree, each node has...
Suppose that binary heaps are represented using explicit links. Give a simple algorithm to find the tree node that is at implicit position i. instructions: provide Java-like pseudocode. The implicit position of a node refers to the index it would have if the heap was stored in the array format reviewed in class (first element at index 1).
2. A regular binary tree is a binary tree whose internal nodes all have two subtrees (left and right). In other words, all their nodes have either zero subtrees (in which case they are leaves) or two subtrees (in which case they are internal nodes). Suppose that you have a boolean function that tells you, for each node of the tree, whether it is a leaf or not (call it: leaf(n), for node n). a) Write a recursive function that...
In the lectures, we studied binary heaps. A min-Heap can be visualized as a binary tree of height with each node having at most two children with the property that value of a node is at most the value of its children. Such heap containing n elements can be represented (stored) as an array with the property Suppose that you would like to construct a & min Heap: each node has at most& children and the value of a node...
You are given a binary tree of the form: Each node in the tree has a left child and a right child. Each of the children will be extended as a linked list. Every node has the following attributes: key, left node, right node, and next node. The next node allows a node, that is a part of the tree, to be extended as a linked list. The diamonds represent the next nodes, which are part of the linked list...
Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes The key T.key is the root node's key. The left child T.left is Ts left subtree, which is an AVL tree (possibly E). The right child T.right is T's right subtree, which is an AVL tree (possibly E). (a) 5 marsl Write a function RANGECOUNT(T, lo, hi) to count the number of nodes in an AVL tree with...
It is due in 2 hours.. Thanks ! Suppose that an algorithm runs on a tree containing n nodes. What is the time complexity of the algorithm if the time spent per node in the tree is proportional to the number of grandchildren of the node? (Assume that the algorithm spends O(1) time for every node that does not have a grandchild.) In modern software development, a useful utility called make is usually employed to manage the compilation order of...
Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes: • The key T.key is the root node’s key. • The left child T.left is T’s left subtree, which is an AVL tree (possibly E). • The right child T.right is T’s right subtree, which is an AVL tree (possibly E). (a) [5 marks] Write a function RangeCount(T, lo, hi) to count the number of nodes in an...
3. [5 marks] Suppose T is a binary tree that stores an integer key value in each node. Assume the following notation/operations on a binary tree. » the key T.key is the root node's integer key value . the left child T.left is T's left subtree, which is possibly an empty tree (or null) the right child T.right is T's right subtree, which is possibly an empty tree (or null) (a) Write an efficient algorithm FINDMAxPrODuCT(T) in pseudocode that returns...
4. Consider the following subtree of a balanced AVL tree a b AA d X Y d+1 d+2 Now, suppose that subtrees (i.e., subtree X and subtree Y) to be both two levels deeper than its right subtree (i.e., subtree Z) so that we have the following unbalanced AVL tree node at the bottom of subtree Z is deleted, causing its two left a a b d d+1 X d+2 Describe how the AVL tree can be rebalanced. Draw the...
Please help this. Q. When is the right subtree of a binary tree processed before the left? When using an inorder traversal algorithm. When using a preorder traversal algorithm. When using a postorder traversal algorithm. Never. The left always goes before the right in the standard traversal algorithms. Q. In a postorder tree traversal, each node is processed ____ its children. instead of after before relative to the ordinal values of Q17. If you have an array of 4,000 sorted...