二叉树C++实现

来源:互联网 发布:掌上电力显示网络问题 编辑:程序博客网 时间:2024/06/06 18:10
//============================================================================// 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 20void 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;            }        }    }}

0 0
原创粉丝点击