如何求BST的最小元素?

发布于 2024-10-13 17:25:19 字数 1841 浏览 5 评论 0原文

我是 C++ 的初学者,在查找 BST 的最小元素时遇到问题。 BST 的实现方式如下:

class Tree{
struct Node {
int Element;
Node *Left, *Right;
Node(int Element) : Element(Element), Left(0), Right(0){}
};

Node *Root;
void InOrder(void(*Action)(int&), Node *Current);
void Destroy(Node *Current);

public:

Tree() : Root(0){}
void Insert(int Element);
void InOrder(void(*Action)(int&)) {InOrder(Action,Root);}
void Destroy() {Destroy(Root);}
};

InOrder、Destroy 和 Insert 方法的实现方式如下:

void Tree::Insert(int Element) {
Node *NewElement = new Node(Element);
if(!Root) Root = NewElement;

 else {
 Node *Previous, *Current = Root;

  while(Current) {
   Previous = Current;
   if(Element < Current->Element) Current = Current->Left;
   else Current = Current->Right;
  }

 if(Element < Previous->Element) Previous->Left = NewElement;
 else Previous->Right = NewElement;
 }
}

void Tree::InOrder(void(*Action)(int&),Node *Current) {
  if(Current) {
  InOrder(Action,Current->Left);
  Action(Current->Element);
  InOrder(Action,Current->Right);
}

}

void Tree::Destroy(Node *Current) {
 if(Current) {
  Destroy(Current->Left);
  Destroy(Current->Right);
  delete Current;
 }
}

我用来打印数字的主要函数和函数如下所示: <代码>

void Print(int &e) {
 cout << e << endl;
}

int main() {
 Tree t;
 while(1) {
 int Number;
 cout << "Insert number (insert 0 to end): ";
 cin >> Number;
 if(Number == 0) break;
 t.Insert(Number);
 }

 t.InOrder(Print);
 t.Destroy();
 getch();
}

<代码> 正如您可能注意到的,还实现了 InOrder 方法,也许可以以某种方式使用它来帮助解决我的问题...抱歉我的英语不好:/

I'm a beginner to c++ and am having problems with finding the minimal element of a BST. The BST is implemented in this way:

class Tree{
struct Node {
int Element;
Node *Left, *Right;
Node(int Element) : Element(Element), Left(0), Right(0){}
};

Node *Root;
void InOrder(void(*Action)(int&), Node *Current);
void Destroy(Node *Current);

public:

Tree() : Root(0){}
void Insert(int Element);
void InOrder(void(*Action)(int&)) {InOrder(Action,Root);}
void Destroy() {Destroy(Root);}
};

The InOrder, Destroy and Insert methods are implemented like this:

void Tree::Insert(int Element) {
Node *NewElement = new Node(Element);
if(!Root) Root = NewElement;

 else {
 Node *Previous, *Current = Root;

  while(Current) {
   Previous = Current;
   if(Element < Current->Element) Current = Current->Left;
   else Current = Current->Right;
  }

 if(Element < Previous->Element) Previous->Left = NewElement;
 else Previous->Right = NewElement;
 }
}

void Tree::InOrder(void(*Action)(int&),Node *Current) {
  if(Current) {
  InOrder(Action,Current->Left);
  Action(Current->Element);
  InOrder(Action,Current->Right);
}

}

void Tree::Destroy(Node *Current) {
 if(Current) {
  Destroy(Current->Left);
  Destroy(Current->Right);
  delete Current;
 }
}

And the main function and function which I use to print the numbers look like this:

void Print(int &e) {
 cout << e << endl;
}

int main() {
 Tree t;
 while(1) {
 int Number;
 cout << "Insert number (insert 0 to end): ";
 cin >> Number;
 if(Number == 0) break;
 t.Insert(Number);
 }

 t.InOrder(Print);
 t.Destroy();
 getch();
}


As you may noticed, the InOrder method is implemented also, maybe it can be used in some way to help solve my problem... Sorry for my bad English :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2024-10-20 17:25:19

最小值将是上面代码中调用 Action 的第一个值。尽可能向左走,你会发现最小值......

The minimal value would be the first value that calls Action in the above code. Go left as far as you can, and the minimal value you shall find...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文