📄 tree.c
字号:
#include "myhdr.h"
struct Node{
int data;
struct Node *lchild;
struct Node *rchild;
};
typedef struct Node BitNode;
typedef BitNode *BitTree;
BitTree CreateBitTree();
void PreOrderTraverse(BitTree root);
int CountLeaf(BitTree root);
int CountNode(BitTree root);
int
main()
{
BitTree root;
int Count_Leaf;
int Count_Node;
root=CreateBitTree();
if(root==NULL)
{
return 0;
}
PreOrderTraverse(root);
Count_Leaf=CountLeaf(root);
printf("the leaf of the tree is %d\n",Count_Leaf);
Count_Node=CountNode(root);
printf("the node of the tree is %d\n",Count_Node);
return (0);
}
BitTree
CreateBitTree()
{
int node_data;
BitTree root1 = NULL;
printf("input a node please\n");
scanf("%d",&node_data);
if(node_data==0)
{
root1=NULL;
}
else
{
if( (root1 =(BitTree)malloc(sizeof(BitNode)))==NULL)
{
printf("Allocated node Error\n");
return NULL;
}
root1->data=node_data;
printf("the node is %d\n",root1->data);
root1->lchild = CreateBitTree();
root1->rchild = CreateBitTree();
}
return root1;
}
void
PreOrderTraverse(BitTree root)
{
if(root!=NULL)
{
printf("-------------------\n");
/*********** PreOrderTraverse(root->lchild);***/
printf("%d\n",root->data);
PreOrderTraverse(root->lchild);
PreOrderTraverse(root->rchild);
}
}
int
CountLeaf(BitTree root)
{
static int Count_Leaf;
if(root!=NULL)
{
if((root->lchild)==NULL && (root->rchild)==NULL)
{
Count_Leaf++;
}
CountLeaf(root->lchild);
CountLeaf(root->rchild);
}
return Count_Leaf;
}
int
CountNode(BitTree root)
{
static int Count_Node;
if(root!=NULL)
{
Count_Node++;
CountNode(root->lchild);
CountNode(root->rchild);
}
return Count_Node;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -