Beginners binary tree summary
First, the nature of binary tree
- 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 node
- A binary tree of depth k at most 2 ^ k -1 nodes (k> = 1)When the depth of kAt least one node k
- 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.
.
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
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.
- Complete binary tree of n nodes having a depthConversely denotes the smallest integer not less than x.
- 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:
the nature when applied to sequential storageEasy to find the parents and offspring node i。
Second, the binary tree storage structure
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
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
typedef struct TriNode{
TelemType data;
struct TriTNode *lchild,*parent,*rchild;
}TriTNode,*TriTree;