[Data structure] Detailed explanation of the principle of the binary tree algorithm + code + interview questions

Data structure and algorithm-binary tree

1. Basic concepts of binary tree

1. The concept of binary tree

Binary Tree (Binary Tree) is a finite set containing n nodes. The set is either an empty set (in this case, a binary tree is called an empty tree), or consists of a root node and two disjoint trees called root nodes. The left subtree and the right subtree consist of a binary tree.
1) There is one and only one specific node called Root.
2) When n>1, the remaining nodes can be divided into m (m>0) disjoint finite sets, where each set is itself a tree and is called a subtree of the root.
Insert picture description here
There are some other concepts:
1. Follow node: the top node of the tree.
2. Branch node: a node with at least one child node.
3. Degree: the number of subtrees a node has.
4. Edge: one node to another. the connection between the
5, path: the sequence of nodes and edges connecting nodes between nodes and their progeny
from the number of all nodes to the root node: 6, number of layers of nodes
depth 7, node: from the root The number of edges from the node to the node
8. The height of the node: The height of the node is the number of edges on the longest path that exists between the node and a certain leaf.
9. The height of the tree: the height of the root node

Insert picture description here

2. The nature of the binary tree:

(1) In a binary tree, there are at most 2 i − 1 2^{i−1} on the i- th layer2i 1 node (i≥1)
(2) A binary tree of depth k has at most2 k − 1 2^{k−1}2k 1 node (k≥1)
(3) For a binary tree, if the number of leaf nodes is n0andthe number of nodes withdegree 2 is n2, then n0=n2+1
(4) The depth of a complete binary tree with n nodes is ⌊log2n⌋+1

Insert picture description here

3. Two storage structures of binary trees

  1. Sequential storage

For a complete binary tree, a sequential storage structure can be used. However, for a general binary tree, there are two disadvantages when using a storage structure. First, if it is not a complete binary tree, it must be converted into a complete binary tree. Second, it adds a lot of virtual nodes and wastes resource space.

Insert picture description here

  1. Chain storage

This is the most commonly used binary tree storage structure. Each node sets three domains, namely the value domain, the left pointer domain and the right pointer domain. Data represents the value domain, and lchild and rchild respectively represent the pointer domains pointing to the left and right subtrees. as the picture shows.

Insert picture description here

4. Traversal of Binary Tree

In the operation of the binary tree, the traversal of the binary tree is the basic operation. For the traversal operation of the binary tree, it is mainly divided into:
pre-order traversal, middle-order traversal, post-order traversal, and hierarchical traversal.
In fact , the traversal of binary tree is a recursive process
. The recursive formula of traversal:
preOrder® = print r->preOrder(r->left)->preOrder(r->right)
The recursive formula of intermediate order traversal:
inOrder® = inOrder(r->left)->print
The recursive formula of r->inOrder(r->right) post-order traversal:
postOrder® = postOrder(r->left)->postOrder(r->right)->print r

1. Preorder traversal: about the root.
Idea: first visit the root, then traverse the left subtree, and then traverse the right subtree
ABDHIEJCFKG
Insert picture description here

2. Middle-order traversal: left root right.
Idea: first traverse the left subtree, then visit the root, and finally traverse the right subtree
HDIBEJAFKCG
Insert picture description here

3. Post-order traversal: left and right roots
Idea: first traverse the left subtree, then traverse the right subtree, and finally visit the root
HIDJEBKFGCA
Insert picture description here

4. Level traversal
Idea: From top to small, traverse
ABCDEFJHIJK from left to right
Insert picture description here

Two, the binary tree code

Binary tree implementation code

// An highlighted block

Three, binary tree interview questions

1. Find the number of nodes in the binary tree

// An highlighted block

2. Find the depth (height) of the binary tree

// An highlighted block

3. Find the number of leaf nodes in the binary tree

// An highlighted block

4. Knowing that the pre-order traversal and the middle-order traversal of a binary tree are ABDEGCFH and DBGEACHF respectively, what is the post-order traversal of the binary tree?

Given that the pre-order traversal and the middle-order traversal of a binary tree bai are ABDEGCFH and DBGEACHF, respectively, the zhi post-order traversal dao of the binary tree is DGEBHFCA.

The first node of the preorder traversal is the root node. From the preorder traversal, A is the root node. The nodes before the root node of the in-order traversal are all nodes of the left subtree, so the node on the left subtree is DBGE. Remove the root node and the left subtree node, and the right subtree node is CHF. The second node of the pre-order traversal is B. From 2 we know that B is the left subtree node, so B is the root node of the left subtree.

It is traversed by preorder, DEG is under node B, and traversed by middle order, D is the left node of B, and GE is the right node of B. Traversed by preorder, E is the root node of G, traversed by middle order, and G is the left child node of E. Traversed by the preorder, C is the right root node of the binary tree, traversed by the middle order, C does not contain the left child node, and HF is the right child node of C. Traverse by preorder, F is the root node of H, traverse by middle order, and H is the left child node of F.

In a binary tree, the traversal in post-order, left and right then root, that is, the left subtree is traversed first, then the right subtree is traversed, and the root node is finally visited. Then the post-order traversal of the binary tree is DGEBHFCA.

Insert picture description here

5. Given a binary tree, the node order of the preorder traversal is: ABDEGHCFI, the node order of the middle order traversal is: DBGEHAFCI, what is the order of the subsequent traversal?

Given a binary tree, the node order of the preorder traversal is: ABDEGHCFI, the node order of the middle order traversal is: DBGEHAFCI, and the order of the subsequent traversal is: DGHEBFICA

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_37882192/article/details/109228263