#define NULL_BITREE_TOKEN '#' #define MAX_STACK_LEVEL 100 #define MAX_QUEUE_SIZE 128 typedef struct BiTreeNode { char Data; struct BiTreeNode *Parent; struct BiTreeNode *LChild; struct BiTreeNode *RChild; }BiTreeNode, *BiTreePtr; enum BiTreeChildType { btLeftChild, btRightChild, btNull }; typedef struct { BiTreePtr Elem[ MAX_STACK_LEVEL ]; int Top; }SeqBiTreePtrStack; typedef struct { BiTreePtr Elem[ MAX_QUEUE_SIZE ]; int Front, Rear; }SeqBiTreePtrQueue; int TotalNodeNum = 0; 根据以上代码补全下面代码并且成功运行:显示二叉树中给定结点的祖先结点 void DisplayBinaryTreeNodeAncestors(BiTreePtr Root, char NodeData){}
时间: 2024-03-30 17:36:47 浏览: 98
C++ 中pragma once 与 #ifndef _XXX_H_ #define _XXX_H_的区别
5星 · 资源好评率100%
void DisplayBinaryTreeNodeAncestors(BiTreePtr Root, char NodeData){
SeqBiTreePtrStack S;
BiTreePtr p = Root, q = NULL;
S.Top = -1;
while(p || S.Top != -1){
if(p){
S.Elem[++S.Top] = p;
p = p->LChild;
}
else{
p = S.Elem[S.Top];
if(p->Data == NodeData){
printf("Ancestors of node %c are: ", NodeData);
for(int i = 0; i < S.Top; i++){
printf("%c ", S.Elem[i]->Data);
}
printf("\n");
return;
}
if(p->RChild && p->RChild != q){
p = p->RChild;
S.Elem[++S.Top] = p;
p = p->LChild;
}
else{
q = p;
--S.Top;
p = NULL;
}
}
}
printf("Node %c not found in the tree!\n", NodeData);
}
阅读全文