Problem: Given a binary tree and one of the nodes, find the next node in the sequence traversal and return. Note that the nodes in the tree contain not only the left and right child nodes, but also pointers to the parent nodes.
Idea: This topic contains three steps:
1. If this node has a right subtree, the next node is the leftmost node of the right child node.
2. If this node does not have a right subtree, and if this node is the left child node of its parent node, the next node is the parent node.
3. If this node does not have a right subtree, and if this node is the right child node of its parent node, keep looking up until you find the first node that is the left node of its parent node, and the next node is the node.
Detailed Code design:
* * Question Description: * (question <coding intervies>) Given a node in a binary tree, please implement A function to retrieve * it next node in the Inorder traversal sequence.
There is a pointer to the parent node in each tree node.
* * #include <stdio.h>//two fork tree node definition struct Binarytreenode {int m_nvalue;
binarytreenode* M_pleft;
binarytreenode* M_pright;
binarytreenode* m_pparent;
};
The next node of the binary tree binarytreenode* GetNext (binarytreenode* pnode) {if (Pnode = null) return null;
binarytreenode* pnext = NULL;
if (pnode->m_pright!= NULL) {binarytreenode* pright = pnode->m_pright;
while (Pright->m_pleft!= NULL) pright = pright->m_pleft;
Pnext = Pright;
else if (pnode->m_pparent!= NULL) {binarytreenode* pcurrent = Pnode;
binarytreenode* pparent = pnode->m_pparent; while (PParENT!= NULL && pcurrent = = pparent->m_pright) {pcurrent = pparent;
pparent = pparent->m_pparent;
} pnext = pparent;
return pnext;
}//==================== Code for Binary trees ==================== binarytreenode* createbinarytreenode (int value) {
binarytreenode* pnode = new Binarytreenode ();
Pnode->m_nvalue = value;
Pnode->m_pleft = NULL;
Pnode->m_pright = NULL;
Pnode->m_pparent = NULL;
return pnode;
} void Connecttreenodes (binarytreenode* pparent, binarytreenode* pleft, binarytreenode* pright) {if (pParent NULL)
{pparent->m_pleft = Pleft;
Pparent->m_pright = Pright;
if (pleft!= NULL) pleft->m_pparent = pparent;
if (pright!= NULL) pright->m_pparent = pparent; } void Printtreenode (binarytreenode* pnode) {if (Pnode!= NULL) {printf ("value of this node is:%d\n ", pnode->m_nvalue);
if (pnode->m_pleft!= NULL) printf ("value of it left":%d.\n, Pnode->m_pleft->m_nvalue);
else printf ("Left child is null.\n");
if (pnode->m_pright!= NULL) printf ("Value of its right:%d.\n", pnode->m_pright->m_nvalue);
else printf ("Right child is null.\n");
else {printf ("This node is null.\n");
printf ("\ n");
} void Printtree (binarytreenode* proot) {printtreenode (proot);
if (proot!= null) {if (proot->m_pleft!= null) printtree (proot->m_pleft);
if (proot->m_pright!= NULL) printtree (proot->m_pright); } void Destroytree (binarytreenode* proot) {if (proot!= NULL) {binarytreenode* pleft = proot->m_ple
Ft
binarytreenode* pright = proot->m_pright;
Delete Proot;
Proot = NULL; DestroyTree (Pleft);
Destroytree (Pright); }//==================== test Code ==================== void Test (char* testname, binarytreenode* Pnode, Binarytreenod
E* expected) {if (testname!= NULL) printf ("%s begins:", testname);
binarytreenode* Pnext = GetNext (Pnode);
if (Pnext = = expected) printf ("passed.\n");
else printf ("failed.\n"); //8//6//5 7 9 one void Test1_7 () {binarytreenode* PNode8 = Createbinarytre
Enode (8);
binarytreenode* PNode6 = Createbinarytreenode (6);
binarytreenode* PNode10 = Createbinarytreenode (10);
binarytreenode* PNODE5 = Createbinarytreenode (5);
binarytreenode* PNode7 = Createbinarytreenode (7);
binarytreenode* PNode9 = Createbinarytreenode (9);
binarytreenode* pNode11 = Createbinarytreenode (11);
Connecttreenodes (PNode8, PNode6, PNode10);
Connecttreenodes (PNode6, PNODE5, PNode7);
Connecttreenodes (PNode10, PNode9, PNODE11); Test ("Test1", PNode8, PNode9);
Test ("Test2", PNode6, PNode7);
Test ("Test3", PNode10, PNODE11);
Test ("Test4", PNode5, PNode6);
Test ("Test5", PNode7, PNode8);
Test ("Test6", PNode9, PNode10);
Test ("Test7", PNode11, NULL);
Destroytree (PNODE8); }//5//4//3//2 void Test8_11 () {binarytreenode* PNODE5 = Createbinarytreenode
(5);
binarytreenode* pNode4 = Createbinarytreenode (4);
binarytreenode* pNode3 = Createbinarytreenode (3);
binarytreenode* PNode2 = Createbinarytreenode (2);
Connecttreenodes (PNODE5, PNode4, NULL);
Connecttreenodes (PNode4, PNode3, NULL);
Connecttreenodes (PNode3, PNode2, NULL);
Test ("Test8", PNode5, NULL);
Test ("Test9", PNode4, PNODE5);
Test ("Test10", PNode3, PNODE4);
Test ("Test11", PNode2, PNode3);
Destroytree (PNODE5); }//2//3//4//5 void Test12_15 () {binarytreenode* PNode2 = Createbinarytreen
Ode (2); Binarytreenode* pNode3 = Createbinarytreenode (3);
binarytreenode* pNode4 = Createbinarytreenode (4);
binarytreenode* PNODE5 = Createbinarytreenode (5);
Connecttreenodes (PNode2, NULL, pNode3);
Connecttreenodes (PNode3, NULL, PNODE4);
Connecttreenodes (PNode4, NULL, PNODE5);
Test ("Test12", PNode5, NULL);
Test ("Test13", PNode4, PNODE5);
Test ("Test14", PNode3, PNODE4);
Test ("Test15", PNode2, PNode3);
Destroytree (PNODE2);
} void Test16 () {binarytreenode* PNODE5 = Createbinarytreenode (5);
Test ("Test16", PNode5, NULL);
Destroytree (PNODE5);
int main (int argc, char* argv[]) {test1_7 ();
Test8_11 ();
Test12_15 ();
Test16 (); }