二叉树的遍历(从递归到非递归)

递归

前序遍历

//递归前序遍历
void preorderTraversal(Binarytree biTree){
    if (biTree == nullptr) {
        return;
    }
    cout<<biTree->data<<" ";
    preorderTraversal(biTree->left);
    preorderTraversal(biTree->right);
}

中序遍历

//递归中序遍历
void inorderTraversal(Binarytree biTree){
    if (biTree == nullptr) {
        return;
    }
    inorderTraversal(biTree->left);
    cout<<biTree->data<<" ";
    inorderTraversal(biTree->right);
}

后序遍历

//递归后序遍历
void postorderTraversal(Binarytree biTree){
    if (biTree == nullptr) {
        return;
    }
    postorderTraversal(biTree->left);
    postorderTraversal(biTree->right);
    cout<<biTree->data<<" ";
}

树的定义

//二叉树节点
typedef struct BitNode{
    int data;
    BitNode* left;
    BitNode* right;
}BitNode,*Binarytree;

typedef struct BitNodeStackBit{
    BitNode* node;
    BitNodeStackBit* next;
}
BitNodeStackBit,*BitNodeStack;
void initBitNodeStack(BitNodeStack& bitNodeStack){
    bitNodeStack = new BitNodeStackBit;
    bitNodeStack->node =nullptr;
    bitNodeStack->next =nullptr;
}
void pushBitNode(BitNodeStack& bitNodeStack,BitNode *newNode){
    BitNodeStackBit* p = new BitNodeStackBit{newNode,bitNodeStack};
    bitNodeStack = p;
}
BitNode* popBitNode(BitNodeStack& bitNodeStack){
    if (bitNodeStack == nullptr) {
        PerException("the stack is empty");
    }
    BitNodeStackBit* p = bitNodeStack;
    bitNodeStack = bitNodeStack->next;
    BitNode* cur = p->node;
    delete p;
    return cur;
}
BitNode* peepBitNode(BitNodeStack& bitNodeStack){
    if (bitNodeStack == nullptr) {
        PerException("the stack is empty");
    }
    BitNodeStackBit* p = bitNodeStack;
    BitNode* cur = p->node;
    return cur;
}
BitNode* getPopBitNode(BitNodeStack& bitNodeStack){
    return popBitNode(bitNodeStack);
}
bool isEmpty(BitNodeStack & bitNodeStack){
    if (bitNodeStack->node == nullptr) {
        return true;
    }
    else {
        return false;
    }
}

非递归

前序遍历

void preorderTraversalN(Binarytree bitTree){
    BitNodeStack stack;
    initBitNodeStack(stack);

    //问题一bitTree取了非
    //二叉树不为空,并且栈中还有元素
    while (bitTree || !isEmpty(stack)) {
        while(bitTree){
            //先打印后压栈,在访问左子树
            cout<<bitTree->data<<" ";
            pushBitNode(stack, bitTree);
            bitTree = bitTree->left;
        }
        if (!isEmpty(stack)) {
            //左子树没有了,拿出栈中的元素 找到其右子树,继续上面的循环
            BitNode*p = getPopBitNode(stack);
            bitTree = p->right;
        }
    }
}

中序遍历

void inorderTraversalN(Binarytree biTree){
    BitNodeStack stack = new BitNodeStackBit;
    initBitNodeStack(stack);
    while (biTree|| !isEmpty(stack)) {
        while (biTree) {
            pushBitNode(stack, biTree);
            biTree = biTree->left;
        }
        if (!isEmpty(stack)) {
            BitNode* p = getPopBitNode(stack);
            biTree = p->right;
            cout<<p->data<<" ";
        }
    }
}

后序遍历—根右左思想

string change(string str){
    string newStr ="";
    for (int i =(int)str.length()-1 ; i>=0; i--)
    {
        newStr += str[i];
    }
    return newStr;
}
void postorderTraversalNF(Binarytree biTree){
    BitNodeStack stack;
    initBitNodeStack(stack);
    string res ="";
    while (biTree || !isEmpty(stack)) {
        while (biTree) {
            res += to_string(biTree->data);
            res += " ";
            pushBitNode(stack, biTree);
            biTree = biTree->right;
        }
        if (!isEmpty(stack)) {
            BitNode *p =  getPopBitNode(stack);
            biTree = p->left;
        }
    }
    cout<<change(res).substr(1,res.length());
}

后序遍历—标记思想

void postorderTraversalND(Binarytree biTree){
    BitNodeStack stack1;
    LinkStack stack2;
    initBitNodeStack(stack1);
    initStack(stack2);
    //1、左结点全压栈1。随之栈2压入0  2、遍历该节点的右子树栈2弹0压1     如此循环 3、栈2为1,循环打印
    while (biTree ||!isEmpty(stack1)) {
        while (biTree) {
            pushBitNode(stack1, biTree);
            push(stack2, 0);
            biTree = biTree->left;
        }
        while (!isEmpty(stack1) && getTop(stack2) == 1) {
            pop(stack2);
            cout<<popBitNode(stack1)->data<<" ";
        }
        if (!isEmpty(stack1)) {
            BitNode* p =  peepBitNode(stack1);
            biTree = p->right;
            pop(stack2);
            push(stack2, 1);
        }
    }
}

栈的定义

//链式栈的定义(不带头结点,头插法)
//N->H->d->b->c->R
typedef struct LSNode{
    int data;
    LSNode* next;
}LSNode ,*LinkStack;

//初始化一个栈
void initStack(LinkStack &top){
    top =nullptr;
}
//清空一个栈
void clearStack(LinkStack &top){
    while (top != nullptr) {
        LSNode* p =top;
        top = top->next;
        delete p;
    }
}
//进栈
void push(LinkStack &top,int data){
    LSNode*p = new LSNode{data,top};
    top = p;
}
//出栈
int pop(LinkStack &top){
    if (top == nullptr) {
        PerException("the stack is empty");
    }
    int res = 0;
    LSNode* p = top;
    top = top->next;
    res = p->data;
    return res;
}
//查看栈顶元素
int getTop(LinkStack top){
    if (top == nullptr) {
        PerException("the stack is empty");
    }
    return top->data;
}
//求栈的长度
int length(LinkStack top){
    int count =0;
    LSNode* p =top;
    while (p!=nullptr) {
        p = p->next;
        count++;
    }
    return count;
}
//栈是否为空
bool isEmpty(LinkStack top){
    if (top == nullptr) {
        return true;
    }
    return false;
}
//栈是否为满
bool isFull(LinkStack top){
    return false;
}

测试数据

int main(){
    BitNode head  = {.data = 0};
    BitNode head1 = {.data = 1};
    BitNode head2 = {.data = 2};
    BitNode head3 = {.data = 3};
    BitNode head4 = {.data = 4};
    BitNode head5 = {.data = 5};
    BitNode head6 = {.data = 6};
    BitNode head7 = {.data = 7};

    head.left = &head1;
    head.right = &head2;
    head.left->left = &head3;
    head.right->left = &head4;
    head.right->right = &head5;
    head.left->left->right = &head6;
    head.left->left->left  = &head7;

    Binarytree  biTree = &head;
    printTree(biTree);
    preorderTraversal(biTree);
    cout<<endl;
    preorderTraversalN(biTree);
    inorderTraversal(biTree);
    cout<<endl;
    inorderTraversalN(biTree);
    postorderTraversal(biTree);
    cout<<endl;
    postorderTraversalNF(biTree);
    cout<<endl;
    postorderTraversalND(biTree);
    return 0;
}

宏观打印二叉树

string getSpace(int num) {
    string space = " ";
    string buf = "";
    for (int i = 0; i < num; i++) {
        buf.append(space);
    }
    return buf;
}
void printInOrder(Binarytree head, int height, string to, int len) {
    if (head == nullptr) {
        return;
    }
    printInOrder(head->right, height + 1, "v", len);
    string val = to ;
    val += to_string(head->data);
    val += to;
    int lenM = (int)val.length();
    int lenL = (len - lenM) / 2;
    int lenR = len - lenM - lenL;
    val = getSpace(lenL) + val + getSpace(lenR);
    cout<<getSpace(height * len) + val<<endl;
    printInOrder(head->left, height + 1, "^", len);
}
void printTree(Binarytree head) {
    cout<<"Binary Tree:";
    printInOrder(head, 0, "H", 17);
    cout<< endl;
}

猜你喜欢

转载自blog.csdn.net/opooc/article/details/81187855