|
在linux下.运行时,"请问根结点是否有左子树?(Y/N):"和"请问根结点是否有右子树?(Y/N):"会同时出现,前面的根本没有输入.为什么?难道一个程序中不能出现两三个getchar或scanf??
- #include <stdio.h>
- typedef struct btnode
- {
- int data;
- struct btnode *lchild;
- struct btnode *rchild;
- } bitreptr;
- bitreptr* create(bitreptr* root);
- bitreptr* createHead()
- {
- bitreptr *root;
- int input;
- char choose;
- root = (bitreptr *) malloc (sizeof(bitreptr));
- root -> lchild = NULL;
- root -> rchild = NULL;
- printf("输入根结点的数据:\t\t");
- scanf("%d", &input);
- root -> data = input;
- printf("请问根结点是否有左子树?(Y/N):\t");
- fflush(stdin);
- choose=getchar();
- if(choose=='Y' || choose=='y')
- root->lchild = create(root->lchild);
- printf("请问根结点是否有右子树?(Y/N):\t");
- fflush(stdin);
- choose=getchar();
- if(choose=='Y' || choose=='y')
- root -> rchild = create(root->rchild);
- return root;
- }
- bitreptr* create(bitreptr* root)
- {
- int input;
- char choose;
- root = (bitreptr *) malloc (sizeof(bitreptr));
- root -> lchild = NULL;
- root -> rchild = NULL;
- printf("输入当前结点内的数据:\t\t");
- scanf("%d", &input);
- root -> data = input;
- printf("请问当前结点是否有左子树?(Y/N):\t");
- fflush(stdin);
- choose=getchar();
- if(choose=='Y' || choose=='y')
- root->lchild = create(root->lchild);
- printf("请问当前结点是否有右子树?(Y/N):\t");
- fflush(stdin);
- choose=getchar();
- if(choose=='Y' || choose=='y')
- root -> rchild = create(root->rchild);
- return (root);
- }
- void outputTree(bitreptr* pbnode,int totalSpace)
- {int i;
- if(pbnode!=NULL)
- {
- totalSpace+=5;
- outputTree(pbnode->rchild,totalSpace);
- for(i=0;i<totalSpace;i++) printf(" ");
- printf("%d\n",pbnode->data);
- outputTree(pbnode->lchild,totalSpace);
- }
- }
- void preorder (bitreptr *root)
- {
- if (root == NULL)
- return;
- printf("%d ", root -> data);
- preorder(root -> lchild);
- preorder(root -> rchild);
- }
- void inorder (bitreptr *root)
- {
- if (root == NULL)
- return;
- inorder(root -> lchild);
- printf("%d ", root -> data);
- inorder(root -> rchild);
- }
- void postorder (bitreptr *root)
- {
- if (root == NULL)
- return;
- postorder(root -> lchild);
- postorder(root -> rchild);
- printf("%d ", root -> data);
- }
- int max(int a, int b)
- {
- if(a>=b) return a;
- else return b;
- }
- int depth_bintree (bitreptr *root)
- {
- int depth, depthl, depthr;
- if (root == NULL)
- depth = 0;
- else
- {
- depthl = depth_bintree(root -> lchild);
- depthr = depth_bintree(root -> rchild);
- depth = (max(depthl, depthr) + 1);
- }
- return (depth);
- }
- int countleaf (bitreptr *root)
- {
- static int count=0;
- if ((root -> lchild == NULL) && (root -> rchild == NULL))
- count++;
- else if((root -> lchild != NULL) && (root -> rchild == NULL))
- countleaf(root -> lchild);
- else if((root -> lchild == NULL) && (root -> rchild != NULL))
- countleaf(root -> rchild);
- else if((root -> lchild != NULL) && (root -> rchild != NULL))
- {
- countleaf(root->lchild);
- countleaf(root->rchild);
- }
- return (count);
- }
- int countall(bitreptr *root)
- {
- static int count=0;
- if((root -> lchild == NULL) && (root -> rchild == NULL))
- count++;
- else if((root -> lchild != NULL) && (root -> rchild == NULL))
- {
- count++;
- countall(root -> lchild);
- }
- else if((root -> lchild == NULL) && (root -> rchild != NULL))
- {
- count++;
- countall(root -> rchild);
- }
- else if((root -> lchild != NULL) && (root -> rchild != NULL))
- {
- count++;
- countall(root->lchild);
- countall(root->rchild);
- }
- return count;
- }
- int main ()
- {
- bitreptr *root;
- int leaves,depth;
- int allnodes;
- int totalSpace=0;
- root = createHead();
- printf("我们构造的这棵二叉树的直观图为:\n");
- outputTree(root,totalSpace);
- printf("\n先根遍历:\n");
- preorder(root);
- printf("\n中根遍历:\n");
- inorder(root);
- printf("\n后根遍历:\n");
- postorder(root);
- depth = depth_bintree(root);
- printf("\n树的深度为:\t%d",depth);
- leaves = countleaf(root);
- printf("\n叶子总数为:\t%d", leaves);
- allnodes =countall(root);
- printf("\n总的结点数目为:\t%d\n",allnodes);
- }
复制代码 |
|