有没有一种方法可以将普通树转换为二叉搜索树? [英] Is there a way to convert from a general tree to binary SEARCH tree?

查看:104
本文介绍了有没有一种方法可以将普通树转换为二叉搜索树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将普通树转换为二叉树,

I know how to convert from a general tree to a binary tree just fine,

      a             a
    / | \          /
   b  c  d   ->   b
                   \
                    c
                     \
                      d

我只是被问到如何从通用树转换为二进制搜索树.我的想法是,问我的人不是不是二叉搜索树(我问过他,他说是的),或者是他在课堂笔记中误解了一些东西.无论如何,有人听说过这样做吗?普通树到二叉搜索树?我给他的答案是首先转换为二叉树,然后对其进行排序以获得一棵二叉树.这是正确的吗?

I was just asked how to convert from a general tree to a binary search tree though. My thoughts are that the person who asked me either didn't mean binary search tree (I asked him, he said he did), or he's misunderstanding something from his class notes. In any case, has anyone heard of doing this? General tree to binary search tree? The answer I gave him was first convert to a binary tree then sort it to get a binary search tree. Is this correct?

推荐答案

我认为您只需traverse初始树并将每个节点插入binary search tree.之后,您将把初始树转换为BST.

I think you just have to traverse the initial tree and insert each node into a binary search tree. After that, you will have converted your initial tree into a BST.

要遍历树,请单击此处

有关二进制搜索树的信息和插入方法单击此处

For binary search tree info and insertion method click here

这篇关于有没有一种方法可以将普通树转换为二叉搜索树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆