A simulated function aims to find the nearest common ancestor of a pair of nodes. the implementation idea is to map the tree to an array first, and set the value range with the node to root, and the value ranges of other nodes to the parent node index, so that you can easily find the parent node. therefore, an obvious simulated function appears.
/* 8-10-12-02-22.29.c -- Chapter 8: Question 10 */</P> <p>/* this is a model that can return a proximate communal parent betwen two node */<br/>/* first, map a tree to an array */</P> <p> elementtype findcommunalparent (const elementtype * const array, const int V, const int W ); </P> <p> elementtype findcommunalparent (const elementtype * const array, const int V, con St int W) <br/>{< br/> If (root = v | root = W) <br/> return failed; <br/> If (array [v] <array [w]) <br/> {<br/> while (W! = Root & array [v]! = Array [w]) <br/> W = array [w]; <br/>}< br/> else if (array [v]> array [w]) <br/>{< br/> while (V! = Root & array [v]! = Arrat [w]) <br/> V = array [v]; <br/>}< br/> else <br/> return array [v]; /* or array [w] */<br/>}