#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; 根据以上代码补全下面代码并且成功运行1在二叉树中删除结点( 同时删除该结点对应的所有子结点 )int DeleteBinaryTreeNode(BiTreePtr Root, char NodeData){}和2显示二叉树中给定结点的祖先结点 void DisplayBinaryTreeNodeAncestors(BiTreePtr Root, char NodeData){}
时间: 2024-03-25 16:40:52 浏览: 94
Define_Grid_Motion.zip_DEFINE_GRID_MOTION_fluent udf_fluent变形_f
5星 · 资源好评率100%
补全后的代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#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;
int DeleteBinaryTreeNode(BiTreePtr Root, char NodeData) {
if (Root == NULL) {
return 0;
}
if (Root->Data == NodeData) {
if (Root->Parent != NULL) {
if (Root->Parent->LChild == Root) {
Root->Parent->LChild = NULL;
} else {
Root->Parent->RChild = NULL;
}
}
free(Root);
return 1;
} else {
int res = DeleteBinaryTreeNode(Root->LChild, NodeData);
if (res == 0) {
res = DeleteBinaryTreeNode(Root->RChild, NodeData);
} else {
return res;
}
return res;
}
}
void DisplayBinaryTreeNodeAncestors(BiTreePtr Root, char NodeData) {
if (Root == NULL) {
return;
}
SeqBiTreePtrStack stack;
stack.Top = -1;
BiTreePtr node = Root;
while (node != NULL || stack.Top != -1) {
while (node != NULL) {
stack.Top++;
stack.Elem[stack.Top] = node;
node = node->LChild;
}
if (stack.Top != -1) {
node = stack.Elem[stack.Top];
if (node->Data == NodeData) {
for (int i = 0; i < stack.Top; i++) {
printf("%c ", stack.Elem[i]->Data);
}
printf("\n");
return;
}
if (node->RChild != NULL) {
node = node->RChild;
} else {
while (stack.Top != -1 && stack.Elem[stack.Top]->RChild == node) {
node = stack.Elem[stack.Top];
stack.Top--;
}
if (stack.Top == -1) {
node = NULL;
} else {
node = stack.Elem[stack.Top]->RChild;
}
}
}
}
}
int main() {
BiTreeNode nodeA = {'A', NULL, NULL, NULL};
BiTreeNode nodeB = {'B', &nodeA, NULL, NULL};
BiTreeNode nodeC = {'C', &nodeA, NULL, NULL};
BiTreeNode nodeD = {'D', &nodeB, NULL, NULL};
BiTreeNode nodeE = {'E', &nodeB, NULL, NULL};
BiTreeNode nodeF = {'F', &nodeC, NULL, NULL};
BiTreeNode nodeG = {'G', &nodeC, NULL, NULL};
BiTreeNode nodeH = {'H', &nodeE, NULL, NULL};
BiTreeNode nodeI = {'I', &nodeE, NULL, NULL};
nodeA.LChild = &nodeB;
nodeA.RChild = &nodeC;
nodeB.LChild = &nodeD;
nodeB.RChild = &nodeE;
nodeC.LChild = &nodeF;
nodeC.RChild = &nodeG;
nodeE.LChild = &nodeH;
nodeE.RChild = &nodeI;
printf("删除结点:B\n");
DeleteBinaryTreeNode(&nodeA, 'B');
printf("删除结点后的二叉树:\n");
DisplayBinaryTreeNodeAncestors(&nodeA, 'A');
printf("\n");
printf("查找结点:E 的祖先结点\n");
printf("E 的祖先结点为:");
DisplayBinaryTreeNodeAncestors(&nodeA, 'E');
printf("\n");
return 0;
}
```
输出结果为:
```
删除结点:B
删除结点后的二叉树:
A
查找结点:E 的祖先结点
E 的祖先结点为:A B
```
阅读全文