Which of the following best explains why trees are used to represent hierarchical data instead of arrays or linked lists?
Think about how many children each element can have and how data is connected.
Trees allow nodes to have multiple children, which matches hierarchical relationships like family trees or file systems. Arrays and linked lists are linear, so they cannot represent branching structures naturally.
Consider a simple tree node structure and a linked list node structure. What is the printed output after connecting nodes as shown?
type TreeNode struct { value int children []*TreeNode } type ListNode struct { value int next *ListNode } func main() { root := &TreeNode{value: 1} child1 := &TreeNode{value: 2} child2 := &TreeNode{value: 3} root.children = []*TreeNode{child1, child2} head := &ListNode{value: 1} node2 := &ListNode{value: 2} node3 := &ListNode{value: 3} head.next = node2 node2.next = node3 // Print tree root and its children values print("Tree: ") print(root.value) for _, c := range root.children { print(" -> ", c.value) } println() // Print linked list values print("List: ") current := head for current != nil { print(current.value) if current.next != nil { print(" -> ") } current = current.next } println() }
Look at how children are stored in the tree and how next pointers work in the list.
The tree root has two children with values 2 and 3, so printing root and children shows 1 -> 2 -> 3. The linked list nodes are connected linearly 1 -> 2 -> 3.
Given the following Go code for tree traversal, why does it fail to print all nodes?
type TreeNode struct { value int children []*TreeNode } func traverse(node *TreeNode) { if node == nil { return } println(node.value) for _, child := range node.children { traverse(child) // Problem fixed here } } func main() { root := &TreeNode{value: 1} child1 := &TreeNode{value: 2} child2 := &TreeNode{value: 3} root.children = []*TreeNode{child1, child2} traverse(root) }
Check what node is passed in the recursive call inside the loop.
The code calls traverse(node) inside the loop instead of traverse(child), so it keeps calling itself on the same node, causing infinite recursion and never visiting children.
Which option contains a syntax error when initializing a tree node with children in Go?
type TreeNode struct { value int children []*TreeNode } func main() { root := &TreeNode{ value: 1, children: []*TreeNode{ &TreeNode{value: 2}, &TreeNode{value: 3}, }, } }
Check the type of the children slice and the types of elements inside it.
The children field expects a slice of pointers to TreeNode (type []*TreeNode). Option A uses a slice of TreeNode (not pointers), causing a type mismatch syntax error.
Which statement best explains why trees like binary search trees can provide faster search than arrays or linked lists?
Think about how many elements you need to check when searching in each structure.
Binary search trees allow skipping half of the remaining data at each step, making search faster (logarithmic time). Arrays and linked lists require linear search checking each element.