Learning data structure (eight)-tree

0. Definition of tree

N is a finite set of tree nodes, and only a specific node called the root, when n>1, the remaining nodes can be divided into m disjoint finite sets, wherein the set itself is a tree, And is called the subtree of the tree. The root node of the tree has no predecessor nodes, all nodes except the root node have and only one predecessor node, and all nodes in the tree can have zero or more trailing nodes. The node in the tree contains a data element and several branches pointing to its subtree. The number of subtrees owned by a node is called the degree of the node. A node with a degree of 0 is called a leaf or a terminal node, and the degree is not 0. The nodes in the tree are called non-terminal nodes or branch nodes. The maximum level of nodes in the tree is called the depth of the tree. The height of leaf nodes is 1, and the height of non-leaf nodes is equal to the height of child nodes plus 1. The height is equal to the depth of the root. The number of branches from one node to another in the tree constitutes the length between nodes called the path, the path length refers to the number of branches on the path, and the path length of the tree is from the root of the tree to each node The sum of the paths. A forest is a collection of m disjoint trees.

Common representations of trees include nested set representation, recessed table representation and generalized table representation.

1. Binary tree

The characteristic of the binary tree is that each node has at most two subtrees, and the subtrees are divided into left and right.

There is at most 2^{i-1}one node on the i-th level of the binary tree, and the binary tree with depth k has at most 2^{k-1}one node. For any non-empty binary tree, if its terminal node is n_{0}, the number of nodes with degree 2 is n_{2}, then n_{0}=n_{2}+1; The depth of the complete binary tree with n nodes is log_{2}(n+1).

A 2^{k-1}binary tree with a depth of k and one node is called a full binary tree. If a full binary tree is numbered continuously from top to bottom, and the same level from left to right, then a binary tree with n nodes in depth k, if and only When each node has a one-to-one correspondence with the nodes numbered from 1 to n in the full binary tree with depth k, it is called a complete binary tree. A full binary tree must be a full binary tree, and a full binary tree is not necessarily a full binary tree. In a complete binary tree, if a node does not have a left child, it must have no right child, and the node must be a leaf node.

(1) Storage method

There are two storage methods, namely sequential storage and chain storage.

<1>Sequential storage

The binary tree is a non-linear structure. The sequential storage of the binary tree is generally stored sequentially from top to bottom and from left to right with consecutive addresses.

<2>Chain storage

Two pointer fields are defined to point to the root nodes of the left and right subtrees respectively. Because it is difficult to find the parent node, an additional pointer to the parent is defined.

(2) Traversal of binary tree

On a given node, you can perform the following operations: (1) visit the node itself (2) traverse the left subtree (3) traverse the right subtree

Use recursive traversal, then use pre-order, middle-order, and post-order traversal to traverse; in non-recursive traversal, use stack to traverse.

(3) Storage representation

(1) Parent pointer representation: an indicator parent of data and parent nodes. (2) Child linked list representation: multiple linked lists are used.

The process of transforming forests into trees is as follows:

 

Guess you like

Origin blog.csdn.net/qq_35789421/article/details/113792341