Basic operation of binary tree (C++ implementation)

basic concept

A binary tree consists of a finite set of nodes.
This finite set is either an empty set, or a set consisting of a root node and two disjoint binary trees called the left and right subtrees of the root.

Features of Binary Trees

  1. Each node has at most two subtrees, so there is no node with degree greater than 2 in the binary tree.
  2. The left subtree and right subtree are ordered, and the order cannot be reversed at will.
  3. Even if a node in the tree has only one subtree, it is also necessary to distinguish whether it is a left subtree or a right subtree.

Five basic forms of binary tree

  1. empty binary tree
  2. only one root node
  3. Root node has only left subtree
  4. Root node has only right subtree
  5. The root node has both a left subtree and a right subtree

Related concepts

Edge: An ordered pair of two nodes, called an edge
Degree of a node: The number of subtrees a node contains becomes the degree of the node
Leaf node: Degree is 0 The node becomes a leaf
node Path (path)
path length (the length of the paths)
layers: the root is layer 0 (the layers of other nodes are equal to the layers of its parent node plus 1)
depth: the largest number of layers The number of layers of leaf nodes

Binary tree related conclusions

insert image description here

Binary tree storage structure

The sequential storage structure of a binary tree is generally only applicable to a complete binary tree. Usually, when we represent a binary tree, we use a chained storage structure.

binary linked list

A binary tree has at most two children per node, so it has a data field and two pointer fields.
We call such a linked list a binary linked list.
As shown in the picture:

lchild data rchild

Among them, data is a data field, and lchild and rchild are both pointer fields, which store pointers to the left child and right child respectively

Binary linked list structure definition of binary tree

/*定义二叉树的结构*/
typedef struct Node
{
    
    
    char data;                    /*数据域*/
    struct Node *lchild, *rchild; /*左子树和右子树*/
} * BiTree, BiNode;
/*整棵树和结点名称*/

Binary tree traversal

Traversal
systematically visits nodes in a data structure such that each node is visited exactly once

Deep search limited traversal of binary trees :
The following are the recursive definitions of three depth-first traversals:

preorder traversal

Rules: If the binary tree is empty, the operation returns, otherwise, visit the root node first, then visit the left subtree in preorder, then visit the right subtree in preorderPlease add a picture description

Inorder traversal

Traverse the left subtree in inorder, visit the root node, and traverse the right subtree in inorder
Please add a picture description

I have read the big guy's explanation of in-order traversal before: treat each node as a grape, project these grapes vertically on the same plane, and then visit from left to right, which is in-order traversal.

post order traversal

Traverse the left subtree in postorder, traverse the right subtree in postorder, and visit the root nodePlease add a picture description

small summary

Pre-order traversal : first root, then left, then right
In-order traversal : first left, then root, then right
Post-order traversal : first left, then right, then root
This root refers to the root of each forked subtree (the root node of the left and right subtrees) A node is not just the root node of the whole tree

the code

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
/*定义二叉树的结构*/
typedef struct Node
{
    
    
    char data;                    /*数据域*/
    struct Node *lchild, *rchild; /*左子树和右子树*/
} * BiTree, BiNode;
/*整棵树和结点名称*/

/*先需创建二叉树*/
void CreateBiTree(BiTree &T)
{
    
    
    char ch;
    cin >> ch;
    if (ch == '#')
        T = NULL;
    else
    {
    
    
        T = new BiNode; /*创建一个新节点*/
        T->data = ch;
        CreateBiTree(T->lchild);
        CreateBiTree(T->rchild);
    }
    /*递归创建*/
}
void InOrderTraverse(BiTree T)
{
    
    
    /*中序遍历*/
    if (T)
    {
    
    
        InOrderTraverse(T->lchild);
        cout << T->data;
        InOrderTraverse(T->rchild);
    }
}
void PreOrderTraverse(BiTree T)
{
    
    
    /*先序遍历*/
    if (T)
    {
    
    
        cout << T->data;
        PreOrderTraverse(T->lchild);
        PreOrderTraverse(T->rchild);
    }
}
void PostOrderTraverse(BiTree T)
{
    
    
    /*后序遍历*/
    if (T)
    {
    
    
        PostOrderTraverse(T->lchild);
        PostOrderTraverse(T->rchild);
        cout << T->data;
    }
}
/*统计二叉树中结点的个数*/
int NodeCount(BiTree T)
{
    
    
    if (T == NULL)
        return 0;
    else
        return NodeCount(T->lchild) + NodeCount(T->rchild) + 1;
}
/*求树的深度*/
int Depth(BiTree T)
{
    
    
    if (T == NULL)
        return 0;
    else
    {
    
    
        int i = Depth(T->lchild);
        int j = Depth(T->rchild);
        return i > j ? i + 1 : j + 1;
    }
}
/*复制二叉树*/
void Copy(BiTree T, BiTree &NewT)
{
    
    
    if (T = NULL)
    {
    
    
        NewT = NULL;
        return;
    }
    else
    {
    
    
        NewT = new BiNode;
        NewT->data = T->data;
        Copy(T->lchild, NewT->lchild);
        Copy(T->rchild, NewT->rchild);
    }
}
/*统计二叉树中叶子结点的个数*/
int LeafCount(BiTree T)
{
    
    
    if (!T)
        return 0;
    if (!T->lchild && !T->rchild)
        return 1;
    /*如果二叉树左子树和右子树皆为空,说明该二叉树根节点为叶子结点,结果为1*/
    else
        return LeafCount(T->lchild) + LeafCount(T->rchild);
}
/*二叉树中从每个叶子结点到跟结点的路径*/
void PrintAllPath(BiTree T, char path[], int pathlen)
{
    
    
    int i;
    if (T != NULL)
    {
    
    
        path[pathlen] = T->data; /*将当前结点放入路径中*/
        if (T->lchild == NULL && T->rchild == NULL)
        {
    
    
            /*若这个节点是叶子结点*/
            for (i = pathlen; i >= 0; i--)
                cout << path[i] << " ";
            cout << "\n";
        }
        else
        {
    
    
            PrintAllPath(T->lchild, path, pathlen + 1);
            PrintAllPath(T->rchild, path, pathlen + 1);
        }
    }
}
/*判断二叉树是否为空*/
int BiTree_empty(BiTree T)
{
    
    
    if (T)
        return 1;
    else
        return 0;
}
int main()
{
    
    
    BiTree T;
    //测试数据AB#CD##E##F#GH###
    cout << "先序遍历输入(以#结束):";
    CreateBiTree(T);
    cout << "中序遍历输出:";
    InOrderTraverse(T);
    cout << endl
         << "先序遍历输出:";
    PreOrderTraverse(T);
    cout << "\n"
         << "后序遍历输出:";
    PostOrderTraverse(T);
    cout << endl
         << "树的深度:" << Depth(T);
    cout << endl
         << "结点的个数:" << NodeCount(T);
    cout << endl
         << "二叉树中从每个叶子结点到根结点的所有路径:" << endl;
    char path[256];
    int pathlen = 0;
    PrintAllPath(T, path, pathlen);
    return 0;
}

special binary tree

  1. Full binary tree
    In a binary tree, if all branch nodes have left and right subtrees and the left and right subtrees are on the same level, such a binary tree becomes a full binary tree as shown in the figure
    :
    Please add a picture description

  2. All nodes
    have only left subtrees called left oblique trees. A binary tree in which all nodes have only right subtrees is called a right oblique tree. Both are collectively referred to as oblique trees. (This thing is actually a linear table when viewed vertically)

  3. If a complete binary tree
    is a binary tree with n nodes (numbered according to the sequence rule), the node numbered i (1<=i<=n) and the node numbered i in the full binary tree of the same depth are in the If the positions in the binary tree are the same, the tree is a complete binary tree, as shown in the figure:Please add a picture description

The properties of a complete binary tree:
1) Leaf nodes can only be in the bottom two layers
2) The leaves of the bottom layer must be concentrated in the left continuous position
3) If the node degree is 1, the node has only left children.
4) In the penultimate layer, if there is a leaf node, it must be located in the continuous position on the right.
5) The binary tree of the same node, the depth of the complete binary tree is the smallest

Guess you like

Origin blog.csdn.net/qq_52109814/article/details/119539568