//二叉树的输入输出操作和判断平衡
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//特殊变量声明
#define _Data char
#define _ArrayMaxSize 100
//结构体
typedef struct TreeNode{
_Data value;
struct TreeNode * father;
struct TreeNode * right;
struct TeenNode * left;
}* pNode, Node;
pNode createNode(_Data value) {
pNode node = (pNode) malloc(sizeof(Node));
node->father = NULL;
node->left = NULL;
node->right = NULL;
node->value = value;
return node;
}
void createByLeft(pNode father) {
_Data value;
scanf("%c", &value);
if (value != '#') {
pNode left = createNode(value);
father->left = left;
createByLeft(left);
}
scanf("%c", &value);
if (value != '#') {
pNode right = createNode(value);
father->right = right;
createByLeft(right);
}
}
void destoryTree(pNode father) {
if (father->right != NULL) {
destoryTree(father->right);
}
if (father->left != NULL) {
destoryTree(father->left);
}
free(father);
}
int getHigh(pNode father) {
int left = 1, right = 1;
if (father->left != NULL) {
left += getHigh(father->left);
}
if (father->right != NULL) {
right += getHigh(father->right);
}
return left>right?left:right;
}
void printTree(pNode father) {
pNode res[_ArrayMaxSize]; //存储列队
int f = 0, r = 0; //前指针 后指针
res[f++] = father;
int count = 0;
int heigh = getHigh(father) + 1;
while(r != f){
// 树循环
pNode cRes[_ArrayMaxSize]; //打印队列
int cf = 0, cr = 0;
while(r != f){
cRes[cf++] = res[(r++)%_ArrayMaxSize];
}
int spaceS = (1 << (heigh - count))+ (heigh - count) - 1;
for(int i = 0; i < spaceS; i++) printf(" ");
int flag = 1;
while(cr != cf){
//层循环
pNode temp = cRes[cr++];
if(temp != NULL){
res[(f++)%_ArrayMaxSize] = temp->left;
res[(f++)%_ArrayMaxSize] = temp->right;
printf("%c", temp->value);
} else{
printf("#");
}
if(flag > 0){
for(int j = (1 << (heigh - count + 1)); j >= 0; j--) printf(" ");
}else{
for(int j = (1 << (heigh - count + 1)); j > 1; j--) printf(" ");
}
flag *= -1;
}
printf("\n");
count++;
}
}
void printByFirst(pNode father){
if(father != NULL){
printf("%c", father->value);
if(father->left != NULL){
printByFirst(father->left);
}
if(father->right != NULL){
printByFirst(father->right);
}
}
}
void printByCenter(pNode father){
if(father != NULL){
if(father->left != NULL){
printByFirst(father->left);
}
printf("%c", father->value);
if(father->right != NULL){
printByFirst(father->right);
}
}
}
void printByEnd(pNode father){
if(father != NULL){
if(father->left != NULL){
printByFirst(father->left);
}
if(father->right != NULL){
printByFirst(father->right);
}
printf("%c", father->value);
}
}
int depth(struct TreeNode *root)
{
if(root == NULL)
{
return 0;
}
int dep1=depth(root->left);
int dep2=depth(root->right);
return (dep1>dep2?dep1:dep2)+1;
}
int isBalanced(struct TreeNode* root){
if(root == NULL)
{
return 0;
}
int c = abs(depth(root->left) - depth(root->right));
printf("%d\t", depth(root->right)); printf("%d\t", depth(root->left));
printf("%d\n", c);
if(c >= 2)
{
return 1;
}
isBalanced(root->right);
isBalanced(root->left);
return 0;
}
//测试
int main() {
pNode node = createNode('A');
createByLeft(node);
printf("树的深度:%d\n", getHigh(node));
printf("先序输出:");
printByFirst(node);
printf("\n中序输出:");
printByCenter(node);
printf("\n后序输出:");
printByEnd(node);
printf("\n");
if(isBalanced(node) == 1) printf("false\n"); else printf("true\n");
destoryTree(node);
}
测试结果为
在isBalanceed函数中,可以看出c=2时,if(c>=2)是成立的,为什么没有return 1呢?
return之后的语句应该不会执行的吧。这里好像执行return 1后继续执行后续代码
图片中第三列是c的值。
希望各位指点。