[Data structure] Binary tree concept

Table of contents

Binary tree concept::

                        1. The concept and structure of the tree

                        2. Tree Representation

                        3. The application of trees in practice

                        4. The concept and structure of binary tree

                        5. Full binary tree and complete binary tree

                        6. Properties of Binary Trees

                        7. Storage structure of binary tree

Binary tree concept::

1. The concept and structure of the tree

The concept of a tree:

A tree is a nonlinear data structure, which is a set of n (n>=0) finite nodes with finite relationships. It is called a tree because it looks like an upside-down tree, that is, with its roots facing upwards and its leaves facing downwards.

1. There is a special node called the root node, and the root node has no predecessor nodes.
2. Except the root node, the rest is divided into M (M>0) disjoint sets T1, T2...Tm, where Each set Ti (1<=i<=m) is a subtree similar in structure to a tree. The root node of each subtree has one and only one predecessor, and can have zero or more successors, so the tree is defined recursively.


Note: In the tree structure, there can be no intersection between subtrees, otherwise it is not a tree structure. 

 

Related concepts of trees: 

1. Degree of a node: the number of subtrees contained in a node is called the degree of the node, as shown in the figure above: the degree of A is 6
2. Leaf node or terminal node: a node with a degree of 0 is called a leaf node, as shown in the figure above : Nodes such as B, C, H, I... are leaf nodes
3. Non-terminal nodes or branch nodes: nodes with degree not 0, as shown in the above figure, nodes such as D, E, F, G... are branch nodes
4 .Parent node: If a node contains child nodes, this node is called the parent node of its child nodes, as shown in the figure above: A is the parent node of B
5. Child node: A node containing the root node of the subtree is called the node
6. Brother nodes: Nodes with the same parent node are called sibling nodes, as shown in the figure above: B is a child node
of A 7. Tree degree: In a tree, The degree of the largest node is called the degree of the tree, as shown in the figure above: the degree of the tree is 6
8. The hierarchy of nodes: starting from the definition of the root, the root is the first layer, and the child nodes of the root are the second layer, and so
on9 .Tree height or depth: the maximum level of nodes in the tree, as shown in the figure above, the height of the tree is 4
10. Cousin nodes: nodes whose parents are on the same level are cousins, as shown in the figure above: H and I are cousin nodes
11. Ancestors of a node: all nodes on the branch from the root to the node, as shown in the figure above: A is the ancestor of all nodes
12. Descendants: any node in the subtree rooted at a node is called the node’s Descendants, as shown in the figure above: All nodes are descendants of A
13. Forest: A collection of m (m>0) disjoint trees is called a forest, such as a union search is a forest of trees

Tree representation:

//静态顺序表存储孩子节点指针
//明确告诉树的度为N
#define N 5
struct TreeNode
{
	int da/ta;
	struct TreeNode* childArr[N];
	int childSize;
};
//动态顺序表存储孩子节点指针 不用定义宏
struct TreeNode
{
	int data;
	struct TreeNode** childArr;
	int childSize;
	int childCapacity;
};

Compared with the linear table, the tree structure is more complicated, and it is more troublesome to store and express. It is necessary to save the value range and the relationship between nodes. In practice, there are many ways to express the tree, such as: parent representation , child notation, child parent notation, and child sibling notation, etc. Here we simply understand the most commonly used child brother notation .

//左孩子右兄弟表示法
//父亲指向左边第一个孩子 孩子之间用兄弟指针链接起来
typedef int DataType;
struct Node
{
	struct Node* firstChild1;//左边第一个孩子节点
	struct Node* pNextBrother;//指向下一个兄弟节点
	DataType data;
};

Parent representation:
store the subscript of the father, its advantage is that it is convenient to find the ancestors of any node, and this data structure is used for querying 

The application of the tree in practice: (represents the directory tree structure of the file system)

2. Concept and structure of binary tree

concept:

A binary tree is a finite set of nodes that:

1. or empty

2. It consists of a root node plus two binary trees called left subtree and right subtree

It can be seen from the above figure:
    1. There is no node with a degree greater than 2 in a binary tree
    2. The subtrees of a binary tree are divided into left and right, and the order cannot be reversed, so a binary tree is an ordered tree
    Note: For any binary tree, it consists of the following types Combination of situations:

 Special binary tree:
1. Full binary tree: a binary tree, if the number of nodes in each layer reaches the maximum value, then the binary tree is a full binary tree, that is, if the number of layers of a binary tree is K, and the total number of nodes is 2 ^K - 1, then it is a full binary tree.
2. Complete binary tree: A complete binary tree is a very efficient data structure, and a complete binary tree is derived from a full binary tree. For a binary tree with a depth of K and n nodes, when And only when each node corresponds to the nodes numbered from 1 to n in the full binary tree with a depth of K, it is called a complete binary tree. It should be noted that a full binary tree is a special complete binary tree.        Note: The first k-1 layers of the complete binary tree are full, and the last layer is full or dissatisfied, but it is required to be continuous
from left to right.
1] (the last layer is at least one and at most full) 

Properties of binary trees:

1. If the number of layers of the root node is specified as 1 , then there are at most 2^(i-1) nodes on the i-th layer of a non-empty binary tree .
2. If the number of layers of the root node is specified as 1 , then the maximum number of nodes in a binary tree with a depth of h is 2^h-1.
3. For any binary tree , if the degree is 0 , the number of leaf nodes is n0 , and the number of branch nodes with  degree 2 is n2 , then there is
n0=n2+ 1.
4. If the number of layers of the root node is specified as 1 , the depth of a full binary tree with n nodes , h=log(n+1)
. (ps : log(n+1) is log base 2, n+1 is logarithm ).
5. For a complete binary tree with n nodes, if all nodes are numbered from 0 in the order of the array from top to bottom and from left to right, then for the node with the serial number i :
1. If i>0 , the parent number of the node at i position: (i-1)/2 ; i=0 , i is the root node number, no parent node
2. If 2i+1<n , the left child number: 2i+1 , 2i+1>=n otherwise there is no left child
3. If 2i+2<n , the serial number of the right child: 2i+2 , 2i+2>=n , otherwise there is no right child
1. 某二叉树共有 399 个结点,其中有 199 个度为 2 的结点,则该二叉树中的叶子结点数为( )
A 不存在这样的二叉树
B 200
C 198
D 199
2.下列数据结构中,不适合采用顺序存储结构的是( )
A 非完全二叉树
B 堆
C 队列
D 栈
3.在具有 2n 个结点的完全二叉树中,叶子结点个数为( )
A n
B n+1
C n-1
D n/2
4.一棵完全二叉树的节点数位为531个,那么这棵树的高度为( )
A 11
B 10
C 8
D 12
5.一个具有767个节点的完全二叉树,其叶子节点个数为()
A 383
B 384
C 385
D 386
答案:
1.B
2.A
3.A
4.B
5.B

3. Storage structure of binary tree

The storage structure of the binary tree:

Binary trees can generally be stored using two structures: a sequential structure and a chain structure.

1. Sequential storage:

Sequential structure storage is to use arrays to store . Generally, arrays are only suitable for representing complete binary trees , because not complete binary trees will have empty space.
waste of time. In reality, only the heap will be stored in an array, and we will specifically explain the heap in the following chapters. Binary tree sequential storage is physically an array and logically a binary tree.

 Chain storage:

The linked storage structure of the binary tree means that a linked list is used to represent a binary tree, that is, a link is used to indicate the logical relationship of elements. The usual way is
Each node in the linked list is composed of three fields, the data field and the left and right pointer fields. The left and right pointers are used to give the left and right children of the node respectively.
The storage address of the link point at . The chain structure is divided into two-fork chains and three-fork chains. Currently, we are generally studying two-fork chains. Later courses
After learning high-level data structures such as red-black trees, ternary chains will be used. 
typedef int BTDataType;
// 二叉链
struct BinaryTreeNode
{
 struct BinTreeNode* _pLeft; // 指向当前节点左孩子
 struct BinTreeNode* _pRight; // 指向当前节点右孩子
 BTDataType _data; // 当前节点值域
}
// 三叉链
struct BinaryTreeNode
{
 struct BinTreeNode* _pParent; // 指向当前节点的双亲
 struct BinTreeNode* _pLeft; // 指向当前节点左孩子
 struct BinTreeNode* _pRight; // 指向当前节点右孩子
 BTDataType _data; // 当前节点值域
};

Guess you like

Origin blog.csdn.net/qq_66767938/article/details/129720041