改正一段代码:#include <stdlib.h> #include <stdio.h> typedef struct Node { int data; struct Node* left; struct Node* right; }BiTreeNode,*BiTree; void Initiate(BiTree *root,DataType x){ *root=(BiTree)mallco(sizeof(BiTreeNode)); (*root)->data=x; (*root)->left=NULL; (*root)->right=NULL; } BiTree InsertLeftNode(BiTree curr,DataType x) { BiTree s,t; if(curr==NULL) return NULL; t=curr->left; s=(BiTree)mallloc(sizeof(BiTreeNode)); s->data=x; s->left=t; s->right=NULL; vurr->left=s; return curr->left; } BiTree InsertRightNode(BiTree curr.DataType x) { BiTree s,t; if(curr==NULL) return NULL; t=curr->right; s=(BiTree)mallloc(sizeof(BiTreeNode)); s->data=x; s->right=t; s->left=NULL; vurr->left=s; return curr->right; } typedef char DataType; void main(void) { InsertRightNode *root, *p; Initiate(&root); p = InsertLeftNode(root, 'A'); p = InsertLeftNode(p, 'B'); p = InsertLeftNode(p, 'D'); p = InsertRightNode(p, 'G'); p = InsertRightNode(root->left, 'C'); InsertLeftNode(p, 'E'); InsertRightNode(p, 'F'); } void PrintBiTree(BiTreeNode *bt, int n) { int i; if(bt == NULL) return; PrintBiTree(bt->right, n+1); for(i = 0; i < n-1; i++) printf(“ ”); // 打印3个空格 if(n > 0) { printf(“---”); printf(“%c\n”, bt->data); } PrintBiTree(bt->left, n+1); }
时间: 2023-07-15 10:10:26 浏览: 195
改正后的代码如下:
```c
#include <stdlib.h>
#include <stdio.h>
typedef char DataType;
typedef struct BiTreeNode {
int data;
struct BiTreeNode* left;
struct BiTreeNode* right;
} BiTreeNode, *BiTree;
void Initiate(BiTree* root, DataType x) {
*root = (BiTree)malloc(sizeof(BiTreeNode));
(*root)->data = x;
(*root)->left = NULL;
(*root)->right = NULL;
}
BiTree InsertLeftNode(BiTree curr, DataType x) {
BiTree s, t;
if (curr == NULL) return NULL;
t = curr->left;
s = (BiTree)malloc(sizeof(BiTreeNode));
s->data = x;
s->left = t;
s->right = NULL;
curr->left = s;
return curr->left;
}
BiTree InsertRightNode(BiTree curr, DataType x) {
BiTree s, t;
if (curr == NULL) return NULL;
t = curr->right;
s = (BiTree)malloc(sizeof(BiTreeNode));
s->data = x;
s->right = t;
s->left = NULL;
curr->right = s;
return curr->right;
}
void PrintBiTree(BiTreeNode* bt, int n) {
int i;
if (bt == NULL) return;
PrintBiTree(bt->right, n + 1);
for (i = 0; i < n - 1; i++) printf(" "); // 打印3个空格
if (n > 0) {
printf("---");
printf("%c\n", bt->data);
}
PrintBiTree(bt->left, n + 1);
}
int main(void) {
BiTree root, p;
Initiate(&root, 'A');
p = InsertLeftNode(root, 'B');
p = InsertLeftNode(p, 'D');
p = InsertRightNode(p, 'G');
p = InsertRightNode(root->left, 'C');
InsertLeftNode(p, 'E');
InsertRightNode(p, 'F');
PrintBiTree(root, 0);
return 0;
}
```
阅读全文