The nature and structure of the binary tree storage

First, the nature of binary tree

  1. The i-th layer in the binary tree has at most i-1 th power node 2(I> = 1), the i-th layerAt least one nodeHere Insert Picture Description
  2. A binary tree of depth k at most 2 ^ k -1 nodes (k> = 1)When the depth of kAt least one node kHere Insert Picture Description
  3. Any of a binary tree T, if it points to the leaf node n0, the number of nodes of degree 2 is n2, then n0 = n2 + 1.

prove: Degree of set of nodes n1 is 1, thenSummary pointsforn = n0+n1+n2.
Number of branches of the binary tree view: in addition to the root, the remaining node has a branch entry B is set to the total number of branches, from the bottom up, B = n1, from the top down, B = n1 + 2 * n2. after the merger,n = n1 + 2 * n2 + 1, And n = n0 + n1 + n2, son0 = n2 + 1.This analysis is the key, try to master.
.Here Insert Picture Description

inBefore understanding the nature and properties 4 5 (complete binary tree is an important feature)We first lookFull Binary TreewithComplete binary tree, Both of them in a binary treeThe sequential storage mode can be restored
Full Binary TreeAnd it has a depth of k 2 ^ k - 1 nodes called Binary obtain a full binary tree. Characterized by: each of nodes is the maximum number of nodes (Each layer is full),Full leaf nodes at the lowest level
Here Insert Picture Description
Complete binary tree Depth is k, there are n nodes in the binary tree, if and only if each of which nodes are associated with a full binary tree of depth k correspond numbered from node 1 ~ n, calledComplete binary tree.
Its characteristics are:
. A) leaf nodes may only appear in a maximum of two levels
b) for any node, if its right subtreeThe maximum levelAs i, then its left subtreeThe maximum levelWill either i or i + 1.
Here Insert Picture Description

  1. Complete binary tree of n nodes having a depthHere Insert Picture DescriptionHere Insert Picture DescriptionConversely denotes the smallest integer not less than x.Here Insert Picture Description
  2. If there is a complete binary tree node n nodes by a layer

Sequence number, then for any node i (i> = 1 && i <= n), there are:
Here Insert Picture Descriptionthe nature when applied to sequential storageEasy to find the parents and offspring node i

Second, the binary tree storage structure

Here Insert Picture Description

Sequentially storing the binary tree (array)

Implementation: PressFull Binary TreeNode level number,Sequentially storedData elements in the binary tree.
It stores a binary sequence

#define MAXTSIZE 100		//二叉树最大结点数
typedef TElemType SqBiTree[MAXTSIZE]//0号单元存储根结点
SqBiTree bt;

But the order only applies to storage or full binary tree complete binary tree, becauseAt depth k k nodes only worst case,butRequired length of 2 ^ k -
a 1-dimensional array
,meetingCausing great waste of storage space, So for a general binary tree, it is more suitable Storage Structure

Chain store binary tree

Binary list storage structure represented

Here Insert Picture Description


typedef struct BiNode{
	TElemType data;
	struct BiNode *lchild,*rchild;//左右孩子指针
}BiNode,*BiTree;	//*BiTree是指向下一节点的一个指针,BiNode是普通结点类型;

The n binary linked list of nodes, theren+1Null pointer field
analysis: n-node pointers certainly 2n domain (domain chain), except the root node, and each node has only one parent, so will only have pointer field n 1-th node the number of square memory pointer field pointer, the pointer that is stored as n-1, a total of 2n nodes, the number of null pointer field = 2n - (n-1) = n + 1 th

Trigeminal list storage structure represented by (three pointer fields)

In order to facilitate finding the parent node, the pointer is incremented just a parent domain, called the trigeminal list

Here Insert Picture Description

typedef struct TriNode{
	TelemType data;
	struct TriTNode *lchild,*parent,*rchild;
}TriTNode,*TriTree;	
Published 34 original articles · won praise 85 · views 4613

Guess you like

Origin blog.csdn.net/weixin_45895026/article/details/104041021