数据结构 树与二叉树

一.树

结点的数量=边数+1;

二.二叉树

1.每个结点的度最多为22.度为0的结点比度为2的结点多1个

结点的个数 = 边的个数 + 1;

N0 + N1 + N2 = N1 + 2 * N2 + 1;

N0 = N2 + 1;

3.

前序遍历 根 左 右

中序遍历 左 根 右

后序遍历 左 右 根(逆波兰表达式容易计算)

4.删除度为2的结点转化为删除度为0的结点;

#include <stdio.h>
#include <stdlib.h>

typedef struct Node{//定义的是二叉树的一个结点;
	int data;
	struct Node *lchild, *rchild;
}Node,*Tree;//给一个别名Node,还要给一个别名Tree为结构体的指针;Tree指向一棵树,Node指向一个结点;

Node *init(int val){//传入一个整形值,返回一个包含整形值的结点;
	Node *p = (Node *)malloc(sizeof(Node) * 1);
	p->data = val;
	p->lchild = p->rchild = NULL;
	return p;
}

int pre_order(Tree tree){
	if(tree == NULL) return 0;
	printf("%d ", tree->data);
	tree->lchild && pre_order(tree->lchild);//没有用if,与运算少一次压栈;
	tree->rchild && pre_order(tree->rchild);
	return 1;
}

int in_order(Tree tree){
	if(tree == NULL) return 0;
	tree->lchild && in_order(tree->lchild);
	printf("%d ", tree->data);
	tree->rchild && in_order(tree->rchild);
	return 1;
}

int post_order(Tree tree){
	if(tree == NULL) return 0;
	tree->lchild && post_order(tree->lchild);
	tree->rchild && post_order(tree->rchild);
	printf("%d ", tree->data);
	return 1;
}

void clear(Tree tree){//将tree清空,不能传入一个指针Node,Tree指向的是Node初始结点;
	if(tree == NULL) return ;
	clear(tree->lchild);
	clear(tree->rchild);
	free(tree);
	return ;
}//用后序遍历销毁;

int main(){
	Tree tree = init(1);
	tree->lchild = init(2);
	tree->rchild = init(3);
	tree->lchild->rchild = init(4);
	tree->rchild->lchild = init(5);
	pre_order(tree), printf("\n");
	in_order(tree), printf("\n");
	post_order(tree), printf("\n");
	clear(tree);
	return 0;
}
输出结果:(前中后序遍历)
1 2 4 3 5 
2 4 1 5 3 
4 2 5 3 1 
完全二叉树[complete binary tree]

堆的基础

除了最后一层,其余层都是满二叉树;最后一层只能是右侧缺失

1.编号为i的子结点:左孩子编号2i;右孩子编号2 * i + 1(从1开始编号) ***[方便]

编号为i的子结点:左孩子编号2i + 1;右孩子编号2 * i + 2(从0开始编号)

2.可用连续空间存储(数组),由于编号连续[通过计算得到孩子结点的编号,节省存储空间];

3.字典树改成双数组字典数,是将边的关系由记录式改成计算式;

满二叉树(中国版)[perfect binary tree]

所有层都是满的;

满二叉树(国际版)[full binary tree]

不是度为0的结点,就是度为2的结点;

二叉树----guangyibiao

() 空树

A/A() 一个字母表示一个结点

A(B,)/A(B) 表示A只有一个左孩子B

A(B,C) 表示A的左孩子为B,右孩子为C


猜你喜欢

转载自blog.csdn.net/qq_38362049/article/details/81044276