优化以下代码#include <stdio.h> #include <stdlib.h> typedef struct TreeNode { char data; struct TreeNode* left; struct TreeNode* right; } TreeNode; TreeNode* createTree() { char ch; TreeNode* root; scanf("%c", &ch); if (ch == '#') { return NULL; } root = (TreeNode*)malloc(sizeof(TreeNode)); root->data = ch; root->left = createTree(); root->right = createTree(); return root; } void digui(TreeNode* root) { if (root == NULL) { return; } digui(root->left); printf("%c ", root->data); digui(root->right); } typedef struct StackNode { TreeNode* tree; struct StackNode* next; } StackNode; typedef struct Stack { StackNode* top; int size; } Stack; Stack* createStack() { Stack* stack = (Stack*)malloc(sizeof(Stack)); stack->top = NULL; stack->size = 0; return stack; } void push(Stack* stack, TreeNode* tree) { StackNode* node; node = (StackNode*)malloc(sizeof(StackNode)); node->tree = tree; node->next = stack->top; stack->top = node; stack->size++; } TreeNode* pop(Stack* stack) { TreeNode* tree; StackNode* temp; if (stack->size == 0) { return NULL; } tree = stack->top->tree; temp = stack->top; stack->top = stack->top->next; stack->size--; free(temp); return tree; } void feidigui(TreeNode* root) { Stack* stack; TreeNode* p; stack = createStack(); p = root; while (p != NULL || stack->size != 0) { while (p != NULL) { push(stack, p); p = p->left; } if (stack->size != 0) { p = pop(stack); printf("%c ", p->data); p = p->right; } } } int getHeight(TreeNode* root) { int leftHeight,rightHeight,max; if (root == NULL) { return 0; } leftHeight = getHeight(root->left); rightHeight = getHeight(root->right); max=leftHeight>rightHeight?leftHeight:rightHeight; return max+1; }
时间: 2024-03-03 14:51:55 浏览: 99
首先,这段代码的缩进不太规范,可以调整一下,使得代码更加清晰易读。
其次,在函数中出现了多次重复的代码,如创建栈、入栈、出栈等,可以将这些代码封装成一个通用的函数,以便在程序中反复使用。
最后,可以考虑使用更加简洁的写法,如使用三目运算符代替if-else语句,使用递归函数代替while循环等,以提高代码的可读性和可维护性。同时,可以加上注释,方便他人理解代码逻辑。
下面是一个优化后的代码示例:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
char data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 创建二叉树
TreeNode* createTree() {
char ch;
scanf("%c", &ch);
if (ch == '#') {
return NULL;
}
TreeNode *root = (TreeNode*)malloc(sizeof(TreeNode));
root->data = ch;
root->left = createTree();
root->right = createTree();
return root;
}
// 递归遍历二叉树
void digui(TreeNode *root) {
if (root == NULL) {
return;
}
digui(root->left);
printf("%c ", root->data);
digui(root->right);
}
typedef struct StackNode {
TreeNode *tree;
struct StackNode *next;
} StackNode;
typedef struct Stack {
StackNode *top;
int size;
} Stack;
// 创建栈
Stack* createStack() {
Stack *stack = (Stack*)malloc(sizeof(Stack));
stack->top = NULL;
stack->size = 0;
return stack;
}
// 入栈
void push(Stack *stack, TreeNode *tree) {
StackNode *node = (StackNode*)malloc(sizeof(StackNode));
node->tree = tree;
node->next = stack->top;
stack->top = node;
stack->size++;
}
// 出栈
TreeNode* pop(Stack *stack) {
if (stack->size == 0) {
return NULL;
}
TreeNode *tree = stack->top->tree;
StackNode *temp = stack->top;
stack->top = stack->top->next;
stack->size--;
free(temp);
return tree;
}
// 非递归遍历二叉树
void feidigui(TreeNode *root) {
Stack *stack = createStack();
TreeNode *p = root;
while (p != NULL || stack->size != 0) {
if (p != NULL) {
push(stack, p);
p = p->left;
} else {
p = pop(stack);
printf("%c ", p->data);
p = p->right;
}
}
}
// 获取二叉树的高度
int getHeight(TreeNode *root) {
if (root == NULL) {
return 0;
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
int main() {
TreeNode *root = createTree();
digui(root);
printf("\n");
feidigui(root);
printf("\n");
printf("%d\n", getHeight(root));
return 0;
}
```
阅读全文