Class notes: the logical structure of the binary tree

A binary tree is a finite set of n (n≥0) nodes. The set is either an empty set (called an empty binary tree), or a root node and two disjoint trees, each called the left of the root node. The subtree and the binary tree of the right subtree are composed.
The characteristics of the binary tree :
1. Each node has at most two subtrees;
2. The binary tree is ordered, and its order cannot be reversed arbitrarily.
Note : Binary tree and tree are two kinds of tree structure.
The basic form of a binary tree: an empty binary tree, only one root node, the root node has only the right subtree, the root node has only the left subtree, and the root node has both left and right subtrees.
Special binary tree
1. Oblique tree
(1) A binary tree whose all nodes have only left subtrees is called a left oblique tree;
(2) A binary tree whose all nodes have only right subtrees is called a right oblique tree;
(3) Left oblique tree The tree and right oblique tree are collectively called oblique tree.
Characteristics of oblique trees :
(1) In oblique trees, there is only one node at each level;
(2) The number of oblique trees is the same as its depth.
2. Full binary tree
In a binary tree, if all branch nodes have left and right subtrees, and all leaves are on the same level.
The characteristics of the full binary tree :
(1) The leaves can only appear at the bottom layer;
(2) Only the nodes with degree 0 and degree 2 are available.
The full binary tree has the most nodes in the binary tree of the same depth.
A full binary tree has the largest number of leaf nodes in a binary tree of the same depth.
3. Complete binary tree
A binary tree with n nodes is numbered in a hierarchical order. If the node with the number i (1≤i≤n) is the same as the node with the number i in the full binary tree of the same depth in the binary tree.
In a full binary tree, starting from the last node, removing any number of nodes in succession is a complete binary tree.
The characteristics of a complete binary tree
(1) The leaf nodes can only appear in the bottom two layers, and the leaf nodes of the bottom layer are concentrated on the left of the binary tree;
(2) If there are nodes with degree 1 in the complete binary tree, only There may be one, and the node has only the left child.
(3) A complete binary tree with depth k must be a full binary tree on the k-1 layer.
Basic Properties
of Binary Trees Properties 5-1 Binary trees have at most 2i-1 nodes (i≥1) on the i-th layer.
Properties 5-2 A binary tree of depth k has at most 2k-1 nodes and at least k nodes.
Note : A binary tree with a depth of k and 2k-1 nodes must be a full binary tree, and a binary tree with a depth of k and k nodes is not necessarily a slanted tree.
Property 5-3 In a binary tree, if the number of leaf nodes is n0 and the number of nodes with degree 2 is n2, then: n0 = n2 + 1.
In a full binary tree with n nodes, there are n0 = (n + 1) / 2 leaf nodes.
Basic Properties of Complete Binary Trees
Properties 5-4 The depth of a complete binary tree with n nodes is log2n (rounded down) +1.
Properties 5-5. For a complete binary tree with n nodes, the numbering starts from 1 and then for any node with sequence number i (1≤i≤n) (referred to as node i for short), there is :
(1) If i> 1, the serial number of the parent node of node i is i / 2; if i = 1, then node i is the root node, and there is no parent node.
(2) If 2i ≤ n, the serial number of the left child of node i is 2i; if 2i> n, then node i has no left child.
(3) If 2i + 1≤n, the serial number of the right child of node i is 2i + 1; if 2i + 1> n, the node i has no right child.
For a complete binary tree with n nodes, the numbering starts from 1 and the parent node of node i is i / 2; the left child of node i is 2i; the right child of node i is 2i + 1 .
Property 5 shows that in a complete binary tree, the sequence number of the node reflects the logical relationship between the nodes.
Binary tree traversal operation
Binary tree traversal refers to starting from the root node and accessing all nodes in the binary tree in a certain order, so that each node is visited once and only once.
The purpose of binary tree traversal operation : linearization of nonlinear structure.
1. Preorder (root) traversal
If the binary tree is empty, the null operation returns; otherwise:
① visit the root node;
② preorder traverse the left subtree of the root node;
③ preorder traverse the right subtree of the root node.
2. Middle order (root) traversal
If the binary tree is empty, the null operation returns; otherwise:
① middle order traverses the left subtree of the
root node; ② visits the root node;
③ middle order traverses the right subtree of the root node.
3. Postorder (root) traversal
If the binary tree is empty, the null operation returns; otherwise:
①Postorder traverses the left subtree of the root node;
②Postorder traverses the right subtree of the root node;
③ Visit the root node.
4, sequence traversing
the binary tree hierarchy traversal refers binary tree starting from a first layer (i.e. the root), traversed from top to bottom layer by layer, in the same layer, press left to right by one of the nodes access.
If the pre-order sequence and mid-order sequence of a binary tree are known, the binary tree can be uniquely determined.

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/103018854