Trees and Binary Tree Heaps: Implementation of Chained Binary Trees

Table of contents

Implementation of chained binary tree:

Prerequisites:

prologue:

Mid-order:

Afterword:

Construction of chained binary tree: 

Define the structure:

Initialization: 

The pointers to construct the left and right subtrees point to:

Implementation of preorder traversal: 

Implementation of in-order traversal: 

Implementation of post-order traversal: 

 Find the number of nodes in a binary tree:

Writing method 1: 

Writing method 2: 

Find the number of leaf nodes of the tree: 

Find the height of the tree: 

Find the Kth layer node: 

Implementation of chained binary tree:

Prerequisites:

The implementation of chained binary trees mainly serves those non-full binary trees and non-complete binary trees that cannot be stored in arrays. In these binary trees, we split their structures into root, left subtree, and right subtree. .

The left subtree and right subtree can be divided into their root, left subtree, and right subtree according to this structure.​ 

prologue:

 It is a traversal method that divides and traverses the structure of a binary tree in the order of root, left subtree, and right subtree.

As shown in the figure: N means that the node is NULL

Expressed numerically: 1 2 3 N N N 4 5 N N 6 N N

Mid-order:

 It is a traversal method that divides and traverses the structure of a binary tree in the order of left subtree, root, and right subtree.

As shown in the figure: N means that the node is NULL

Expressed numerically as N 3 N 2 N 1 N 5 N 4 N 6 N

Afterword:

 is a traversal method that divides and traverses the structure of a binary tree in order of left subtree, right subtree, and root.

As shown in the figure: N means that the node is NULL

 Expressed numerically as N N 3 N 2 N N 5 N N 6 4 1

Construction of chained binary tree: 

The root of all evil~recursion!​ 

Define the structure:

A linked binary tree requires data to store node elements and two pointers pointing to the left subtree and the right subtree respectively.

Initialization: 

 Create space to build nodes:

Remember to initialize the left and right subtree pointers of the constructed node and assign values ​​to the node elements.​ 

The pointers to construct the left and right subtrees point to:

The constructed node is shown in the binary tree structure in the figure above.

Implementation of preorder traversal: 

The idea of ​​​​preorder traversal is the root, left subtree, and right subtree, so you need to print the root first, and then go to the left subtree of the root, and the left subtree of the root can be traversed in preorder, so the idea of ​​recursion is used , first make continuous calls to reach the leftmost node, then recurse step by step, and then move to the right subtree to perform the same operation, that is, call into the left subtree, and then recurse step by step... 

Binary tree trend diagram 

Code calling idea diagram 

Implementation of in-order traversal: 

The idea of ​​in-order traversal is the left subtree, root, and right subtree, so when printing, the left subtree is printed first, then the root node, and finally the right subtree. Therefore, using the idea of ​​recursion, we want to traverse to The leftmost node is then printed, then enters the right subtree, and then traverses the left subtree, and so on...

 Binary tree trend diagram

Code idea call diagram

Implementation of post-order traversal: 

Postorder means that the order of pointing to the right subtree and the printing node subtree can be interchanged. 

 Find the number of nodes in a binary tree:

Use recursive thinking to resolve the problem into the number of nodes in the left subtree + the number of nodes in the right subtree + the number of nodes in the root node.

  • The number of nodes in the left and right subtrees can be divided into the number of nodes in the left subtree + the number of nodes in the right subtree + the number of nodes in the root node.
  • Therefore, this question can be changed to ++size if the current node is not NULL, and return if it is NULL.
  • And return in recursion just jumps out of the process this time, returns to the last called process and enters the next code.
Writing method 1: 

Illustration

Writing method 2: 

 Writing method 2 completely simplifies writing method 1, using the ternary operator, completely turning the ++size problem into the accumulation of 1.

If root = NULL, return 0, if not equal to NULL, make a call to enter the recursive call of the left subtree and the recursive call of the right subtree.

Find the number of leaf nodes of the tree: 

Leaf node concept:Tree and binary tree heap: Tree-CSDN Blog 

The core of the problem can be split into the number of leaf nodes of the tree = the number of leaf nodes of the left cotyledon + the number of leaf nodes of the right cotyledon. 

This question is actually an advanced version of the previous question. Essentially, there are two more conditions. That is, when the left and right subtree pointers of the node point to NULL, 1 is returned, indicating that the node is a leaf node, not a leaf node. Then enter the call, enter the left and right subtrees of the node respectively to make a recursive call, until root == NULL or a leaf node, perform return, return to the previous node and start returning step by step...

Find the height of the tree: 

 This problem can be transformed into finding that the height of the tree is the largest of the left subtree and the right subtree + 1. This 1 is the root node, which can be turned into an accumulation problem of 1.

That is, root == NULL is returned step by step, and +1 is used to achieve the accumulation of 1.

  1. As shown in the above code, after entering the leftmost node, root == NULL is the return condition.
  2. When the leftheight of the left subtree of the leftmost node returns 0 because root == NULL, after the end, it returns to the previous node (the leftmost node) and enters the right subtree of the node (the leftmost node).
  3. Then the right subtree returns with root == NULL and enters the height comparison of the two subtrees.
  4. Because NULL, the two return values ​​here are 0 + 1 and 0 + 1 for comparison, so 1 is finally returned to the previous node of this node (the leftmost node) (the parent node of the leftmost node)

Comparison using special functions 

Find the Kth layer node: 

 Assume that if the root node is to find the Kth level, then the root nodes of the left and right subtrees of the root node are k-1 levels

  • And because the condition root == NULL means that the tree is empty, so 0 is returned, and k == 1 means that the tree has only one root node, so there is only one node.
  • And because the tree can split the left and right subtrees and roots, and the left and right subtrees can continue to be split, and the number of nodes in the Kth layer is found, so the K==1 condition can be used to return and perform back-algebra values. .​ 
  • Therefore, you can use k>1 and k-1 to continuously call to the Kth layer, and then back-substitute the return value.

Note that the return value is brought back rather than accumulated.

Idea map 


Guess you like

Origin blog.csdn.net/2301_76445610/article/details/134697082