Binary Search Tree

Binary Search Tree

4 questions · Fundamental Engineering

Practice

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The function isPerfectBinaryTree checks whether the binary tree with a given root node is perfect. In a perfect binary tree, all the internal nodes have exactly two children, and all leaf nodes are at the same level. The function returns true if the tree is perfect, or false otherwise.
The class Node models a node of a binary tree as described in Table 1. A Node-type variable holds a reference to an instance of the class Node.

Table 1 Class Node

Member variableTypeDescription
KeyintegerInteger value stored in a node of a binary tree.
LeftNodeA reference to the left child of the node. If the left child does not exist, it is set to undefined.
RightNodeA reference to the right child of the node. If the right child does not exist, it is set to undefined.

Table 2 describes the functions used in the program.

Table 2 Functions

FunctionReturn valueDescription
pow(integer a,integer b)integerReturns the result of raising a to the power of b (i.e., ab ).
max(integer a,integer b)integerReturns the maximum of the two integers a and b.
[Program]
○ boolean: isPerfectBinaryTree(Node: root)
// The argument root holds a reference to the root node
// of a binary tree.
integer: height, size
height ← getHeight(root)
size ← getSize(root)
return A
○ integer: getHeight(Node: root)
integer: leftHeight, rightHeight
if (root is undefined)
return 0
endif
leftHeight ← getHeight(root.left)
rightHeight ← getHeight(root.right)
return 1 + max(leftHeight, rightHeight)
○ integer: getSize(Node: root)
if (root is undefined)
return 0
endif
return B

Answer Group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A through C in the program.

Binary Search Trees (BST) are a fundamental data structure where each node has at most two children: a left child and a right child. The key property of a BST is that each value of all nodes in its left subtree are less than the value of the node, and each value of all nodes in its right subtree are greater than the value of the node. The table shows the member variables of the class Node. Node-type variable holds a reference to an instance of the class Node. Each node in a BST has a different value.

Table Member variables of class Node

Member variableTypeDescription
keyintegerInteger to be stored in the node.
leftNodeReference to the instance that holds the left child of a binary tree. If no left child exists, it is undefined.
rightNodeReference to the instance that holds the right child of a binary tree. If no right child exists, it is undefined.

The function remove removes a node with the same key value as the argument key from the BST specified by the argument node and returns the resulting BST. The function searches for a node with the same key value, and if the found node (call it X here) does not have a right subtree, the function returns its left subtree. If X has a right subtree, the function locates the node (call it Y here) with the smallest key value in the right subtree, replaces the key value of X with that of Y, and then removes Y. If there is no node with the same key value in the BST, the function performs no operations. The procedure test demonstrates this behavior by removing 1 node from a BST containing 7 nodes. The figure shows the BST before and after node removal.

12345671345673remove 2

Figure BST before and after node removal

[Program]
○ Node: remove(Node: node, integer: key)
Node: node2
if (node is undefined)
return undefined
elseif (key < node.key)
node.left ← remove(node.left, key)
elseif (key > node.key)
node.right ← remove(node.right, key)
elseif (node.right is undefined)
return node.left
else
node2 ← A
while (B)
node2 ← node2.left
endwhile
node.key ← node2.key
node.right ← remove(node.right, C)
endif
return node
○ test()
Node: root ← the root node of BST on the left side of the figure
root ← remove(root, 2)

Answer group

OptionABC

From the answer group below, select the correct combination of answers to be inserted into A through C in the program.

Given the root of two binary trees, the aim of the following program is to check whether these two trees are identical. Two binary trees are defined as identical if they satisfy the following formal conditions:

  • •Structure: Both trees have the same structure, implying that for every corresponding node in the two trees, the arrangement of the left and right children is the same.
  • •Node Values: Each corresponding node in the two trees must contain the same value. Specifically, if tree T1 has a node N1 with value v1 and tree T2 has the corresponding node N2 with value v2, then v1 = v2.

The function isSameTree takes two instances of class TreeNode as the argument representing the root nodes of two binary trees, and returns true if the trees are identical, or false otherwise. The member variables of TreeNode are listed in the table below:

Table Explanation of the member variables of the class TreeNode

Member variableTypeDescription
valintegerThe integer value of a current node
leftTreeNodeLeft child node
rightTreeNodeRight child node
[Program]
○ boolean: isSameTree(TreeNode: p, TreeNode: q)
boolean: checkLeft, checkRight
if (p = undefined A q = undefined)
return true
endif
if (p = undefined B q = undefined)
return false
endif
if (p.val ≠ q.val)
return false
endif
checkLeft ← isSameTree(p.left, q.left)
checkRight ← isSameTree(p.right, q.right)
return checkLeft C checkRight

Answer group

OptionABC

From the answer group below, select the correct combination of answers to be inserted into A through C in the program. Here, the array index starts at 1.

The procedure preorder traverses a binary tree and outputs the value of each node by following the sequence: root, left subtree, and right subtree using a stack (Last In, First Out). Each node of the binary tree is represented by the class Node. The table shows the description of the class Node. The Node-type variable holds a reference to an instance of the class Node. The argument root holds a reference to the root of the binary tree, which is an instance of the class Node. In the program, areas outside of the array must not be referenced.

Table Class Node

Member variableTypeDescription
infocharacterCharacter type value to be stored in a node of a binary tree.
leftNodeA reference to the left child of a binary tree. If there is no left child, the status is undefined
rightNodeA reference to the right child of a binary tree. If there is no right child, the status is undefined
[Program]
○ preorder(Node: root)
Node []: stack ← {undefined, ···, undefined}
// an array with sufficient number of elements
Node: v
integer: sp ← 1 // The stack pointer
stack[sp] ← root // Push root to the stack
while (sp is not A)
v ← stack[sp] // Pop an element from the stack
output v.info
sp ← sp - 1
if (B is not undefined)
sp ← sp + 1
stack[sp] ← B
endif
if (C is not undefined)
sp ← sp + 1
stack[sp] ← C
endif
endwhile

Answer group

OptionABC