数学结构之二叉树学习

#pragma once
#include <iostream>
using namespace std;

template <class T>
struct TreeNode
{
    T tvalue;
    TreeNode<T> *lChild, *rChild; //左子树与右子树

    TreeNode(T Nodevalue = 0, TreeNode<T> *leftnode = NULL, TreeNode<T> *rightnode = NULL) \
        : tvalue(Nodevalue), lChild(leftnode), rChild(rightnode)
    {

    }
};

//创建二叉树
template <class T>
bool CreateBinaryTree(TreeNode<T> *&root) //传递指针的引用
{
    T newvalue;
    cout << "input newvalue:";
    cin >> newvalue;
    if (newvalue == -1)
    {
        root = NULL;
    }
    else
    {
        root = new TreeNode<T>;
        root->tvalue = newvalue;
        CreateBinaryTree(root->lChild); //递归创建左右子树
        CreateBinaryTree(root->rChild);
    }
    return true;
}

//二叉树先序遍历
//1.访问根结点
//2.先序遍历左子树
//3.先序遍历右子树
template <class T>
void preOder(TreeNode<T> *&root)
{
    if (root)
    {
        cout << root->tvalue << "  ";
        preOder(root->lChild);
        preOder(root->rChild);
    }
}

//二叉树中序遍历
// 1. 中序遍历左子树
// 
// 2. 访问根结点
// 
// 3.  中序遍历右子树
template <class T>
void inOrder(TreeNode<T> *&root)
{
    if (root)
    {
        inOrder(root->lChild);
        cout << root->tvalue << "  ";
        inOrder(root->rChild);
    }
}

//二叉树后序遍历
// 1. 后序遍历左子树
// 
// 2. 后序遍历右子树
// 
// 3.  访问根结点

template <class T>
void postorder(TreeNode<T> *&root)
{
    if (root)
    {
        postorder(root->lChild);
        postorder(root->rChild);
        cout << root->tvalue << "  ";
    }
}

//统计二叉中的结点数
template <class T>
int countNode(TreeNode<T> *&root)
{
    if (root == NULL)
    {
        return 0;
    }
    return 1 + countNode(root->lChild) + countNode(root->rChild);
}

//求二叉树的深度 
template <class T>
int depth(TreeNode<T> *&root)
{
    if (root == NULL)
    {
        return -1;
    }
    int h1 = depth(root->lChild);
    int h2 = depth(root->rChild);
    if (h1 > h2)
    {
        return h1 + 1;
    }
    return h2 + 1;
}

//二叉树的销毁操作
template <class T>
void destory(TreeNode<T> *&root)
{
    if (root)
    {
        destory(root->lChild);
        destory(root->rChild);

        delete root;

        root = NULL;
    }
}
//横向打印的好处是规避命令界面一行多个字符的缩进问题,每行只用控制一个数字的缩进
//按树状结构打印二叉树
//先打印右子树,打印完右子树,打印本结点,再打印左子树
template <class T>
void printfTree(TreeNode<T> *&root, int nLayer)
{
    if (root == NULL)
    {
        return;
    }
    printfTree(root->rChild, nLayer + 3);
    for (int i = 0; i < nLayer; i++)
    {
        cout << " ";
    }
    cout << root->tvalue << endl;
    printfTree(root->lChild, nLayer + 3);
}


int _tmain(int argc, _TCHAR* argv[])
{
    TreeNode<int> *root;
    CreateBinaryTree(root);
    cout << "先序遍历:"; preOder(root); cout << endl;
    cout << "中序遍历:"; inOrder(root); cout << endl;
    cout << "后序遍历:"; postorder(root); cout << endl;
    cout << "树的结点数:" << countNode(root) << endl;
    cout << "树的深度:" << depth(root) << endl;
    cout << "销毁二叉树" << endl;
    destory(root);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值