The concept of tree and the basic content of binary tree

1. The definition of tree and binary tree

1. Definition of tree

A tree is a finite set of n (n≥0) nodes. When n=0, it is called an empty tree;
for non-empty trees (n≠0):
(1) There is one and only one node called the root ;
(2) the rest of the nodes other than the root node can be divided into m (m> 0) disjoint finite sets T1, T2, ..., Tm, wherein each set is itself a tree, known as the root and Subtree

2. Basic terminology of tree

(1) The degree of the node : the number of subtrees owned by the node is called the degree of the node;
(2) The degree of the tree : the degree of the tree is the maximum value of the node degree in the tree;
(3) The leaf node: degree A node with 0 is also called a terminal node;
(4) Parents and children: The root of the child tree of the node is
called the child of the node, and correspondingly, the node is called the parent of the child;
(5) Level : The level of the node is defined from the root, the root is the first level, and the children of the root are the second level.
(6) The depth of the tree : the maximum level of the node in the tree is called the depth or height of the tree;

3. Definition of Binary Tree

A binary tree is a collection of n (n≥0) nodes. When n=0, it is called an empty tree;
for non-empty trees (n≠0):
(1) There is one and only one node called the root Point;
(2) The remaining nodes except the root node are divided into two disjoint subsets T1 and T2, which are called the left subtree and the right subtree of T, respectively. And T1 and T2 are both binary trees;
note: the difference between binary trees and trees:
binary trees are not a special case of trees, they are two different data structures;
(1) each node of a binary tree has at most two subtrees (ie in There is no node with a degree greater than 2)
(2) The subtrees of the binary tree are divided into left and right, and the order cannot be reversed.

Second, the nature of the binary tree

Property 1: At most 2^(i-1) nodes (i>=1) on the i-th level of the binary tree.
Property 2: The binary tree node with depth k has at most 2^k-1 nodes (k>=1).
Property 3: For any binary tree T, if the number of leaf nodes is n0 and the number of nodes with degree 2 is n2, then n0=n2+1; (the number of nodes with degree 0 is more than the number of nodes with degree 2 One more)
Promotion:Insert picture description here

Property 4: The depth of a complete binary tree with n nodes is: [logarithm of n based on 2]+1.
Property 5 If the nodes of a complete binary tree with n nodes are numbered in sequence (each level from left to right), then for any node i (1<=i<=n):
(1) If i=1, node i is the root of the binary tree and has no parents; if i>1, its parent PARENT(I) is node [i/2];
(2) If 2i>n, node i has no parents Left child (node ​​i is a leaf node); otherwise its left child LCHILD(I) is node 2i;
(3) If 2i+1>n, then node i has no right child; otherwise its right child RCHILD(i ) Is the node 2i+1.

Three, full binary tree and complete binary tree

1. Full binary tree: a binary tree with a depth of k and 2^k-1 nodes. The
shape is like: a
Insert picture description here
full binary tree: the
number of nodes on each layer is the largest number of nodes, that is, the node of each layer i All points have a maximum value of 2^(i-1). All leaf nodes are on the largest layer.

2. Complete binary tree: a binary tree with a depth of k and n nodes, if and only if the depth of each node corresponds to the node numbered from 1 to n in the full binary tree of depth k, it is called It is a complete binary tree. Its
shape is as follows:
Insert picture description here
the characteristics of a complete binary tree:
(1) Leaf nodes can only appear on the two largest levels;
(2) For any node, if the maximum level of descendants under its right branch is l, Then the maximum level of descendants under its left branch must be l or l+1.

Fourth, the storage structure of the binary tree

1. Sequential storage structure

#define MAXTSIZE 100
typedef TElemType SqBiTree[MAXTSIZE];
SqBiTree bt;

Insert picture description here
As can be seen from the above figure, there is a waste of storage space in the sequential storage structure, so the sequential storage structure is only suitable for complete binary trees. For general binary trees, it is more suitable to use chain storage structure
2. Chain storage structure

typedef struct BiTNode{
    
    
  TElemType data;         //数据域
  struct BiTNode *lchild,*rchild;  //左右子树指针
}BiTNode,*BiTree;

Insert picture description here

Five, create a binary tree (the order of pre-order traversal)

BiTree CreateBiTree()
{
    
    
	BiTree T;
	char ch;
	if ((ch = getchar()) == '#')T = NULL;
	else
	{
    
    
		T = (BiTNode*)malloc(sizeof(BiTNode));
		T->data = ch;
		T->lchild = CreateBiTree();
		T->rchild = CreateBiTree();
	}
	return T;
}

Guess you like

Origin blog.csdn.net/gets_s/article/details/105538460