按层创建二叉树(含二叉树的深度遍历和广度遍历)

/*
    层次创建二叉树
*/

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

typedef struct node{
    char value;
    struct node *left;
    struct node *right;
}CTree;

/*层次创建二叉树*/
void Create(char arr[],int length,CTree* &root)   
{
    if(arr == NULL || length<=0 ) {
        root = NULL;
    }
    int k = 0;
    int parent_node_count  =0 ;
    int child_node_count =0;
    CTree**  parent_list = NULL;
    CTree**  child_list = NULL;
    if( arr[k] != '#'){     /*满足条件时创建根节点*/
        root = new CTree;
        root->left = NULL ; root->value = arr[k]; root->right = NULL;
        parent_list = new CTree*[1];
        parent_node_count =1;
        parent_list[0] = root;
    }
    else{
        root = NULL;
    }
    int count =0;
    while(1){
        count =0;
        CTree* node = NULL;
        child_node_count = parent_node_count*2;    /*定义child_list的长度*/
        child_list = new CTree* [child_node_count];
        for(int j=0;j<child_node_count;j++){      /*遍历数组  街昂符合条件的节点装入child_list*/
            k++;
            if(arr[k] != '#'){
                node = new CTree;
                node->left = NULL ; node->right = NULL; node->value = arr[k];
                child_list[count] = node;
                count++;
                if(j%2 == 0){        /*创建child_list和parent的相应的节点的连接关系*/
                    parent_list[j/2]->left = node;
                }
                else{
                    parent_list[j/2]->right =  node;
                }
            }
        }
        parent_node_count = count;      /*获得新的parent_node_count的数值 */
        delete[] parent_list;
        parent_list = NULL;
        parent_list = child_list;
        if(count == 0){
            break;
        }
    }
}

/*先序遍历*/
void Traver_x(CTree *root)
{
    if(root == NULL)  return ;
    cout<<root->value<<" ";
    Traver_x(root->left);
    Traver_x(root->right);
}

/*中序遍历*/
void Traver_z(CTree *root)
{
    if( root == NULL )  return ;
    Traver_z(root->left);
    cout<<root->value<<" ";
    Traver_z(root->right);
}

/*后序遍历*/
void Traver_h(CTree *root)
{
    if(root == NULL)  return ;
    Traver_h(root->left);
    Traver_h(root->right);
    cout<<root->value<<" ";
}

/*层次遍历*/
void Traver_c(CTree *root)
{
    if(root == NULL)  return ;
    queue<CTree*>  qu;
    CTree* node = NULL;
    qu.push(root);
    while(!qu.empty()){
        node = qu.front();
        cout<<node->value<<" ";
        if(node->left){
            qu.push(node->left);
        }
        if(node->right){
            qu.push(node->right);
        }
        qu.pop();
    }
}

int main()
{
    char arr[] = {'1','2','3','#','4','5','#','#','#','#','#'};
    CTree* root = NULL;
    Create(arr,sizeof(arr)/sizeof(char),root);
    Traver_x(root);
    cout<<endl<<"--------------------------我是分割线-------------------------------"<<endl;
    Traver_z(root);
    cout<<endl<<"--------------------------我是分割线-------------------------------"<<endl;
    Traver_h(root);
    cout<<endl;
    system("pause");
    return 0;
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值