On the binary tree

Binary Tree

Binary Tree is not a special case of the tree, although the trees have many similarities , but the tree and binary tree with two main differences:

1. the tree node is not the maximum degree of restriction, and the binary maximum node degree is 2 ;

2. No tree node left and right points, while the binary tree node only the left and right of the points .

 

The following figure is the typical is a binary tree! ! !

Then we analyze this chart:

In this "tree" tree

Root ------ F

Leaf nodes ------ A, B, H, M

Child node of the root node, among other ----- F,

Father junction ----- addition to other nodes leaf nodes

Sibling ----- [C, E] [A, D] [H, G]

Node cousin ----- [B, M] [A, H] [D, G]

----- depth of the tree depth of 4

...... like this, there are many technical terms, listed here are several commonly used.

 

 Then we look at some form of binary tree:

 

A ------ an empty binary tree. (Remember: nothing, it can be a binary tree is just a tree empty)

B ------ binary tree has a node. (This is both a root node, but also leaf node)

C ------ only the left subtree.

D ------ Only the right subtree.

E ------ complete binary tree. (Here is only a special kind of complete binary tree binary tree)

So this binary tree can be defined:

struct Binary_tree // construct binary 
{ 
	int a Date; // node value 
	struct Binary_tree * lchild; // left subtree 
	struct Binary_tree * rchild; // right subtree (where the left and right subtrees can be seen as a binary tree) 
} * Bitree;

Introduce special binary tree:

1、完全二叉树:

 

定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。

一棵二叉树至多只有最下面的两层上的结点的度数可以小于2,并且最下层上的结点都集中在该层最左边的若干位置上,则此二叉树成为完全二叉树,并且最下层上的结点都集中在该层最左边的若干位置上,而在最后一层上,右边的若干结点缺失的二叉树,则此二叉树成为完全二叉树。

2、满二叉树:

定义:除最后一层无任何子节点外,每一层上的所有结点都有两个子结点二叉树。

一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是(2^k) -1 ,则它就是满二叉树。(这里是国内的说法,国外的说法不同)

3、平衡二叉树:

 

 定义:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

平衡二叉树或者是一棵空树,或者是具有以下性质的二叉排序树:(1)它的左子树和右子树的高度之差绝对值不超过1;(2)它的左子树和右子树都是平衡二叉树。

 以上三种为特殊的二叉树,都十分的特别,都具有一些性质。(这里就不详细的讲解了)

 

最后来讲一下二叉树的遍历:

遍历是对树的一种最基本的运算,所谓遍历二叉树,就是按一定的规则和顺序走遍二叉树的所有结点,使每一个结点都被访问一次,而且只被访问一次。

由于二叉树是非线性结构,因此,树的遍历实质上是将二叉树的各个结点转换成为一个线性序列来表示。 

遍历规则:
⑴访问结点本身(N),
⑵遍历该结点的左子树(L),
⑶遍历该结点的右子树(R)。
 
遍历方法:

一、 NLR:前序遍历

(Preorder Traversal 亦称(先序遍历))

——访问根结点的操作发生在遍历其左右子树之前。

⑴ 访问根(N)结点;
⑵ 遍历左(L)子树;
⑶ 遍历右(R)子树。
void NLR_PreTraversal ( Binary_tree *tree ) //先序遍历 
{
	if ( tree->Data == NULL )
	{
		return;
	}
	cout << tree->Data ;
	NLR_PreTraversal ( tree->lchild ) ;
	NLR_PreTraversal ( tree->rchild ) ;
}

二、 LNR:中序遍历

(Inorder Traversal)

——访问根结点的操作发生在遍历其左右子树之中(间)。

⑴遍历左(L)子树;
⑵访问根(N)结点;
⑶遍历右(R)子树。
void LRN_InoTraversal ( Binary_tree *tree ) //中序遍历 
{
	if ( tree->Data == NULL )
	{
		return;
	}
	LRN_InoTraversal ( tree->lchild ) ;
	cout << tree->Data ;
	LRN_InoTraversal ( tree->rchild ) ;
}

三、 LRN:后序遍历

(Postorder Traversal)

——访问根结点的操作发生在遍历其左右子树之后。

⑴遍历左(L)子树;
⑵遍历右(R)子树;
⑶访问根(N)结点。
void LRN_PosTraversal ( Binary_tree *tree ) //后序遍历 
{
	if ( tree->Data == NULL )
	{
		return;
	}
	LRN_PosTraversal ( tree->lchild ) ;
	LRN_PosTraversal ( tree->rchild ) ;
	cout << tree->Data ;
}

注:还有层序遍历,其实就只BFS(宽度优先搜索),这里就不单独提出来了。

 

 

以上即为我总结的二叉树基础内容。

Guess you like

Origin www.cnblogs.com/yhz-Albert/p/11234605.html