Complete the code to declare the horizontal distance variable in the Node class.
public class Node { public int data; public Node left, right; public int [1]; public Node(int item) { data = item; left = right = null; [1] = 0; } }
The horizontal distance variable is commonly named horizontalDistance to track the node's position relative to the root in the bottom view algorithm.
Complete the code to add the root node to the queue with horizontal distance 0.
Queue<(Node node, int hd)> queue = new Queue<(Node, int)>(); queue.Enqueue(([1], 0));
We start the traversal by adding the root node with horizontal distance 0 to the queue.
Fix the error in updating the horizontal distance when moving to the left child.
if (current.node.left != null) { queue.Enqueue((current.node.left, current.[1] - 1)); }
The tuple stores the horizontal distance as 'hd', so we must use 'current.hd' to update it.
Fill both blanks to update the dictionary with the bottom view node data at the current horizontal distance.
Dictionary<int, int> bottomViewMap = new Dictionary<int, int>(); while (queue.Count > 0) { var current = queue.Dequeue(); bottomViewMap[[1]] = [2]; // rest of traversal code }
The dictionary key is the horizontal distance current.hd, and the value is the node's data current.node.data.
Fill the blanks to print the bottom view nodes in order of their horizontal distances.
foreach (var key in bottomViewMap.Keys.[1]) { Console.Write(bottomViewMap[[2]] + " "); } Console.WriteLine();
We use OrderBy(k => k) to sort keys ascending, then print values by each key.
