树的建立与层次遍历

#include <iostream>
#include <stack>
#include <queue>
#include <vector>
using namespace std;

typedef struct TreeNode{
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x):val(x),left(NULL),right(NULL){}
}TreeNode;

TreeNode* BulidTree()//普通建立一棵树(根左右)
{
    TreeNode *T;
    int a;
    cin >> a;
    if(a == 0)
    {
        return NULL;
    }
    else
    {
        T = new  TreeNode(a);
        T->left = BulidTree();
        T->right = BulidTree();
    }
    return T;
}

TreeNode* BulidTreeA(int b[])//通过数组建立一棵树
{
    TreeNode *T;
    static int i = 0 ;
    int a = b[i++];
    if(a == 0)
    {
        return NULL;
    }
    else
    {
        T = new  TreeNode(a);
        T->left = BulidTreeA(b);
        T->right = BulidTreeA(b);
    }
    return T;
}

TreeNode* BulidBST(TreeNode *T,int x)//生成二叉搜索树
{
    if(T == NULL)
    {
        TreeNode* T = new TreeNode(x);
        return T;
    }
    else
    {
        if(T->val > x)  T->left = BulidBST(T->left,x);
        else if((T->val < x))T->right = BulidBST(T->right,x);
    }
    return T;
}

void PrintVinTree(TreeNode *T)//中序遍历
{
    if(T == NULL)
        return;
    PrintVinTree(T->left);
    cout <<T->val <<" ";
    PrintVinTree(T->right);
}

vector<vector<int>> res;
vector<int> vec;

int PrintatLevel(TreeNode *T,int level)//层次遍历
{
    if(level<0 || !T)
    {
        return 0;
    }
    if( 0 == level)
    {
        cout << T->val <<" ";
        return 1;
    }
    return PrintatLevel(T->left,level-1) + PrintatLevel(T->right,level-1) ;
}

void print_by_level_1(TreeNode *T) {
    int i = 0;
    for (i = 0; i<=5 ; i++) {
        if (!PrintatLevel(T, i))
            break;
           cout << endl;
    }
    cout << endl;
}

//层次遍历迭代
void PrintLevelTree(TreeNode *T)//从上之下从左往右边打印结点
{
    queue<TreeNode*> que;
    que.push(T);
    while(!que.empty())
    {
        TreeNode *tmp = que.front();
        cout << tmp->val <<endl;
        que.pop();
        if(tmp->left) que.push(tmp->left);
        if(tmp->right) que.push(tmp->right);
    }
}


int main()
{
    TreeNode *T = NULL;
    //1、普通建立树
    // T = BulidTree();
    //2、建立BST树
//    int x ;
//    for(int i=0 ; i<5 ; i++)
//    {
//        cin >> x;
//        T = BulidBST(T,x);
//    }
  //  PrintVinTree(T);
  //  print_by_level_1(T);
  //   PrintLevelTree(T);
    //3、数组建立一棵树
    int b[11] = {10,8,0,6,0,0,12,0,13,0,0};
    T = BulidTreeA(b);
    PrintVinTree(T);
    return 0;
}

 

 

#include <iostream>
#include <stack>
using namespace std;

typedef struct TreeNode{
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x):val(x),left(NULL),right(NULL){}
}TreeNode;

TreeNode* BulidTree(TreeNode* T,int val)//生成二叉树
{
    if(T == NULL){
        T = new TreeNode(val);
        return T;
    }
    if(val < T->val) T->left  = BulidTree(T->left, val);
    else if(val > T->val) T->right = BulidTree(T->right, val);
    return T;
}

void PrintTree(TreeNode *T)//前序遍历
{
    if(T == NULL)
        return;
    cout << T->val <<endl;
    PrintTree(T->left);
    PrintTree(T->right);
}

void PrintpreTree(TreeNode *T)
{
    stack<TreeNode*> s;
    s.push(T);
    while(!s.empty())
    {
        TreeNode * tmp = s.top();
        s.pop();
        cout << tmp->val <<endl;
        if(tmp->left) s.push(tmp->left);
        if(tmp->right) s.push(tmp->right);
    }
}

void PrintVinTree(TreeNode *p)
{
    stack<TreeNode*> s;
    TreeNode* T = p;
    while(T != NULL  || !s.empty())
    {
        while(T != NULL)
        {
            s.push(T);
            T = T->left;
        }
        if(!s.empty()){
            T = s.top();
            s.pop();
            cout << T->val <<endl;
            T = T->right;
        }
    }
}

int main()
{
    TreeNode *T = NULL;
    int a;
     while(cin >> a)
    {
        if(a == 0) break;
        T = BulidTree(T,a);
    }
//    int t[] = {5,4,6,3,T};
//    for(int i=0 ; i<5 ; i++)
//    {
//         T = BulidTree(T,t[i]);
//    }
    PrintVinTree(T);

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值