Binary tree study notes using preorder traversal to recursively create a binary tree

tree traversal

Tree traversal refers to starting from the root node and visiting all nodes in the tree in a certain order, so that each node is visited once and only once.

preorder traversal

If the tree is empty, the null operation returns; otherwise
1. Visit the root node
2. Traverse each subtree of the root node in order from left to right.
Preorder traversal of a binary tree
If the tree is empty, the null operation returns; otherwise
1. Visit the root node
2. Preorder traversal of the left subtree of the root node
2. Preorder traversal of the right subtree of the root node
The preorder traversal is Refers to visiting all nodes in the binary tree in the order of the root node first, then the left node, and finally the right node, so that each node is visited and only visited once.

Create a binary tree using preorder traversal

Define the node data structure of the tree using the binary linked list representation (that is, the tree's child brother representation)

template<class DataType>
struct TNode
{
	DataType data; //树结点元素
	TNode* left;  //左子树指针
	TNode* right; //右子树指针
}

data is the data field, which stores the data information of the node;
left is the left pointer field, which stores the pointer of the left child of the node, and is a null pointer when the left child does not exist;
right is the right pointer field, which stores the right child of the node A pointer to , or a null pointer if the right child does not exist.

BiTreeNode* CreatBiTree(char* s, int &i, int len)
// 利用先序遍历创建二叉树
// 参数:先序遍历字符串s,字符串初始下标i=0,字符串长度len。
// 返回:二叉树
{
    BiTreeNode* root;
    char item = s[i++];
    if(i>=len || item=='#')//当字符串下标上溢或者对应的位置为空时,将结点指针指向空
    {
        root=NULL;
    }
    else
    {
        root = new BiTreeNode(item);
        root->left = CreatBiTree(s, i, len);
        root->right = CreatBiTree(s, i, len);
    }
    return root;
}

or

BiTreeNode* CreatBiTree(char* s,int &i,int len)
// 利用先序遍历创建二叉树
// 参数:先序遍历字符串s,字符串初始下标i=0,字符串长度len。
// 返回:二叉树
{
    BiTreeNode* root;
    if(s[i]=='#'||len==0||i==len){
        return NULL;
    }
    else{
        root = new BiTreeNode(s[i++]);
        root->left = CreatBiTree(s,i,len);
        root->right = CreatBiTree(s,++i,len);
    }
    return root;
}

Inorder traversal visits all nodes

void InOrder(BiTreeNode* root)
// 二叉树的中序遍历
// 参数:二叉树根结点root
{
    if(root){       //如果root !=NULL的话执行后续操作
        InOrder(root->left);
        printf("%c",root->data);
        InOrder(root->right);
    }
}

Preorder traversal visits all nodes

void PreOrder(BitreeNode* root)
//二叉树的前序遍历
//参数:二叉树根结点root
{
	if(root)
	{
		printf("%c",root->data);
		PreOrder(root->left);
		PreOrder(root->right);
	}

Postorder traversal visits all nodes

void PostOrder(BitreeNode* root)
//二叉树的后序遍历
//参数:二叉树根结点root
{
	if(root)
	{
		PreOrder(root->left);
		PreOrder(root->right);
		printf("%c",root->data);
	}

example

#include <iostream>
#include <cstdio>          //注
#include <cstring>
#include <algorithm>      //注

using namespace std;

struct BiTreeNode {
    char data;              //  树结点元素
    BiTreeNode* left;       //  左子树指针
    BiTreeNode* right;      //  右子树指针
    BiTreeNode(){           //  树结点初始化
        left=NULL;
        right=NULL;
    }
    BiTreeNode(char item){  //  用元素初始化树结点
        data=item;
        left=NULL;
        right=NULL;
    }
    ~BiTreeNode(){          //  释放树结点内存
        left=NULL;
        right=NULL;
    }
};
BiTreeNode* CreatBiTree(char* s, int &i, int len)
// 利用先序遍历创建二叉树
// 参数:先序遍历字符串s,字符串初始下标i=0,字符串长度len。
// 返回:二叉树
{
    BiTreeNode* root;
    char item = s[i++];
    if(i>=len || item=='#')
    {
        root=NULL;
    }
    else
    {
        root = new BiTreeNode(item);
        root->left = CreatBiTree(s, i, len);
        root->right = CreatBiTree(s, i, len);
    }
    return root;
}
/*
BiTreeNode* CreatBiTree(char* s,int &i,int len)
// 利用先序遍历创建二叉树
// 参数:先序遍历字符串s,字符串初始下标i=0,字符串长度len。
// 返回:二叉树
{
    BiTreeNode* root;
    if(s[i]=='#'||len==0||i==len){
        return NULL;
    }
    else{
        root = new BiTreeNode(s[i++]);
        root->left = CreatBiTree(s,i,len);
        root->right = CreatBiTree(s,++i,len);
    }
    return root;
}
*/
void InOrder(BiTreeNode* root)
// 二叉树的中序遍历
// 参数:二叉树根结点root
// 输出:中间没有空格,末尾不换行。
{
    if(root){
        InOrder(root->left);
        printf("%c",root->data);
        InOrder(root->right);
    }
}

int main(int argc, const char * argv[]) {
    char str[200];
    scanf("%s", str);

    int i = 0;
    int len = int(strlen(str));
    BiTreeNode* root = CreatBiTree(str, i, len);

    InOrder(root);

    return 0;
}

Test file:
Test input: ABC##D##EF###
Expected output: CBDAFE

Test Input: ABCD###E#F##G##
Expected Output: DCBEFAG

Note:
1. #include<cstdio>
cstdio expresses the content of stdio.h in the form of C++ header file. stdio.h is the header file in the C standard function library, namely: standard buffered input&output. Provides basic text input and output stream operations (including screens and files, etc.). Since the C language does not provide keywords dedicated to text input and output, this library is the most common C language program loading library.
How to use
stdio.h is the header file of C and C++ in the past, cstdio is standard C++ (STL), and the functions in cstdio are all defined in a namespace std, if you want to call the function of this namespace, you must Add std:: or declare using namespace std in the file.

#include<cstdio>
 
using namespace std;/*你也可以同时加上这个语句*/

2.#include<algorithm>
algorithm means "algorithm", which is one of the most important header files in the C++ Standard Template Library (STL), providing a large number of non-member template functions based on iterators.

Reference: Data structure experiment questions in https://www.educoder.net/

Guess you like

Origin blog.csdn.net/qq_40804558/article/details/84307393