在C ++中使用广度优先遍历来打印二叉树的问题?

时间:2017-10-17 04:09:51

标签: c++ binary-tree breadth-first-search

我正在编写一个程序,将前缀表示法中的表达式转换为表达式树。我无法得到正确的问题,我相信我的打印功能有问题,它使用广度优先遍历。例如,当我输入+ - * 1234时,我得到下面的输出。但是,1和2应该是Depth 3中的前两个节点。有人能指出我正确的方向或解释发生了什么吗?

输出:

Enter a prefix expression: +-*1234
Depth 0: + 
Depth 1: - 4 
Depth 2: * 3 1 2 
Depth 3: [Empty][Empty][Empty][Empty][Empty][Empty][Empty][Empty]


我的节目:

 #include <iostream>           // For cout, cin
 #include <cctype>            // For isdigit
 #include <queue>            // For queue, push, pop, front
 #include <algorithm>       // For max

 using namespace std;

 class node {
 public:
     // CONSTRUCTOR
     node(char data);

     char data;
     node *left, *right;
 };

 node::node(char data)
 {
     this -> data = data;
     this -> left = NULL;
     this -> right = NULL;
 }

 class next_node {
 public:
     // CONSTRUCTOR
     next_node(node *tree_node);

     node *tree_node;
     next_node *next;

 };

 next_node::next_node(node *tree_node)
 {
     this->tree_node = tree_node;
     next = NULL;
 }

 class tree {
 public:
     // CONSTRUCTOR
     tree() { root = NULL; }

     // Member Functions
     void add_node(node *ptr);
     void insert(char data);
     void build(string prefix);
     void print();
     int get_height(node *ptr);
     int double_num(int n);
     bool isOperator(char sym);
     node *remove_node()
     {
         if (root != NULL) {
             node *ptr = root -> tree_node;
             root = root -> next;
             return ptr;
         }
     }

     node *get_root()
     {
         return (root -> tree_node);
     }

 private:
     next_node *root;

 };

 void tree::add_node(node *ptr)
 {
     if (root == NULL) root = new next_node(ptr);
     else {
         next_node *new_ptr = new next_node(ptr);
         new_ptr -> next = root;
         root = new_ptr;
     }
 }

 void tree::insert(char data)
 {

     // If number
     if(isdigit(data)) {
         node *new_ptr = new node(data);
         add_node(new_ptr);

     // If operator
     } else if (isOperator(data)) {
         node *new_ptr = new node(data);
         new_ptr -> left = remove_node();
         new_ptr -> right = remove_node();
         add_node(new_ptr);
     }
 }

 void tree::build(string prefix)
 {
     // Scan from right to left
     for (int i = prefix.length() - 1;  i >= 0 ; i--) {
         insert(prefix[i]);
     }
 }

 void tree::print()
 {
     queue<node*> values;
     node * root_ptr = get_root();
     queue<char> line;

     if (root_ptr) values.push(root_ptr);
     else {
         values.push(NULL);
     }

     while (!values.empty())
     {
         node * current_node = values.front();
         values.pop();
         line.push(current_node -> data);

         if (current_node -> left)  {
             values.push(current_node -> left);
         }
         if (current_node -> right)  {
             values.push(current_node -> right);
         }
     }

     for(int i=0; i < get_height(root_ptr); i++) {
         cout << "Depth " << i << ": ";
         for(int j=0; j < double_num(i); j++) {
             if (!line.empty()) {
                 if (line.front() == NULL) {
                     cout<< "Empty";
                 } else {
                     cout << line.front() << " ";
                     line.pop();
                 }
             } else if (line.empty()) {
                 cout << "[Empty]";
             }
         }
         cout << "\n";
     }
 }


 int tree::get_height(node *ptr) {
     if(ptr == NULL ) return 0;
     else {
         return (1 + max(get_height(ptr -> left),
                         get_height(ptr -> right)));
     }
 }

 int tree::double_num(int n){
     if (n == 0) {
         return 1;
     } else {
         return 2 * double_num(n - 1);
     }
 }

 bool tree::isOperator(char sym)
 {
     return ( sym == '+' ||
              sym == '-' ||
              sym == '*' ||
              sym == '/' ||
              sym == '^' ||
              sym == '(' ||
              sym == ')' );
 }


 int main()
 {
     string user_input;
     tree expr_tree;

     cout << "Enter a prefix expression: ";
     cin >> user_input;

     // Build binary tree and print result
     expr_tree.build(user_input);
     expr_tree.print();

     return 0;
 }`

0 个答案:

没有答案