二叉树的相关操作

本文介绍了二叉树的基本概念,包括满二叉树和完全二叉树,并详细阐述了二叉树的遍历方法(前序、中序、后序)、结点计数和高度计算等核心操作。同时提供了C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉树的相关操作

一、二叉树的相关概念

1、二叉树的定义

一棵二叉树是一个结点的集合,这个集合要么为空,要么满足一下条件:

(1) 他仅有一个被称为根的结点;

(2) 除了根节点,其他的结点可以分为两个不相交的子集T1和T2,他们也是二叉树,分别叫做根的左子树和右子树。

2、二叉树的分类

· 满二叉树

一棵深度为K且有2^k-1个结点的二叉树称为满二叉树。

· 完全二叉树

一棵深度为K的完全二叉树,满足以下条件:

(1) 叶子结点只可能在层次最大的两层上出现;

(2) 最下面一层的结点集中分布在左侧。

二、二叉树的运算

1、遍历

访问二叉树的所有结点,每个结点能且只能访问一次,他包括前序遍历、中序遍历和后序遍历。

前序遍历

(1) 访问根结点;

(2) 前序遍历根结点的左子树;

(3) 前序遍历根结点的右子树;

中序遍历

(1) 中序遍历根结点的左子树;

(2) 访问根结点;

(3) 访问遍历根结点的右子树;

后序遍历

(1) 后序遍历根结点的左子树;

(2) 后序遍历根结点的右子树;

(3) 访问根结点;

2、结点个数

(1) 根结点计数;

(2) 根节点的左子树计数;

(3) 根结点的右子树计数;

3、二叉树高度(深度)

(1) 二叉树左结点计数;

(2) 二叉树右结点计数;

(2) 左右子树高度最大的计数 + 1(根结点)

三、二叉树的链式存储

二叉树的链式存储 * left _ data _ * left。

这里写图片描述

四、代码实现

binary_tree.cpp

#include <iostream>
using namespace std;

// 构造结点; 
template <class Record>
struct Binary_node
{
    Record data;
    Binary_node<Record> *left;
    Binary_node<Record> *right;
    Binary_node()
    {
        left=right=NULL;
    }   
    Binary_node(Record item, Binary_node<Record> *lptr=NULL, Binary_node<Record> *rptr=NULL)
    {
        data = item;
        left = lptr;
        right = rptr;
    }

};

// 构造二叉树 
template <class Record>
class Binary_tree
{
    protected:
        Binary_node<Record> *root;
    public:
        Binary_tree()
        {
            root=NULL;
        }
        void create_tree();

        void preorder()
        {
            recursive_preorder(root);
        }

        void inorder()
        {
            recursive_inorder(root);
        }

        void postorder()
        {
            recursive_postorder(root);
        }

        int size(int num)
        {
            recursive_size(root, num);
            return num;
        }

        int height()
        {
            return recursive_height(root);
        }

    private:
        void recursive_preorder(Binary_node<Record> *sub_root);
        void recursive_inorder(Binary_node<Record> *sub_root);
        void recursive_postorder(Binary_node<Record> *sub_root);
        void recursive_size(Binary_node<Record> *sub_root, int &num);
        int recursive_height(Binary_node<Record> * sub_root);
};


template <class Record>
void Binary_tree<Record>::create_tree()
{
    Binary_node<Record> *xn, *yn, *an, *bn, *cn, *dn, *en;
    dn = new Binary_node<Record>('D');
    en = new Binary_node<Record>('E');
    bn = new Binary_node<Record>('B', NULL, dn);
    cn = new Binary_node<Record>('C', en, NULL);
    an = new Binary_node<Record>('A', bn, cn);
    yn = new Binary_node<Record>('Y');
    root = xn = new Binary_node<Record> ('X', yn, an);
}

//前序遍历
template <class Record>
void Binary_tree<Record>::recursive_preorder(Binary_node<Record> *sub_root)
{
    if (sub_root == NULL)
        return;
    cout<<sub_root->data<<"  ";
    recursive_preorder(sub_root->left);
    recursive_preorder(sub_root->right);
};

//中序遍历
template <class Record>
void Binary_tree<Record>::recursive_inorder(Binary_node<Record> *sub_root)
{
    if (sub_root == NULL)
        return;
    recursive_inorder(sub_root->left);
    cout<<sub_root->data<<"  ";
    recursive_inorder(sub_root->right);
};

//后序遍历
template <class Record>
void Binary_tree<Record>::recursive_postorder(Binary_node<Record> *sub_root)
{
    if (sub_root == NULL)
        return;
    recursive_postorder(sub_root->left);
    recursive_postorder(sub_root->right);
    cout<<sub_root->data<<"  ";
};

// 二叉树结点个数
template <class Record>
void Binary_tree<Record>::recursive_size(Binary_node<Record> *sub_root, int &num)
{
    if(sub_root == NULL)
        return;
    num++;
    recursive_size(sub_root->left, num);
    recursive_size(sub_root->right, num);
};

//二叉树深度
template <class Record>
int Binary_tree<Record>::recursive_height(Binary_node<Record> *sub_root)
{
    int l, r, h;
    if(sub_root==NULL)
        return 0;
    l = recursive_height(sub_root->left);
    r = recursive_height(sub_root->right);
    h = 1 + (l>r?l:r);
    return h;
}

binary_tree_operation.cpp

#include <iostream>
#include "binary_tree.cpp"


int main()

{
    int number = 0;  
    Binary_tree<char> btree;
    cout<<"二叉树是: X(Y(NULL,NULL),A(B(NULL,D(NULL,NULL)),C(E(NULL,NULL),NULL)))"<<endl<<endl;
    btree.create_tree();
    cout<<"前序遍历结果是:";
    btree.preorder();
    cout<<endl;
    cout<<"中序遍历结果是:";
    btree.inorder();
    cout<<endl;
    cout<<"后序遍历结果是:";
    btree.postorder();
    cout<<endl;
    cout<<"结点个数是:";
    cout<<btree.size(number)<<endl;
    cout<<"高度是:";
    cout<<btree.height()<<endl;
    return 0;
}

五、结果

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值