002. An in-depth understanding of [Build a binary tree, traverse the tree in the middle and back order, and switch between left and right Subtrees]. 002
Binary Trees are defined as recursion. If you do not know about recursion well, we recommend that you read 001. A simple explanation [recursion].
Writing a recursive function is very easy. You only need to remember the following two points:
1. Recursive stop condition: for Binary trees, the leaf node is determined when node = null.
2. Recursive Function:; describes an intermediate process and implements it using code. When calling itself, the parameter passed is the method you want to recursion.
The following code is the process of creating a binary tree, traversing in the middle and back order, the depth of the tree, and interchange between left and right Subtrees.
# Include <stdio. h>
// Define the node of a binary tree
Struct treeNode {
Char data;
TreeNode * left; // point to the left child node
TreeNode * right; // point to the right child node
};
// Create a binary tree
TreeNode * creatBinaryTree (char a [], int pos, int length ){
TreeNode * node = NULL;
// Boundary Condition
If (pos> length | a [pos] = '0 '){
Return NULL;
}
Node = new treeNode;
Node-> data = a [pos];
// Recursive Creation
Node-> left = creatBinaryTree (a, 2 * pos, length );
Node-> right = creatBinaryTree (a, 2 * pos + 1, length );
Return node;
}
// Traverse binary trees in the forward order
VoidpreOrderScanBinary (treeNode * node ){
// Boundary condition (until the node does not exist)
If (node ){
// Print the root node
Printf ("% c", node-> data );
// Print left and right subtree recursively
PreOrderScanBinary (node-> left );
PreOrderScanBinary (node-> right );
}
}
// Sequential Traversal
VoidinOrderScanBinary (treeNode * node ){
If (node ){
InOrderScanBinary (node-> left );
Printf ("% c", node-> data );
InOrderScanBinary (node-> right );
}
}
// Post-order traversal
VoidpostOrderScanBinary (treeNode * node ){
If (node ){
PostOrderScanBinary (node-> left );
PostOrderScanBinary (node-> right );
Printf ("% c", node-> data );
}
}
// Calculate the depth of a binary tree
Int binaryDepth (treeNode * node ){
If (! Node)
Return 0;
Int nLeft = binaryDepth (node-> left); // depth of the left subtree
Int nRight = binaryDepth (node-> right); // depth with subtree
Return nLeft> nRight? (NLeft + 1): (nRight + 1); // tree depth = max (left subtree depth, right subtree depth) + 1
}
// Left/right subtree exchange
Void exchangeSubTree (treeNode * node ){
TreeNode * tempSubTree;
If (! Node ){
TempSubTree = node-> left;
Node-> left = node-> right;
Node-> right = tempSubTree;
ExchangeSubTree (node-> left );
ExchangeSubTree (node-> right );
}
}
Int main (int argc, const char * argv [])
{
Int length;
Printf ("Enter the length of the array and array elements :");
Scanf ("% d", & length );
Char * a = new char [length + 10];
For (int I = 1; I <= length; I ++ ){
Scanf ("% c", & a [I]);
}
// Create a binary tree
TreeNode * head = creatBinaryTree (a, 1, length );
// First-order traversal
Printf ("sequential traversal :");
PreOrderScanBinary (head );
// Sequential Traversal
Printf ("\ n sequential traversal :");
InOrderScanBinary (head );
// Post-order traversal
Printf ("\ n sequential traversal :");
PostOrderScanBinary (head );
// Tree depth
Printf ("\ n tree depth: % d \ n", binaryDepth (head ));
Return 0;
}
1. Create a binary tree and traverse the binary tree in ascending, central, and descending order. 2. The depth of the Binary Tree and the number of leaf nodes. 3. Create a Harman tree and a Harman tree.
0 indicates the number of initial nodes.
When entering the key, please input ABC certificate when DE certificate G certificate when F certificate then press ENTER. Do not ENTER one.
# Include "stdio. h"
# Include "string. h"
# Include "stdlib. h"
# Define Max 20 // maximum number of nodes
Typedef struct node {
Char data;
Struct node * lchild, * rchild;
} BinTNode; // The Node Type of the custom Binary Tree
Typedef BinTNode * BinTree; // defines the binary tree pointer.
Int NodeNum, leaf; // NodeNum indicates the number of nodes, and leaf indicates the number of leaves.
// ============= Create a binary tree based on the first-order traversal algorithm ==================
// ====== Enter the first sequence, and add the virtual node "#" to indicate the position of the NULL pointer ========
BinTree CreatBinTree (void)
{
BinTree T;
Char ch;
If (ch = getchar () = '')
Return (NULL); // read #, return a NULL pointer
Else {
T = (BinTNode *) malloc (sizeof (BinTNode); // generate a node
T-> data = ch;
T-> lchild = CreatBinTree (); // construct the left subtree
T-> rchild = CreatBinTree (); // construct the right subtree
Return (T );
}
}
// ========= NLR first-order traversal ====================
Void Preorder (BinTree T)
{
If (T ){
Printf ("% c", T-> data); // Access Node
Preorder (T-> lchild); // first traverse the left subtree
Preorder (T-> rchild); // first traverse the right subtree
}
}
// ========= LNR sequential traversal ============================
Void Inorder (BinTree T)
{
If (T ){
Inorder (T-> lchild); // traverse the left subtree in the middle order
Printf ("% c", T-> data); // Access Node
Inorder (T-> rchild); // traverse the right subtree in the middle order
}
}
// ============= LRN post-sequential traversal ====================
Void Postorder (BinTree T)
{
If (T ){
Postorder (T-> lchild); // traverse the left subtree in descending order
Postorder (T-> rchild); // traverse the right subtree in descending order
Printf ("% c", T-> data); // Access Node
}
}
/// ====== Use post-sequential traversal to obtain the depth of the binary tree, the number of knots, and the number of leaves
Write the first, middle, and back sequence of the following Binary Trees
Well, I have answered this question before.
Let's take a look.
Obviously, you do not understand how to traverse a binary tree.
When you get a binary tree, no matter how strange it is
We can divide it as follows:
Root
/\
Left subtree right subtree
A binary tree with multiple nodes can be divided into the above forms
It can also be understood that any combination of the above forms can be called a binary tree.
A binary tree with only root nodes can also be divided into the above forms, but its left and right subtree are empty.
Therefore, we found that binary tree definition is actually a process of recursive definition.
A big binary tree is built by a small binary tree.
So when we want to traverse a binary tree
It is also the first choice for Recursive traversal.
Traverse Binary Trees
Its basic idea is to first divide the entire binary tree into three parts according to the above form
Which of the following work is easy?
We only need to traverse the three parts (Here we use the divide and conquer idea)
For the three parts
Root Node traversal is undoubtedly the most convenient. It is okay to directly access it.
What about left and right subtree?
We can hardly find that the left and right subtree are actually two complete trees.
They have their own independent root nodes, left subtree and right subtree.
For their traversal, it is clear that they should be consistent with the previous Traversal method.
(If you have understood the above, this question is just a piece of cake. If you cannot understand it, you can break down several trees by yourself in the following method)
For this question, traverse the binary tree in the middle order
First look at the Root Node
1
/\
Left subtree right subtree
We should traverse the left subtree first.
That is, the tree below
2
/\
4 5
Traverse the tree in the middle order
We should first traverse her left subtree
He has only one root node 4, and left and right subtree are empty.
Which one can traverse a binary tree with only one root node?
Access her left subtree first, empty
Return
Access the root node 4 of the tree
The right subtree is also blank during access.
At this point, the tree has been completely traversed
We need to return the previous layer, that is
2
/\
4 5
This tree
At this time, her left subtree has been accessed
Rules that are traversed in the middle order
The root node 2 that needs to access this tree
The access order is 4-2.
Access to the root node
There is only one root node in the right subtree (For details, refer to access in step 4)
5. Access completed
That means
2
/\
4 5
This tree has been accessed
Back to the previous Layer
That is, 1 is the root tree.
Now the left subtree of this tree has been accessed
The access sequence is 4-2-5.
Next, access Root Node 1.
Access the right subtree
3
/\
4 7
Do you feel familiar ???
Her access should follow
2
/\
4 5
Consistent
Why is the final traversal order?
4-2-5-1-6-3-7
-----------------------------
It took over 10 minutes
Hope to help you
By the way, I will review it myself.
Haha... the remaining full text>