二叉树创建及遍历 (递归&非递归)

本文介绍了二叉树的基本概念,包括完全二叉树、满二叉树、二叉排序树和平衡二叉树,并详细阐述了如何使用C++创建并操作完全二叉树。主要内容涵盖了树的节点定义、创建完全二叉树的方法、以及前序、中序和后序的递归与非递归遍历方法。

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

 先来些基本概念:

完全二叉树(Complete Binary Tree)

 

满二叉树(Full Binary Tree)

 

二叉排序树 (二叉查找树)(Binary Sort Tree)

 

平衡二叉树(Balanced Binary Tree)

平衡二叉树是一种自平衡二叉查找树算法。在AVL中任何节点的两个儿子子树的高度最大差别为一,所以它也被称为高度平衡树。查找、插入和删除在平均和最坏情况下都是O(log n)。增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。平衡二叉树的常用算法有红黑树、AVL、Treap、伸展树、SBT等。

可见,树的部分内容很多,变化也很丰富,这里先练习点基础的。

//二叉树
#include <iostream>
#include <vector>
#include <stack>
#include <fstream>
using namespace std;

//二叉树节点定义
template <typename T> 
struct BTNode
{
    T data;
    BTNode<T> *left;
    BTNode<T> *right;

    BTNode()
    {
        left = NULL;
        right = NULL;
    }
};

typedef BTNode<int> BinNode;


//在完全二叉树中的索引值 + 结点value
BinNode* createIntBinTree()
{
    BinNode* root = NULL;
    BinNode* pNodes[255];

    int i;
    int val;

    ifstream in("data.txt") ;
#define cin in

    while ( cin >> i >> val )
    {
        BinNode* node = new BinNode;
        node->data = val;
        pNodes[i] = node;

        if ( i == 1 )
            root = node;
        else
        {
            int j = i/2;

            if ( i%2 == 0 )
                pNodes[j]->left = node;
            else
                pNodes[j]->right = node;
        }
    }

    return root;
}

//创建完全二叉树,返回根节点
BinNode* creatIntBTree()
{
    BinNode* root = NULL;

    vector<BinNode *> vecPNode;
    vecPNode.push_back(NULL);

    ifstream in("data.txt") ;
#define cin in

    //cout << "输入节点:";

    int i=0;
    int val;

    while(cin>>val)
    {
        ++i;
        BinNode* node = new BinNode;
        node->data = val;
        vecPNode.push_back(node);

        if (i==1)
            root = node;
        else
        {
            int parent = i/2;
            if (i%2==0) //left node
                vecPNode[parent]->left = node;
            else
                vecPNode[parent]->right = node;
        }
    };

    vecPNode.clear();
    return root;
}

//前序递归遍历
void recurPreOrder(BinNode* root)
{
    if(root)
    {
        cout << root->data << " ";
        recurPreOrder(root->left);
        recurPreOrder(root->right);
    }
}

//中序递归遍历
void recurInOrder(BinNode* root)
{
    if(root)
    {
        recurInOrder(root->left);
        cout << root->data << " ";
        recurInOrder(root->right);
    }
}

//后序递归遍历
void recurPostOrder(BinNode* root)
{
    if(root)
    {
        if(root->left)
            recurPostOrder(root->left);
        if(root->right)
            recurPostOrder(root->right);
        cout << root->data << " ";
    }
}

//非递归前序遍历
void preOrder(BinNode* root)
{
    BinNode* pn;
    pn = root;
    stack<BinNode *> nodes;

    while(pn || !nodes.empty())
    {
        if (pn)
        {            
            cout << pn->data << " "; //入缓冲区时访问结点数据
            nodes.push(pn);
            pn = pn->left;
        }
        else
        {
            BinNode* p = nodes.top();
            nodes.pop();
            pn = p->right;    
        }
    }
}

//非递归中序遍历
void inOrder(BinNode* root)
{
    BinNode* pn = root;
    stack<BinNode *> sn;

    while (pn != NULL || !sn.empty())
    {
        if (pn != NULL)
        {
            sn.push(pn);
            pn = pn->left;
        }
        else
        {
            BinNode* p = sn.top();
            cout << p->data << " "; //出缓冲区时访问结点数据
            sn.pop();
            pn = p->right;
        }
    }
}

//后序遍历
void postOrder(BinNode* root)
{
    BinNode* pn = root;
    BinNode* pre = NULL;
    stack<BinNode *> sn;

    while(pn!=NULL || !sn.empty())
    {
        if (pn!=NULL)
        {
            sn.push(pn);
            pn = pn->left;
        }
        else
        {        
            BinNode* p = sn.top();
            if (p->right == NULL || p->right == pre) //右子树为空或者右儿子已经被访问过,则访问该结点
            {    
                cout << p->data << " ";
                sn.pop();

                pre = p; //记录最近一次被访问的结点
                pn = NULL;
            }
            else
            {
                pn = p->right;
            }
        }
    }
}

int main ()
{
    BinNode *tree = NULL;
    tree = creatIntBTree();
    preOrder(tree);            cout << endl;
    recurPreOrder(tree);    cout << endl << endl;
    inOrder(tree);            cout << endl;
    recurInOrder(tree);        cout << endl << endl;
    postOrder(tree);        cout << endl;
    recurPostOrder(tree);    cout << endl << endl;

    return 0;
}

 

转载于:https://www.cnblogs.com/haba/p/3405530.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值