前序中序、中序后序确定二叉树,广度优先遍历、前序、中序、后序的递归、非递归遍历

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

class Node {
    friend class BinaryTree;
    char data;
    Node *leftChild;
    Node *rightChild;
public:
    explicit Node(char _data='0',Node*_leftChild=nullptr,Node*_rightChild=nullptr):data(_data),leftChild(_leftChild),rightChild(_rightChild){}
};

class BinaryTree {
    Node *root;
    stack<Node*>stack;
    queue<Node*>queue;
public:
    explicit BinaryTree(Node*_root= nullptr):root(_root){}

    Node* PIFind(char *pre,int preFirst,char*in,int inFirst,int length){
        for (int i = 0; i < length; ++i) {
            if (in[inFirst+i]==pre[preFirst]){
                Node *node= new Node();
                node->data=pre[preFirst];
                node->leftChild= PIFind(pre,preFirst+1,in,inFirst,i);
                node->rightChild= PIFind(pre,preFirst+i+1,in,inFirst+i+1,length-i-1);
                return node;
            }
        }
        return nullptr;
    }

    Node* IPFind(char *in,int inFirst,char*post,int postFirst,int length){
        for (int i = 0; i < length; ++i) {
            if (in[inFirst+i]==post[postFirst+length-1]){
                Node*node=new Node();
                node->data=post[postFirst+length-1];
                node->leftChild= IPFind(in,inFirst,post,postFirst,i);
                node->rightChild= IPFind(in,inFirst+i+1,post,postFirst+i,length-i-1);
                return node;
            }
        }
        return nullptr;
    }

    void BreadthFirstSearch(){
        Node*mark=new Node();
        int cnt=0;
        while (!queue.empty())
            queue.pop();
        queue.push(mark);
        queue.push(root);
        while (queue.front()!=queue.back()){
            if (queue.front()==mark){
                queue.push(mark);
                queue.pop();
                cnt++;
            } else{
                cout<<queue.front()->data<<' ';
                if (queue.front()->leftChild)
                    queue.push(queue.front()->leftChild);
                if (queue.front()->rightChild)
                    queue.push(queue.front()->rightChild);
                queue.pop();
            }
        }
        cout<<endl<<cnt<<" layers"<<endl;
    }

    void PreOrderSearch(Node*p){
        if (p){
            cout<<p->data<<' ';
            PreOrderSearch(p->leftChild);
            PreOrderSearch(p->rightChild);
        }
    }

    void InOrderSearch(Node*p){
        if (p){
            InOrderSearch(p->leftChild);
            cout<<p->data<<' ';
            InOrderSearch(p->rightChild);
        }
    }

    void PostOrderSearch(Node*p){
        if (p){
            PostOrderSearch(p->leftChild);
            PostOrderSearch(p->rightChild);
            cout<<p->data<<' ';
        }
    }

    void PreSearch(){
        Node*p=root;
        while (!stack.empty()||p){
            if (p){
                stack.push(p);
                cout<<p->data<<' ';
                p=p->leftChild;
            } else {
                p=stack.top()->rightChild;
                stack.pop();
            }
        }
    }

    void InSearch(){
        Node*p=root;
        while (!stack.empty()||p){
            if (p){
                stack.push(p);
                p=p->leftChild;
            } else {
                cout<<stack.top()->data<<' ';
                p=stack.top()->rightChild;
                stack.pop();
            }
        }
    }

    void PostSearch(){
        Node *p= root,*q= nullptr;
        while (!stack.empty()||p){
            if (p){
                stack.push(p);
                p=p->leftChild;
            } else if (stack.top()->rightChild&&stack.top()->rightChild!=q){
                p=stack.top()->rightChild;
            } else{
                cout<<stack.top()->data<<' ';
                q=stack.top();
                stack.pop();
            }
        }
    }

};

int main() {
    Node*e=new Node('E');
    Node*d=new Node('D');
    Node*c=new Node('C');
    Node*b=new Node('B',d,e);
    Node*a=new Node('A',b,c);
    BinaryTree*binaryTree=new BinaryTree(a);
    binaryTree->BreadthFirstSearch();
    cout<<endl;
    binaryTree->PreOrderSearch(a);
    cout<<endl;
    binaryTree->InOrderSearch(a);
    cout<<endl;
    binaryTree->PostOrderSearch(a);
    cout<<endl;
    binaryTree->PreSearch();
    cout<<endl;
    binaryTree->InSearch();
    cout<<endl;
    binaryTree->PostSearch();
    cout<<endl;
    BinaryTree*binaryTree1=new BinaryTree(binaryTree->PIFind("ABDGCEF",0,"DGBAECF",0,7));
    BinaryTree*binaryTree2=new BinaryTree(binaryTree->IPFind("DGBAECF",0,"GDBEFCA",0,7));
    binaryTree1->BreadthFirstSearch();
    binaryTree2->BreadthFirstSearch();
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,二叉树遍历有三种主要方式:先序遍历(根-左-右)、中序遍历(左-根-右)和后序遍历(左-右-根)。非递归的层次遍历(也叫广度优先遍历,从上到下、从左到右)通常使用队列来辅助实现。 这里分别给出这些遍历非递归算法代码: 1. 层序遍历广度优先遍历): ```c #include <stdio.h> #include <stdlib.h> #include <queue> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; void levelOrder(struct TreeNode* root) { if (root == NULL) return; // 使用队列存储每一层的节点 queue<struct TreeNode*> q; q.push(root); while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { struct TreeNode* node = q.front(); q.pop(); printf("%d ", node->val); // 打印当前节点值 if (node->left != NULL) q.push(node->left); if (node->right != NULL) q.push(node->right); } printf("\n"); // 换行表示新的一层 } } ``` 2. 先序遍历递归非递归两种方式,这里是非递归版本,使用栈): ```c void preorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s; s.push(root); while (!s.empty()) { struct TreeNode* node = s.top(); s.pop(); printf("%d ", node->val); // 打印当前节点值 if (node->right != NULL) s.push(node->right); if (node->left != NULL) s.push(node->left); } } ``` 3. 中序遍历非递归,同样使用栈): ```c void inorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s; struct TreeNode* curr = root; while (curr != NULL || !s.empty()) { while (curr != NULL) { s.push(curr); curr = curr->left; } curr = s.top(); s.pop(); printf("%d ", curr->val); // 打印当前节点值 curr = curr->right; } } ``` 4. 后序遍历非递归,使用两个栈): ```c void postorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s1, s2; s1.push(root); while (!s1.empty()) { struct TreeNode* node = s1.top(); s1.pop(); s2.push(node); if (node->left != NULL) s1.push(node->left); if (node->right != NULL) s1.push(node->right); } while (!s2.empty()) { struct TreeNode* node = s2.top(); s2.pop(); printf("%d ", node->val); // 打印当前节点值 } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值