Comparison of the difference between tree and binary tree, perfect/full binary tree and complete binary tree

Let’s first talk about trees and binary trees:

1. Different in nature

Tree: A tree is a data structure that can have multiple subtrees.

Binary tree: Binary tree is a tree structure with at most two subtrees per node.

2. Different nodes
Tree: Each node of the tree has zero or more child nodes; the node without a parent node is called the root node; each non-root node has and only one parent node.

Binary tree: Each node has at most two subtrees.

Three, different types

Trees: The types of trees include disordered trees, ordered trees, binary trees, and Huffman trees.

Binary trees: The types of binary trees include complete binary trees, full binary trees and balanced binary trees.

Perfect/full binary tree and complete binary tree:

The difference between full binary tree and complete binary tree:

The complete binary zhi tree is derived from the full binary tree. For a dao binary tree with a depth of K and n nodes, if and only if each node corresponds to the node numbered from 1 to n in the full binary tree of depth K, it is called a complete binary tree. .

For a full binary tree, all nodes on each layer have two child nodes except for the last layer without any child nodes. The complete binary tree is a very efficient data structure, and the complete binary tree is derived from the full binary tree. For a binary tree with a depth of K and n nodes, if and only if each node corresponds to a node numbered from 1 to n in the full binary tree with a depth of K, it is called a complete binary tree.

1. Full binary tree

Definition: A binary tree. If the number of nodes in each layer reaches the maximum value, the binary tree is a full binary tree. In other words, 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.

2. Complete Binary Tree

Definition: If the depth of the binary tree is h, except for the h-th layer, the number of nodes in the other layers (1~h-1) reaches the maximum number, and all the nodes of the h-th layer are continuously concentrated on the leftmost side, this It is a complete binary tree.

A full binary tree means that each node has two child nodes, and each level is full, while a perfect binary tree does not require each level to be slow, as long as each node has two child nodes. A complete binary tree means that except for the bottom layer, the other layers have two child nodes under the node, and the child nodes of the bottom layer are concentrated on the left.

Guess you like

Origin blog.csdn.net/weixin_37081112/article/details/108472876