Maximum Distance of nodes in a transformed binary tree (the longest path of the tree) -- Non-recursive Solution
Problem description:
If we regard a binary tree as a graph, and the line between parent and child nodes is bidirectional, we would like to define "distance" as the number of edges between two nodes.
Write a program to find the distance between the two farthest nodes in a binary tree. Tree for testing:
N1
/\
N2 n3
/\
N4 n5
/\/\
N6 n7 n8 Environment
//
N10 n11
UnfortunatelyAt the beginning, I read the wrong question: "the connection between parent and child nodes is considered as two-way, in the definition of a node, there is really a pointer to the parent node ....
I will make the mistake first. Therefore, in order to implement non-recursive solutions, a strange number node struct is defined ....
As a result, things become:I changed the question requirements to solve the problem........ Dizzy
In fact, for the original question, according to the tree structure, use the node defined by me to initialize a deformed binary tree, and then use thisNon-recursive SolutionYou can solve the problem, but you still need to do some work to initialize the tree. This solution is not completely meaningless ....
The solution to the original question will be discussed later.
Algorithm:
The question is to find the longest path in a tree.
For node t, the longest path of the tree with it as the root must be one of the following threeMaximum Value:
Longstpath of the Left subtree of ① t
② The longstpath of the right subtree of t
③ Depth of the Left subtree of t + depth of the right subtree of t + 2
-- Conclusion 1
Therefore, each node has two important attributes:Depth② For the tree with the node as the rootMaximum path length
Start from each leaf node and process it from the bottom up. Each processing is called:
If the two attributes of the node are determined, "inform" the parent node. After the parent node obtains the attributes of all the child nodes, its two attributes can be determined based on Conclusion 1. Continue to "report" your attributes.
For a parent node that does not know all the attributes of the child node, let him wait for the report of the child node in each processing. Obviously, a queue is required to store the waiting node.
Code implementation:
# Include
# Include "Queue. h "using namespace std; // The node Structure struct BinaryTreeNode {BinaryTreeNode * father = NULL; // point to the parent node BinaryTreeNode * left = NULL; BinaryTreeNode * right = NULL; int arrived = 0; // number of records that the subtree depth value reaches. The value is 0, 1, or 2 int depth = 0; // The depth of the tree with this node as the root int longstpath = 0; // The longest path bool stored = false in the tree with this node as the root; // whether the column is waiting }; // take struct int max2 (int a, int B) {if (a> B) return a; else return B;} // take struct int max3 (int a, int B, int B, int c) {return max2 (max2 (a, B), c) ;}// depending on the situation (some nodes do not have left/right subnodes ), update longstpath // longstpath must be the maximum value of the following three numbers: longstpath of the Left subtree, longstpath of the right subtree, left subtree depth, and + 2 void SetLongstpath (BinaryTreeNode * temp) {int lpath, rpath, ldepth, rdepth; if (temp-> left) {lpath = temp-> left-> longstpath; ldepth = temp-> left-> depth ;} else lpath = ldepth = 0; if (temp-> right) {rpath = temp-> right-> longstpath; rdepth = temp-> right-> depth ;} else rpath = rdepth = 0; temp-> longstpath = max3 (lpath, rpath, ldepth + rdepth + 2); // update the longest path longstpath} int FindPath (Queue
& Queue) {BinaryTreeNode * temp; while (! Queue. isEmpty () {queue. delete (temp); temp-> stored = false; if (temp-> arrived = 2) // The Sub-nodes have arrived, and everything is ready {SetLongstpath (temp ); // update the longstpatlif (temp-> father) of temp // if it is not the root node, move it up (that is, process the parent node) {temp-> father-> depth = max2 (temp-> father-> depth, temp-> depth + 1 ); // update the depth value of the parent node temp-> father-> arrived ++; // The number of child nodes reached + 1if (! Temp-> father-> stored) // indicates that this is the first child node to arrive. If the parent node is never included in the column, it will be included in the {queue. add (temp-> father); temp-> father-> stored = true;} else // root node, return longstpathreturn temp-> longstpath ;} if (temp-> arrived = 1) // if a child node has not arrived, the conditions for updating longstpath are insufficient. re-enter the column and wait for the child node {queue. add (temp); temp-> stored = true ;}} void main () {BinaryTreeNode * n1 = new BinaryTreeNode; BinaryTreeNode * n2 = new BinaryTreeNode; binaryTreeNode * n3 = new BinaryTreeNode; BinaryTreeNode * n4 = new BinaryTreeNode; BinaryTreeNode * n5 = new feature; BinaryTreeNode * n6 = new feature; BinaryTreeNode * n7 = new BinaryTreeNode; binaryTreeNode * n8 = new BinaryTreeNode; BinaryTreeNode * handle = new BinaryTreeNode; BinaryTreeNode * n10 = new BinaryTreeNode; BinaryTreeNode * n11 = new BinaryTreeNode; // construct the binary tree n2-> father = n3-> father = n1; n4-> father = n5-> father = n2; n6-> father = n7-> father = n4; n8-> father = birthday-> father = n5; n10-> father = n6; n11-> father = birthday; n6-> left = n10; n4-> left = n6; n4-> right = n7; align-> left = n11; n5-> left = n8; n5-> right = align; n2-> left = n4; n2-> right = n5; n1-> left = n2; n1-> right = n3; n3-> arrived = 2; // The initial leaf node is 2n7-> arrived = 2; n8-> arrived = 2; n10-> arrived = 2; n11-> arrived = 2; n6-> arrived = 1; // but the child node is 1-> arrived = 1; Queue
Queue; queue. add (n3); n3-> stored = true; queue. add (n7); n7-> stored = true; queue. add (n8); n8-> stored = true; queue. add (n10); n10-> stored = true; queue. add (n11); n11-> stored = true; cout <"Maximum path length:" <FindPath (queue) <endl; system ("pause ");}