主题:[讨论]查找二叉树某个结点所在的层次
查找二叉树某个结点所在的层次
事先建立好一个先序的字符二叉树,然后从键盘输入一个字符,在二叉树中查找该字符所在的层次,如果有返回层次数,否则返回0
例如:输入abdg h cei fj
再输入 b 则输出“该字符在第2层”
以下是我的代码:但不知问题出在哪里
#include <stdio.h>
#include <stdlib.h>
typedef struct node{char data;struct node *left,*right;}NodeTp;
#define CreateBtNode(p) p = (NodeTp*)malloc(sizeof(NodeTp));
#define DeleteBtNode(p) free((void*)(p));
NodeTp* CrtTree();
int SearchNode(char,NodeTp*);
int GetDepth(NodeTp*);
void Print(NodeTp*);
NodeTp* CrtTree()
{char data;
NodeTp *root;
data = getchar();
//scanf("%d",&data);
if(data == 32)return NULL;
CreateBtNode(root);root->data = data;
root->left = CrtTree();
root->right = CrtTree();
return root;
}
int GetDepth(NodeTp*root)
{int d1,d2;
if(!root)return 0;
d1 = GetDepth(root->left);
d2 = GetDepth(root->right);
return(d1>d2?d1:d2)+1;
}
int SearchNode(char data , NodeTp*root)
{
int i,j;
if(!root||(root&&(root->data != data)&&(!root->left)&&(!root->right)) )return 0;
if(root->data != data)
{i = SearchNode(data,root->left);
j = SearchNode(data,root->right);
}
return (i>j?i:j)+1;
}
void Print(NodeTp*root)
{if(!root)return;
//putchar(root->data);
printf("%2c",root->data);
Print(root->left);
Print(root->right);
}
int main()
{NodeTp *root;
char data,ch;
int d,k;
printf("请输入不同字符建立二叉树:\n");
root = CrtTree();
d = GetDepth(root);
printf("二叉树的深度为%d:\n",d);
while(1)
{ printf("请输入要查找的字符:\n");
getchar();
data = getchar();
k = SearchNode(data,root);
printf("这个字符在二叉树的第%d层\n",k);
Print(root);
printf("是否继续:\n");
getchar();
ch = getchar();
if(ch != 'y'&&ch !='Y')break;}
return 0;}
事先建立好一个先序的字符二叉树,然后从键盘输入一个字符,在二叉树中查找该字符所在的层次,如果有返回层次数,否则返回0
例如:输入abdg h cei fj
再输入 b 则输出“该字符在第2层”
以下是我的代码:但不知问题出在哪里
#include <stdio.h>
#include <stdlib.h>
typedef struct node{char data;struct node *left,*right;}NodeTp;
#define CreateBtNode(p) p = (NodeTp*)malloc(sizeof(NodeTp));
#define DeleteBtNode(p) free((void*)(p));
NodeTp* CrtTree();
int SearchNode(char,NodeTp*);
int GetDepth(NodeTp*);
void Print(NodeTp*);
NodeTp* CrtTree()
{char data;
NodeTp *root;
data = getchar();
//scanf("%d",&data);
if(data == 32)return NULL;
CreateBtNode(root);root->data = data;
root->left = CrtTree();
root->right = CrtTree();
return root;
}
int GetDepth(NodeTp*root)
{int d1,d2;
if(!root)return 0;
d1 = GetDepth(root->left);
d2 = GetDepth(root->right);
return(d1>d2?d1:d2)+1;
}
int SearchNode(char data , NodeTp*root)
{
int i,j;
if(!root||(root&&(root->data != data)&&(!root->left)&&(!root->right)) )return 0;
if(root->data != data)
{i = SearchNode(data,root->left);
j = SearchNode(data,root->right);
}
return (i>j?i:j)+1;
}
void Print(NodeTp*root)
{if(!root)return;
//putchar(root->data);
printf("%2c",root->data);
Print(root->left);
Print(root->right);
}
int main()
{NodeTp *root;
char data,ch;
int d,k;
printf("请输入不同字符建立二叉树:\n");
root = CrtTree();
d = GetDepth(root);
printf("二叉树的深度为%d:\n",d);
while(1)
{ printf("请输入要查找的字符:\n");
getchar();
data = getchar();
k = SearchNode(data,root);
printf("这个字符在二叉树的第%d层\n",k);
Print(root);
printf("是否继续:\n");
getchar();
ch = getchar();
if(ch != 'y'&&ch !='Y')break;}
return 0;}