qq_36865094 2016-12-24 11:54 采纳率: 80%
浏览 3129
已结题

求c++或c由先序中序遍历得出并打印树形二叉树,如下图。

如图 15
/ \
12 23
/ \
9 25求给出代码!!!!

  • 写回答

3条回答 默认 最新

  • Xefvan 2016-12-24 15:39
    关注

    如果是单纯的遍历,只需要输出数字即可,代码如下

    #include <iostream>
    using namespace std;
    
    template <typename T>
    struct binaryTreeNode
    {
        T element;
        binaryTreeNode<T> *leftChild, *rightChild;
    
        binaryTreeNode() {
            leftChild = rightChild = NULL;
        }
    
        binaryTreeNode(const T &theElement) : element(theElement)
        {
            leftChild = rightChild = NULL;
        }
    
        binaryTreeNode(const T &theElement, binaryTreeNode *theLeftChild, binaryTreeNode *theRightChild) : element(theElement)
        {
            leftChild = theLeftChild;
            rightChild = theRightChild;
        }
    };
    
    template <typename T>
    void printvalue(binaryTreeNode<T> *t)
    {
        cout << t->element << ' ';
    }
    
    template <typename T>
    void preOrder(binaryTreeNode<T> *t) 
    {
        if(t != NULL)
        {
            printvalue(t);                      // 输出元素
            preOrder(t->leftChild);     // 前序遍历左子树
            preOrder(t->rightChild);        // 后序遍历右子树
        }
    }
    
    template <typename T>
    void inOrder(binaryTreeNode<T> *t)
    {
        if(t != NULL)
        {
            inOrder(t->leftChild);        // 中序遍历左子树
            printvalue(t);                      // 输出元素
            inOrder(t->rightChild);         // 中序遍历右子树
        }
    }
    
    int main()
    {
        binaryTreeNode<int> *first, *second, *third, *four, *five, *six, *seven;
        seven = new binaryTreeNode<int>(7);
        six = new binaryTreeNode<int>(6);
        five = new binaryTreeNode<int>(25);
        four = new binaryTreeNode<int>(9);
        third = new binaryTreeNode<int>(23, six, seven);
        second = new binaryTreeNode<int>(12, four, five);
        first = new binaryTreeNode<int>(15, second, third);
        cout << "先序遍历:";
        preOrder(first);
        cout << endl;
        cout << "中序遍历:";
        inOrder(first);
        // PrintNodeByLevel(first);
        return 0;
    }
    

    博客里用这些代码和部分解析
    二叉树

    如果要输出有树形的二叉树,代码如下

    #include <iostream>
    #include <queue>
    using namespace std;
    
    template <typename T>
    struct binaryTreeNode
    {
        T element;
        binaryTreeNode<T> *leftChild, *rightChild;
    
        binaryTreeNode() {
            leftChild = rightChild = NULL;
        }
    
        binaryTreeNode(const T &theElement) : element(theElement)
        {
            leftChild = rightChild = NULL;
        }
    
        binaryTreeNode(const T &theElement, binaryTreeNode *theLeftChild, binaryTreeNode *theRightChild) : element(theElement)
        {
            leftChild = theLeftChild;
            rightChild = theRightChild;
        }
    };
    
    template <typename T>
    void printvalue(binaryTreeNode<T> *t)
    {
        cout << t->element << ' ';
    }
    
    template <typename T>
    void PrintNodeByLevel(binaryTreeNode<T>* root) {
        queue<binaryTreeNode<T>*> Q;
        queue<string> C; 
        Q.push(root);
        Q.push(0);
        do {
            binaryTreeNode<T>* node = Q.front();
            Q.pop();
            if (node) {
                cout << node->element << " ";
                if (node->leftChild) {
                    Q.push(node->leftChild);
                    C.push("/");
                }
                if (node->rightChild) {
                    Q.push(node->rightChild);
                    C.push("\\");
                }
            }
            else if (!Q.empty()) {
                cout << endl;
                while(C.size() != 0) {
                    cout << C.front() << " ";
                    C.pop();
                }
                Q.push(0);
                cout << endl;
            }
        } while (!Q.empty());
    }
    
    int main()
    {
        binaryTreeNode<int> *first, *second, *third, *four, *five, *six, *seven;
        seven = new binaryTreeNode<int>(7);
        six = new binaryTreeNode<int>(6);
        five = new binaryTreeNode<int>(25);
        four = new binaryTreeNode<int>(9);
        third = new binaryTreeNode<int>(23, six, seven);
        second = new binaryTreeNode<int>(12, four, five);
        first = new binaryTreeNode<int>(15, second, third);
        PrintNodeByLevel(first);
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB yalmip 可转移负荷的简单建模出错,如何解决?
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?