二叉树

要求:

  1. 孩子兄弟链表存储二叉树
  2. 输入二叉树
  3. 凹入表法输出树
  4. 求树中叶子节点的个数
  5. 求树中最大度的节点(没写)
  6. 求树的深度
  7. 求根到某节点的路径

输入:

0
1
3
-1
-1
4
5
-1
-1
6
-1
-1
2
7
-1
-1
8
-1
-1

输出:

    0(root)
        1(left)
            3(left)
            4(right)
                5(left)
                6(right)
        2(right)
            7(left)
            8(right)
**********
leavesnumber:5
**********
depth:4
Please enter destination:
2
Path:
0
2

--------------------------------
Process exited with return value 0
Press any key to continue . . .

代码:

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 40
typedef struct TNode{
	int data;
	struct TNode *left,*right;
}TNode,*Tree;
TNode* createTree(){
	TNode* p;
	int data;
	printf("Please enter:\n");
	scanf("%d",&data);
	if(data==-1){
		p=NULL;
	}else{
		p=(TNode*)malloc(sizeof(TNode));
		p->data=data;
		p->left=createTree();
		p->right=createTree();
	}
	return p;
}
void DispTree(TNode* b)
{
	TNode* stack[MaxSize],* p;          //stack[top]为指针数组 
	int level[MaxSize][2], top, i, n, width = 4;
	//top为stack栈指针、levle[MaxSize][2]为二维数组,0维度-width(输出空格数)、1维度-不同节点
	char* type;
 
	if (b != NULL)
	{
		top = 1;                  //左节点之前输出(0)
		stack[top] = b;        //根节点入栈
		level[top][0] = width;
		level[top][1] = 2;     //2代表根节点
 
		while (top > 0)        //栈不为空时,循环继续 
		{
			p = stack[top];        //退栈并凹入显示该节点值
			n = level[top][0];
 
			switch (level[top][1])
			{
			case 0:
				type = "left";         //左节点
				break;
			case 1:
				type = "right";        //右节点
				break;
			case 2:
				type = "root";        //根节点
				break;
			}
 
			for (i = 1; i <= n; i++)           //n为输出的空格数,字符以右对齐显示 
				printf(" ");
			printf("%d(%s)\n",p->data,type);
			top--;                     //根节点出栈
 
			if (p->right != NULL)
			{
				top++;
				stack[top] = p->right;          //将右子树根节点入栈 
				level[top][0] = n + width;        //输出空格数增加width 
				level[top][1] = 1;              //1表示是右子树 
			}
			if (p->left != NULL)
			{
				top++;
				stack[top] = p->left;          //将左子树根节点入栈 
				level[top][0] = n + width;        //输出空格数增加width  
				level[top][1] = 0;              //0代表左子树 
			}
		}
	}
}
int getLeavesNum(TNode* p){
	if(p==NULL){
		return 0;
	}
	if(p->left==NULL && p->right==NULL){
		return 1;
	}
	int ln=getLeavesNum(p->left);
	int rn=getLeavesNum(p->right);
	return (ln+rn);
}
int getDepth(TNode* p){
	if(p==NULL) return 0;
	int ld=getDepth(p->left);
	int rd=getDepth(p->right);
	return (ld>rd? ld+1 : rd+1);	
}
int stack[MaxSize];
int top=0;
void getPath(TNode* p,int des){
	int flag=top-1;
	if(top>0&&stack[flag]==des){
		printf("**********\nPath:\n");
		for(int i=0;i<top;i++){
			printf("%d\n",stack[i]);
		}
		top=0;//起到了标志位的作用,不然输出2的时候会输出两遍 
		return ;
	} 
	if(p== NULL){
		return ;
	}
	stack[top++]=p->data;
	getPath(p->left,des);
	getPath(p->right,des);
	top--;
}
void printPath(TNode* p){
	int des=-1;
	printf("Please enter destination:\n");
	scanf("%d",&des);
	getPath(p,des);
}
int main(){
	TNode* root=createTree();
	DispTree(root);
	printf("**********\nleavesnumber:%d\n",getLeavesNum(root));
	printf("**********\ndepth:%d\n",getDepth(root));
	printPath(root);
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值