为什么我的代码无法在二进制搜索树中正确执行预购表格?

Why my code won't do preorder form correctly in Binary Search Tree?

我为字符串编辑了二叉搜索树的代码。但是有一个小问题。当我输入像 A B C D E F 这样的简单输入时,我的程序显示预订单是 A B C D E F... 它实际上应该是 A B D E C F.

Since, it should print the word at the root then print the word at the left subtree in pre-order and then print the word at the right subtree in pre-order.

Post order 也应该打印 D E B F C A 但它打印 A B C D E F 并且 in-order 应该打印 D B E A F C... 但它只是给了我 F E D C B A.

感谢任何帮助,我不知道哪里出了问题。

这是完整的工作源代码:

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

class Node {
string word;
Node* left;
Node* right;
public:
Node() { word=-1; left=NULL; right=NULL; };
void setWord(string aWord) { word = aWord; };
void setLeft(Node* aLeft) { left = aLeft; };
void setRight(Node* aRight) { right = aRight; };
string Word() { return word; };
Node* Left() { return left; };
Node* Right() { return right; };
};

class Tree {
 Node* root;
public:
 Tree();
 ~Tree();
 Node* Root() { return root; };
 void addNode(string word);
 void inOrder(Node* n);
 void preOrder(Node* n);
 void postOrder(Node* n);
private:
 void addNode(string word, Node* leaf);
 void freeNode(Node* leaf);
};

Tree::Tree() {
 root = NULL;
}

Tree::~Tree() {
 freeNode(root);
}

void Tree::freeNode(Node* leaf)
{
if ( leaf != NULL )
{
   freeNode(leaf->Left());
   freeNode(leaf->Right());
   delete leaf;
}
}

void Tree::addNode(string word) {
 if ( root == NULL ) {
    Node* n = new Node();
    n->setWord(word);
    root = n;
 }
 else {
   addNode(word, root);
 }
 }

void Tree::addNode(string word, Node* leaf) {
if ( word <= leaf->Word() ) {
   if ( leaf->Left() != NULL )
      addNode(word, leaf->Left());
   else {
      Node* n = new Node();
      n->setWord(word);
      leaf->setLeft(n);
   }
}
else {
   if ( leaf->Right() != NULL )
      addNode(word, leaf->Right());
   else {
      Node* n = new Node();
      n->setWord(word);
      leaf->setRight(n);
   }
}
}

void Tree::inOrder(Node* n) {
if ( n ) {
   inOrder(n->Left());
   cout << n->Word() << " ";
   inOrder(n->Right());
}
}

void Tree::preOrder(Node* n) {
if ( n ) {
   cout << n->Word() << " ";
   preOrder(n->Left());
   preOrder(n->Right());
}
}


void Tree::postOrder(Node* n) {
if ( n ) {
   postOrder(n->Left());
   postOrder(n->Right());
   cout << n->Word() << " ";
}
}

int main() {
string word;
Tree* tree = new Tree();
while(word != "end"){
   cin >> word;
   if(word == "end"){
       break;
   }
   tree->addNode(word);
}

cout << "In order traversal" << endl;
tree->inOrder(tree->Root());
cout << endl;

cout << "Pre order traversal" << endl;
tree->preOrder(tree->Root());
cout << endl;

cout << "Post order traversal" << endl;
tree->postOrder(tree->Root());
cout << endl;

delete tree;
_getch();
return 0;
}

在您的测试示例中 A B C D E F 您的树基本上是一个链表。首先,您插入 A,因此它成为您的新根。 BA 大,所以当你插入它时,它会像右 child 一样。所有接下来的字符串都会发生同样的情况:

A->B->C->D->E->F.

因此,当您从左侧遍历树时,您只需按原样打印列表,因为树中的任何节点都没有左侧 child。当你从右边遍历它时,你只是向后打印它。

因为你的树是不平衡的,这是一个预期的行为。据我所知,您的代码中没有错误。尝试添加平衡或创建另一个根。