码迷,mamicode.com
首页 > 其他好文 > 详细

树形结构打印二叉树

时间:2015-12-23 19:17:10      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

先给出效果图:

                                46                             
                118                              0             
        64              89               60            85      
   19       88       88     17      13       26     5      65  
114  105  58  68   88  8  22  96  92  102  21  20 55 70  73  98

 

代码中主要是运用了递归, 递归思想还是很重要的,感觉与数学中分形的思想有异曲同工之妙!

下面贴出代码:

//binary_tree.h

struct binary_tree_node{
    int level;
    int value;
    int pre_space;
    int left_space;
    int right_space;
    binary_tree_node* p_left;
    binary_tree_node* p_right;
};

binary_tree_node* create_binary_tree_node(int value);
void destroy_binary_tree(binary_tree_node* p_root);

binary_tree_node* create_binary_tree_auto();
void create_binary_tree_recursively(binary_tree_node* p_node);//generate two son nodes, when pass into one node

 

//binary_tree.cpp

#include <stdio.h>
#include "binary_tree.h"
#include <stdlib.h>

int MAX_LEVEL=5;

binary_tree_node* create_binary_tree_node(int value,int level){
    binary_tree_node* p_node = new binary_tree_node();
    p_node->value = value;
    p_node->level = level;
    p_node->pre_space = 0;
    p_node->left_space = 0;
    p_node->right_space = 0;
    p_node->p_left = NULL;
    p_node->p_right = NULL;

    return p_node;
}

void destroy_binary_tree(binary_tree_node* p_root)
{
    if(p_root != NULL)
    {   
        binary_tree_node* p_left = p_root->p_left;
        binary_tree_node* p_right = p_root->p_right;

        delete p_root;
        p_root = NULL;

        destroy_binary_tree(p_left);
        destroy_binary_tree(p_right);
    }   
}

static int get_random_num(){
    int number = (int)(rand()%123);
    return number;
}

//此处创建根结点
binary_tree_node* create_binary_tree_auto(){
    int number = get_random_num();
    binary_tree_node* p_root = create_binary_tree_node(number,1);
    create_binary_tree_recursively(p_root);
    return p_root;
}

void create_binary_tree_recursively(binary_tree_node* p_node){
    int level = p_node->level;
    if(level < MAX_LEVEL){
        int number = get_random_num();
        binary_tree_node* p_left = create_binary_tree_node(number,level+1);
        p_node->p_left = p_left;

        number = get_random_num();
        binary_tree_node* p_right = create_binary_tree_node(number,level+1);
        p_node->p_right = p_right;

        create_binary_tree_recursively(p_left);
        create_binary_tree_recursively(p_right);
    }else{
        return;
    }
}

 

//print_binary_tree.cpp

#include <stdio.h>
#include "binary_tree.h"
#include <deque>

int calc_characters(int value){
    if(value == 0){
        return 1;
    }
    int characters = 0;
    while(value){
        characters ++;
        value = value/10;
    }
    return characters;
}

binary_tree_node* calc_space(binary_tree_node* p_node){
    if(p_node->p_left == NULL && p_node->p_right == NULL){
        p_node->left_space = 0;
        p_node->right_space = 0;
    }else if(p_node->p_left == NULL && p_node->p_right != NULL){
        p_node->p_right->pre_space = calc_characters(p_node->value);//right
        p_node->left_space = 0;
        p_node->right_space = calc_space(p_node->p_right)->left_space
            + calc_characters(p_node->p_right->value)
            + calc_space(p_node->p_right)->right_space;
    }else if(p_node->p_left != NULL && p_node->p_right == NULL){
        p_node->p_left->pre_space = p_node->pre_space;//left
        p_node->right_space = 0;
        p_node->left_space = calc_space(p_node->p_left)->left_space 
            + calc_characters(p_node->p_left->value)
            + calc_space(p_node->p_left)->right_space;
    }else{
        //set pre spaces, from father to son
        p_node->p_left->pre_space = p_node->pre_space;//left
        p_node->p_right->pre_space = calc_characters(p_node->value);//right
        //set left spaces
        p_node->left_space = calc_space(p_node->p_left)->left_space 
            + calc_characters(p_node->p_left->value)
            + calc_space(p_node->p_left)->right_space;
        //set right spaces
        p_node->right_space = calc_space(p_node->p_right)->left_space
            + calc_characters(p_node->p_right->value)
            + calc_space(p_node->p_right)->right_space;
    }
    return p_node;
}

void print_binary_tree(binary_tree_node* p_node){
    int value = p_node->value;
    int left_space = p_node->left_space + p_node->pre_space;
    int right_space = p_node->right_space;
    for(int k=0;k<left_space;k++){
        printf(" ");
    }
    printf("%d",value);
    for(int k=0;k<right_space;k++){
        printf(" ");
    }
}

int g_currunt_level = 1;

void print_from_top_to_bottom(binary_tree_node* p_root)
{
    if(p_root == NULL)
        return;

    //队列
    std::deque<binary_tree_node *> deque_tree_node;
    deque_tree_node.push_back(p_root);

    while(deque_tree_node.size())
    {   
        binary_tree_node* p_node = deque_tree_node.front();//从前面获取结点
        deque_tree_node.pop_front();//之后将其弹出

        if(p_node->level > g_currunt_level){
            printf("\n");
            g_currunt_level = p_node->level;
        }

        print_binary_tree(p_node);

        //先压入左结点
        if(p_node->p_left)
            deque_tree_node.push_back(p_node->p_left);

        //后压入右结点
        if(p_node->p_right)
            deque_tree_node.push_back(p_node->p_right);
    }   
    printf("\n");
}

int main(){
    binary_tree_node* p_root = create_binary_tree_auto();
    p_root = calc_space(p_root);
    print_from_top_to_bottom(p_root);
    destroy_binary_tree(p_root);
    return 0;
}

 

树形结构打印二叉树

标签:

原文地址:http://www.cnblogs.com/lengyue365/p/5070686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!