Building and traversing a binary tree

1. Basic Concepts

BinaryTree.png

Binary tree: A tree with no more than 2 child nodes per node is called a binary tree.
Root node: The top node is called the root node, and the root node is the common ancestor of all child nodes. For example, the "7" node in the above figure is the root node.
Child Nodes: Nodes other than the root node are called child nodes.
Leaf node: A node without child nodes is called a leaf node. For example, the "1" node, "5" node and "11" node in the above figure.

There are three types of binary tree traversal:
(1) Preorder traversal: first traverse the root node, then traverse the left subtree, and finally traverse the right subtree. The preorder traversal sequence of the above figure is: 7->4->1->5->12->8->11->13
(2) In-order traversal: traverse the left subtree first, then traverse the root node, Finally traverse the right subtree. The in-order traversal sequence in the above figure is: 1->4->5->7->8->11->12->13
(3) Post-order traversal: first traverse the left subtree, then traverse the right subtree, Finally traverse the root node. The post-order traversal order of the above figure is: 1->5->4->11->8->13->12->7

Binary sorted tree: A binary tree with left child node <= root node <= right child node is called a binary sorted tree (or sorted binary tree). The above figure is a binary sorted tree.

Second, the establishment and traversal of the binary tree

#include<iostream>
using namespace std;

struct BTreeNode                //定义二叉树结点的数据结构
{
    int data;
    BTreeNode *leftChild;       //左子结点指针
    BTreeNode *rightChild;      //右子结点指针
};

class Btree
{
    BTreeNode *root;            //根结点的指针,没写访问权限则为默认的private

public:
    Btree()                     //构造函数
    {
        root = NULL;
    }

    void createBtree(int);

    void preOrder()                     //前序遍历方法,对外接口
    {
        preOrderTraverse(root);
        cout << endl;
    }

    void preOrderTraverse(BTreeNode *); //中序遍历具体过程

    void inOrder()                      //中序遍历方法,对外接口
    {
        inOrderTraverse(root);
        cout << endl;
    }

    void inOrderTraverse(BTreeNode *);  //中序遍历具体过程

    void postOrder()                    //后序遍历方法,对外接口
    {
        postOrderTraverse(root);
        cout << endl;
    }

    void postOrderTraverse(BTreeNode *);//后序遍历具体过程
};

void Btree::createBtree(int x)
{
    BTreeNode *newnode = new BTreeNode;
    newnode->data = x;
    newnode->leftChild = NULL;
    newnode->rightChild = NULL;

    if(NULL == root)    //如果没有根结点,则第一个结点就是根结点
    {
        root = newnode;
    }
    else                //根据数值大小判断是左子结点还是右子结点
    {
        BTreeNode *father;
        BTreeNode *current = root;

        while(current != NULL)   //找到要插入newnode的节点指针
        {
            father = current;
            if(current->data > x)
            {
                current = current->leftChild;
            }
            else
            {
                current = current->rightChild;
            }
        }

        //newnode插入到father结点的左(或右)子结点位置
        if(father->data > x)
        {
            father->leftChild = newnode;
        }
        else
        {
            father->rightChild = newnode;
        }
    }
}

void Btree::preOrderTraverse(BTreeNode *root)
{
    if(root)
    {
        cout << root->data << " ";
        preOrderTraverse(root->leftChild);
        preOrderTraverse(root->rightChild);
    }
}

void Btree::inOrderTraverse(BTreeNode *root)
{
    if(root)
    {
        inOrderTraverse(root->leftChild);
        cout << root->data << " ";
        inOrderTraverse(root->rightChild);
    }
}

void Btree::postOrderTraverse(BTreeNode *root)
{
    if(root)
    {
        postOrderTraverse(root->leftChild);
        postOrderTraverse(root->rightChild);
        cout << root->data << " ";
    }
}

int main()
{
    Btree A;
    int arr[]={7, 4, 1, 5, 12, 8, 13, 11};

    //排序二叉树:左子结点<根节点<右子节点
    cout << "建立排序二叉树: ";
    int cnt = sizeof(arr) / sizeof(int);
    for(int i = 0; i < cnt; i++)
    {
        cout << arr[i] << " ";
        A.createBtree(arr[i]);
    }
    cout << endl;

    cout << "前序遍历: ";
    A.preOrder();

    cout << "中序遍历: ";
    A.inOrder();

    cout << "后序遍历: ";
    A.postOrder();

    return 0;
}

operation result:

建立排序二叉树: 7 4 1 5 12 8 13 11
前序遍历:7 4 1 5 12 8 11 13
中序遍历:1 4 5 7 8 11 12 13
后序遍历:1 5 4 11 8 13 12 7


TopCoder & Codeforces & AtCoder exchange QQ group: 648202993 For
more information, please pay attention to the WeChat public account
wechat_public.jpg

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324895439&siteId=291194637