请简要分析以下代码的含义并输出包括此代码的完整的主函数代码:In0rder1TNode treeif tree ≠ NULL InOrder tree-Lsubtree;Accesstree-data;InOrdertree-Rsubtree;In0rder2TNode treep Ü tree;InitStack S ;do whilep ≠ NULL PushStackS p;p Ü p
此代码为二叉树的中序遍历,输出结果为二叉树中每个节点的值。
完整的主函数代码:
void InOrder(TNode *tree) { if (tree != NULL) { InOrder(tree->Lsubtree); Access(tree->data); InOrder(tree->Rsubtree); } }
void InOrder2(TNode *tree) { TNode *p = tree; InitStack(S); do { while (p != NULL) { PushStack(S, p); p = p->Lsubtree; } if (!EmptyStack(S)) { p = PopStack(S); Access(p->data); p = p->Rsubtree; } } while (!EmptyStack(S) || p != NULL);
原文地址: https://www.cveoy.top/t/topic/eqUG 著作权归作者所有。请勿转载和采集!