[Data Structure] Concept and structure of trees and binary trees (1)

Table of contents

1. The concept and structure of tree

        1. Definition of tree

        2. Classification and relationships of tree nodes

        3. Representation of tree

2. Concept and structure of binary tree

        1. The definition of binary tree

        2. Special binary tree

        3. Properties of binary trees

        4. Storage structure of binary tree

1. Sequential storage

2. Chain storage


1. The concept and structure of tree

        1. Definition of tree

Tree is a non-linear data structure, which is a set of hierarchical relationships composed of n (n>=0) limited nodes. It's called a tree because it looks like an upside-down tree, which means it has the roots pointing up and the leaves pointing down.

A tree is a finite set of n (n>=0) nodes;

When n=0, it is called an empty tree;

In any non-empty tree:

1. There is and is only one specific node called root;

2. When n>1, the remaining nodes can be divided into m (m>0) disjoint finite sets T1, T2,..., Tm, each of which is itself a tree. , and is called the subtree (SubTree);

As shown below:

Note: In the tree structure, there cannot be intersection between subtrees , otherwise it will not be a tree structure.

        2. Classification and relationships of tree nodes

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: A is 4

Leaf node or terminal node: A node with degree 0 is called a leaf node; as shown in the figure above: K, G, L, M, N... and other nodes are leaf nodes.

Non-terminal node or branch node: a node with a degree other than 0; as shown in the figure above: nodes such as B, C, E... are branch nodes

Parent node or parent node: If a node contains child nodes, this node is called the parent node of its child node; As shown in the figure above: A is the parent node of B

Child node or child node: The root node of the subtree contained by a node is called the child node of the node; As shown in the figure above: B is the child node of A

Brother nodes: Nodes with the same parent node are called brother nodes; as shown in the figure above: B and C are brother nodes

Degree of tree: 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 4

The level of the node: starting from the definition of the root, the root is the first level, the child nodes of the root are the second level, and so on.

The height or depth of the tree: the maximum level of nodes in the tree; as shown above: the height of the tree is 4

Cousin nodes: Nodes whose parents are on the same level are cousins ​​of each other; as shown in the picture above: H and I are cousin nodes of each other.

Ancestors of a node: all nodes on the branches from the root to the node; as shown in the figure above: A is the ancestor of all nodes, C is the ancestor of G and H

Descendants: Any node in the subtree rooted at a node is called a descendant of that node. As shown in the picture above: all nodes are descendants of A, G and H are descendants of C

Forest: A collection of m (m>0) disjoint trees is called a forest.

        3. Representation of tree

        The tree structure is more complicated than the linear table , and it is more troublesome to store and represent it. Since the value range is saved, the relationship between the nodes and the nodes must also be saved . In fact, there are many ways to represent trees, such as: parent representation. , child representation , child parent representation, child brother representation, etc. Here we will briefly understand the most commonly used child brother representations.

typedef int DataType;
struct Node
{
 struct Node* _firstChild1; // 第一个孩子结点
 struct Node* _pNextBrother; // 指向其下一个兄弟结点
 DataType _data; // 结点中的数据域
};

Illustration:

2. Concept and structure of binary tree

        1. The definition of binary tree

Binary Tree is a finite set of n (n>=0) nodes;

This set is either an empty set (called an empty binary tree);

Or it consists of a root node and two mutually disjoint binary trees, namely the left subtree and the right subtree of the root node;

 Illustration:

As can be seen from the above figure:

1. There is no node with degree greater than 2 in a binary tree.

2. The subtrees of a binary tree can be divided into left and right subtrees, and the order cannot be reversed, so the binary tree is an ordered tree.

Binary trees have the following five basic forms:

        2. Special binary tree

1. leaning tree

A binary tree in which all nodes have only left subtrees is called a left-skew tree;

A binary tree in which all nodes have only the right subtree is called a right-skew tree;

Slanted tree icon:

2. Full binary tree

A binary tree is a full binary tree if the number of nodes in each layer reaches the maximum. That is to say, if the number of levels of a binary tree is K and the total number of nodes is 2^k-1, then it is a full binary tree

3. 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 depth K and n nodes, it is called a complete binary tree if and only if each node corresponds one-to-one with the nodes numbered from 1 to n in the full binary tree with depth K. It should be noted that a full binary tree is a special kind of complete binary tree.

        3. Properties of binary trees

1. If the number of levels of the root node is 1, then there are at most 2^(i-1) nodes on the i-th level of a non-empty binary tree.

2. If the number of levels of the root node is specified to be 1, then the maximum number of nodes of a binary tree with depth 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 n1, then n0=n1+1

4. If the number of levels of the root node is specified to be 1, the depth of a full binary tree with n nodes, h=log2(n+1) 

5. For a complete binary tree with n nodes, if all nodes are numbered starting from 0 in the array order from top to bottom, left to right, then for the node with serial number i :

1. If i>0, the parent number of the node at position i: (i-1)/2; i=0, i is the root node number, and there is no parent node.

2. If 2i+1<n, left child number: 2i+1

3. If 2i+1>n, the right child number: 2i+2

        

        4. Storage structure of binary tree

Binary trees can generally be stored in two structures, a sequential structure and a chain structure.

1. Sequential storage

Sequential structure storage is to use arrays for storage. Generally, arrays are only suitable for representing complete binary trees , because if they are not complete binary trees, there will be a waste of space. In reality, only heaps use arrays to store

The sequential storage of binary trees is physically an array and logically a binary tree.

2. Chain storage

The linked storage structure of a binary tree refers to using a linked list to represent a binary tree, that is, using a chain to indicate the logical relationship of elements;

The usual method is that each node in the linked list consists of three fields, the data field and the left and right pointer fields. The left and right pointers are used to give the storage addresses of the link points where the left child and right child of the node are located respectively;

The chain structure is divided into two-pronged chain and three-pronged chain.

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; // 当前节点值域
};

The first stage is here. Let us first understand the principles of trees and binary trees;

The blogger will update it one after another later;

If there are any deficiencies, please feel free to supplement and communicate!

end. . .


Guess you like

Origin blog.csdn.net/m0_71676870/article/details/132650448