二叉树C++实现

//============================================================================
// Name        : TreeNodeCPP.cpp
// Author      : 
// Version     :
// Copyright   : 
// Description : Hello World in C++, Ansi-style
//============================================================================

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

#define MAX_NODE_NUM 20

void init_stack(std::stack<int> &cstack, int size) {
    int i = 0;

    for (i = 0; i < size; ++i) {
        cstack.push(i);
    }
}

struct Node {
    int value;
    int tag;
    struct Node *left;
    struct Node* right;
};

static Node* build_tree(int size);
static Node* new_node(int value);
static void pre_order(Node* root);
static void in_order(Node* root);
static void back_order(Node* root);
static void pre_stack_order(Node* root);
static void in_stack_order(Node* root);
static void back_stack_order(Node* root);
int main() {
    Node* root = build_tree(15);
    cout << "---------------------" << endl;
    pre_order(root);
    cout << "---------------------" << endl;
    pre_stack_order(root);
    cout << "---------------------" << endl;
    in_order(root);
    cout << "---------------------" << endl;
    in_stack_order(root);
    cout << "---------------------" << endl;
    back_order(root);
    cout << "---------------------" << endl;
    back_stack_order(root);
    return 0;
}

static Node* build_tree(int size) {
    std::queue<Node*> node_queue;
    Node* pnode = NULL;
    Node* root = NULL;
    int i = 0;
    int index = 0;

    while (i < size) {
        index = i;
        if (!pnode) {
            root = new_node(index);
            node_queue.push(root);
            ++i;
        } else {
            if (index < size) {
                pnode->left = new_node(index);
                node_queue.push(pnode->left);
                ++i;
            }

            index = i;

            if (index < size) {
                pnode->right = new_node(index);
                node_queue.push(pnode->right);
                ++i;
            }
        }

        if (node_queue.empty()) {
            break;
        }

        pnode = node_queue.front();
        node_queue.pop();
    }
    return root;
}

static Node* new_node(int value) {
    Node* pnode = NULL;
    pnode = (Node*) malloc(sizeof(Node));
    memset(pnode, 0, sizeof(*pnode));
    pnode->value = value;
    return pnode;
}

static void pre_stack_order(Node* root) {
    std::stack<Node*> cstack;
    Node* pnode = NULL;
    cstack.push(root);

    while (!cstack.empty()) {
        pnode = cstack.top();
        cstack.pop();

        while (pnode) {
            cout << "node:" << pnode->value << endl;
            cstack.push(pnode);
            pnode = pnode->left;
        }

        while (!cstack.empty()) {
            pnode = cstack.top();
            cstack.pop();
            if (pnode->right) {
                cstack.push(pnode->right);
                break;
            }
        }
    }
}

static void pre_order(Node* root) {
    if (root) {
        cout << "node:" << root->value << endl;
        pre_order(root->left);
        pre_order(root->right);
    }
}

static void in_stack_order(Node* root) {
    std::stack<Node*> cstack;
    Node* pnode = NULL;
    cstack.push(root);

    while (!cstack.empty()) {
        pnode = cstack.top();
        cstack.pop();

        while (pnode) {
            cstack.push(pnode);
            pnode = pnode->left;
        }

        while (!cstack.empty()) {
            pnode = cstack.top();
            cstack.pop();
            cout << "node:" << pnode->value << endl;
            if (pnode->right) {
                cstack.push(pnode->right);
                break;
            }
        }
    }
}

static void in_order(Node* root) {
    if (root) {
        in_order(root->left);
        cout << "node:" << root->value << endl;
        in_order(root->right);
    }
}

static void back_order(Node* root) {
    if (root) {
        back_order(root->left);
        back_order(root->right);
        cout << "node:" << root->value << endl;
    }
}

static void back_stack_order(Node* root) {
    std::stack<Node*> cstack;
    Node* pnode = NULL;
    cstack.push(root);

    while (!cstack.empty()) {
        pnode = cstack.top();
        cstack.pop();

        if ((pnode->tag & 0x01) != 1) {
            while (pnode) {
                cstack.push(pnode);
                pnode->tag |= 0x01;
                pnode = pnode->left;
            }
        }

        while (!cstack.empty()) {
            pnode = cstack.top();
            cstack.pop();
            if (pnode->tag == 3 || !pnode->right) {
                cout << "node:" << pnode->value << endl;
            } else if (pnode->right) {
                pnode->tag |= 0x2;
                cstack.push(pnode);
                cstack.push(pnode->right);
                break;
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值