数据结构之二叉树

树的相关术语
  1. 节点:数的分支或者边缘的结合部
  2. 叶子节点:树的最边缘部分,没有分支的节点
  3. 分之结点:数的分支结合部
  4. 结点的度:从结点出发的分支个数
  5. 树的宽度/度:所有树中结点的度的最大值
  6. 树的高度/深度:距离根节点最远的叶子节点与根节点之间间隔的节点个数。
树的性质
  1. 有且仅有一个称为root的跟结点
  2. 其他结点都是不相交的结点子集
  3. 跟结点没有前驱
  4. 叶子结点没有直接后续

2.树的简化形式 ==》二叉树
性质:除过必须有树本身的性质外,还有如下特性。
二叉树必须有左右子树之分:

定义:
1、有限集合
2、互不相交
3、左右之分

五种性质

  1. 二叉树第i层最多有2^(i-1)个结点
  2. 二叉树的高度为k,则最多有2^k -1 个节点
  3. 满二叉树 高度为k的树有2^k -1 个节点。除过叶子节点以外,其他节点都有左右孩子
  4. 完全二叉树将一棵二叉树按照满二叉树方式从根节点依次向下编码,如果最后的编码连续,则称为完全二叉树,否则不是完全二叉树
树的基本结构
typedef int  datatype;
typedef struct _node_
	{
		struct _node_ * lchild;
		datatype        data;
		struct _node_ * rchild;
	}bitree;
//创建二叉树 ===>递归算法
	bitree *create_node(datatype va)
	{
		bitree *n = malloc(sizeof(bitree));
		n->data = va;
		n->lchild = n->rchild = NULL;
		return n;
	}
///递归方式建立满二叉树

	bitree * bitree_create(int size , datatype value)
	{
		bitree * t = create_node(value);
		if(2* value <= size)
		{
			t->lchild = bitree_create(size,2*value );
		}
		if(2 * value +1 <= size)
		{
			t->rchild = bitree_create(size,2*value+1);
		}
		retrun t;
	}
树的前中后遍历
//先序遍历
	 int pro_order(bitree * root)
	 {
		 printf("%d ",root->data);    //中

		 if(root->lchild)
		 {
			 pro_order(root->lchild);  //左
	     }
		 if(root->rchild)
		 {
			pro_order(root->rchild);   //右
		 }
		 return 0;
	 }
```c
//中序序遍历
	 int pro_order(bitree * root)
	 {
		 if(root->lchild)
		 {
			 pro_order(root->lchild);  //左
	     }	 
	     printf("%d ",root->data);    //中
		 if(root->rchild)
		 {
			pro_order(root->rchild);   //右
		 }
		 return 0;
	 }
```//后序遍历
	 int pro_order(bitree * root)
	 {
		 if(root->lchild)
		 {
			 pro_order(root->lchild);  //左
	     }
		 if(root->rchild)
		 {
			pro_order(root->rchild);   //右
		 }
		  printf("%d ",root->data);    //中
		 return 0;
	 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值