Learning data structure-Chapter 4: Trees and Binary Trees (basic concepts, basic terms, properties of trees)

Chapter 4: Trees and Binary Trees (Basic Concepts of Trees)

1. The basic concept of the tree

First of all, the tree is a logical structure.
**Tree: ** is n≥0a finite set of n( ) nodes. When n=0, it is called an empty tree. And any non-empty tree should satisfy:

  • 1) There is one and only one specific node called
  • 2) When n>1, the remaining nodes can be divided into m (m>0) disjoint finite sets, and each set is itself a tree, called the root node 子树.

Features: Except for the root node, each node has a predecessor node. Each node has zero or more successor nodes.

A tree with n nodes has only n-1 edges.

2. Basic terminology

2.1 The nodes of the tree

2.2 Degrees of the tree

Degree:子节点 the number of a node in the tree is called the node's

The degree of the tree: the maximum degree in the tree is called树的度

Degree of A: 4

2.3 The branch nodes and leaf nodes of the tree

Greater than 0 is called a node branch nodes (ABCDE)

0 degree is referred to as nodes leaf node (FGHIJKL)

2.4 The level, height and depth of the node

Level: some of the first level is also called the zeroth level

Insert picture description here

Node height : starting from the leaf node and accumulating layer by layer. For example, the height of node B is 3, and he has gone through the fourth layer, the third layer and the second layer respectively.

The depth of the node: It is accumulated layer by layer from the top to the bottom of the root node.

The height (depth) of the tree is the maximum number of nodes in the tree. The height and depth are the same.

2.5 Ordered Trees and Unordered Trees

Ordered tree
Each subtree is ordered from left to right.

Unordered tree
Every subtree from left to right is unordered . This kind of tree is called an unordered tree, also called a free tree

2.6 Path

Path The path between two nodes in the tree is made up of the path passed between these two nodes 结点序列.

The branches in the tree are directed, that is, from the parent node to the child node, so the path must be top-down, which means that the E node cannot reach the F node, and there is no path between them.
For example: the path from A to E is: ABE

Path length : the number of passes on the path .

For example: the path length from A to E is: 2

2.7 Forest

Forest : m(m>=0) a collection of disjoint trees.

3. The nature of the tree

1) The number of tree species nodes is equal to the degree of all nodes (not counting root nodes) plus 1

2) The tree species with degree m ihas at most m^(i-1) nodes (i>=1) on the first layer


3) The m-ary tree with height h has at most (m^h-1)/(m-1) nodes

This is the summation of the geometric sequence, at most, the number of sub-nodes of each node is m, the common ratio of this geometric sequence is m, and the first term is 1, according to the geometric sequence 1-m is negative, 1- q^n is also a negative number, here the formula is directly exchanged, and the result remains unchanged.


4) The minimum height of an m-ary tree with n nodes is logm(n(m-1)+1) rounded down.

This is the inference of the third property. The first is the minimum height, which is 尽量to arrange the nodes on a smaller layer, that is, the number of sub-nodes of each node is as much as possible m, set (m^h-1) /(m-1)=n, just find m, as for the result is rounded down, because this result is not necessarily an integer, because our condition here is the minimum height, which is the number of nodes per layer , Except for the last layer, all reach the maximum number, then this last layer may not meet the maximum number of nodes, then the result of calculation using this formula will appear decimals, but the last layer also has nodes and it is also considered a layer , So round down.

The knowledge about data structure is being updated continuously, welcome to pay attention to the official account 理木客.

Guess you like

Origin blog.csdn.net/qq_41941875/article/details/106342319