[Data structure] The concept and structure of trees and binary trees

 

1. Tree concept and structure

1.1 The concept of a tree

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

  • There is a special node called the root node , and the root node has no predecessor nodes.
  • Except the root node, the other nodes are divided into M (M>0) disjoint sets T1, T2, ..., Tm , each of which Ti (1<= i <= m) is a structure and Tree-like subtrees. The root node of each subtree has one and only one predecessor, and can have zero or more successors.
  • Therefore, the tree is defined recursively .

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

1.2 Related concepts of trees

Degree of node: The number of subtrees contained in a node is called the degree of the node; as shown in the figure above: A is 6

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

Non-terminal nodes or branch nodes: nodes whose degree is not 0; as shown in the figure above: nodes such as D, E, F, G... are branch nodes

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

Child node or child node: the root node of the subtree contained in a node is called the child node of the node; as shown 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 the tree: In a tree, the degree of the largest node is called the degree of the tree; as shown above: the degree of the tree is 6

The level of nodes: 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;

Tree height or depth: 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 layer are cousins; as shown in the figure above: H and I are sibling nodes

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

Descendants: Any node in the subtree rooted at a node is called a descendant of the node. As shown above: all nodes are descendants of A

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

1.3 Tree Representation

The tree structure is more complicated than the linear table, and it is more troublesome to store and express. Since the value range is saved, the relationship between nodes and nodes is also saved. In practice, there are many ways to represent trees, such as: parent representation , child representation, child parent representation, and child sibling representation. Here we simply understand the most commonly used child brother notation.

Child- sibling representation of a tree, also known as left-child-right-sibling representation, is a method for representing tree structures. In this notation, each node contains two pointers: one to its first child, and one to its next sibling .

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

 

1.4 The use of trees in practice (representing the directory tree structure of the file system)

 

2. Concept and structure of binary tree

2.1 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

 As can be seen from the figure above:

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

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

Note: For any binary tree, it is composed of the following situations:

 

2.2 Special Binary Trees

1. Full binary tree: a binary tree, if the number of nodes in each layer reaches the maximum value, then this binary tree is a full binary tree . That is to say, if the number of layers of a binary tree is K and the total number of nodes is, 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, it is called a complete binary tree if and only if each node has a one-to-one correspondence with the nodes numbered from 1 to n in the full binary tree with a depth of K. It should be noted that a full binary tree is a special kind of complete binary tree.

 

2.3 Properties of Binary Trees

 

1. 某二叉树共有 399 个结点,其中有 199 个度为 2 的结点,则该二叉树中的叶子结点数为( )
A 不存在这样的二叉树
B 200
C 198
D 199

 Analysis: A leaf node is a node with a degree of 0, according to the above property 3:

 So the leaf node=199+1=200;

Answer: B

2.在具有 2n 个结点的完全二叉树中,叶子结点个数为( )
A n
B n+1
C n-1
D n/2

Parse:

Answer: B

3.一棵完全二叉树的节点数位为531个,那么这棵树的高度为( )
A 11
B 10
C 8
D 12

 Parse:

 Answer: B

4.一个具有767个节点的完全二叉树,其叶子节点个数为()
A 383
B 384
C 385
D 386

 Parse: 

  Answer: B

2.4 Storage structure of 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 for storage . Generally, arrays are only suitable for representing complete binary trees, because not complete binary trees will waste space. In reality, only the heap will use arrays for storage. Binary tree sequential storage is physically a number and logically a binary tree.

 2. Chain storage

The linked storage structure of the binary tree means: 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 method is that each node in the linked list is composed of three fields, the data field and the left and right pointer fields, and 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. The chain structure is further divided into binary chains and triple chains. At present, we generally study binary chains, and high-level data structures such as red-black trees will use triple chains.

 

 

Guess you like

Origin blog.csdn.net/m0_73648729/article/details/132259164