The tree on this board is the shape every question below is asked about.
A company has 100,000 people (a teaching value). Every person has one manager, except the head. Software holds this as a tree. The manager of a person is that person’s parent, and everyone on the way up to the head is an ancestor. Four questions arrive all day. Who is the 77,777th ancestor of this person? Who is the lowest manager above both A and B? What is the total salary under a manager? How far is each person from everyone else? Each plain answer is a walk through the tree.
By the end you will be able to answer the first two in about 17 steps, turn the tree into a line, find every person’s total distance, and sum a path while salaries change. You will solve two free LeetCode problems, 1483 and 834.
The history (optional)
Sleator and Tarjan published “A Data Structure for Dynamic Trees” in the Journal of Computer and System Sciences, volume 26, in 1983. Wikipedia credits it with the heavy path split used here. Harel and Tarjan published “Fast Algorithms for Finding Nearest Common Ancestors” in the SIAM Journal on Computing, volume 13, in May 1984.