二进制搜索树疑问 [英] Binary search tree doubt

查看:104
本文介绍了二进制搜索树疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好!



这次考虑数据结构我怀疑是另一回事。



我有一个作业分类。



创建一个具有以下值的二叉搜索树。



5,4,3,1,17,8,1



我发布了一张图片,但我不知道怎么回事..老式的方式



给定的解决方案是:



根节点是5
root的孩子是4

它的左孩子是3

而且它的左孩子是1



然后是正确的子树



root的右边孩子是17

它的左边孩子是8

而且它的孩子是11岁,其他方式写的是b
$ b

左子树(5> 4,4-> 3,3-> 1)右子树(5-> 17,17-> 8,8-> 11)



我的解决方案略有不同......



i就是这样做的:



左子树(5-> 4,4-> 3,3-> 1)右子树(5-> 8,8-> ; 11,11-> 17)



我的问题是:



既然看起来都是一个有效的二进制树我无法真正看到这些解决方案的区别,因此,我的问题..我的解决方案也是一个有效的解决方案,给定问题。





提前致谢



我尝试过的:



我可以看出为什么给定的解决方案没问题,但我没有看到为什么我的错会有正当理由,



这个想法是否正确?

Hello everyone!

Here I am with another doubt this time considering data structures.

I was given a homework sorta.

To create a binary search tree with the following values.

5, 4, 3 ,1, 17, 8, 11

I'd post a picture but i don't know how so.. the old fashioned way

The given solution is :

root node is 5
root's left-child is 4
it's left child is 3
and it's left child is 1

then the right subtree

root's right-child is 17
it's left child is 8
and it's rigth child is 11

written other way :

left subtree(5->4, 4->3, 3->1 ) right subtree (5->17, 17->8, 8->11)

My Solution differs a bit...

i made it that way :

left subtree(5->4, 4->3, 3->1) right subtree (5->8, 8->11, 11->17)

My question is :

Since both look as being a valid binary trees I can't really see the difference in
these solutions, therefore, my question.. is my solution also a valid solution to
the given problem.


Thanks in advance

What I have tried:

I can see why is the given solution all right but I don't see a valid reason why mine would be wrong,

Is this thinking correct?

推荐答案

两棵树都完全合法。

简单来说,给定的解决方案在订单值中插入值。

在您的解决方案中,17将在所有其他值之后插入。

给定的解决方案来自5,4, 3,1,17,8,11。

您的解决方案来自5 ,4,3,1,8,11,17。

您是否可以重新排序值。
Both trees are perfectly legal.
Simply, the given solution insert values in the order values are given.
In you solution, the 17 is inserted after all other values.
The given solution comes from 5, 4, 3 ,1, 17, 8, 11.
Your solution comes from 5, 4, 3 ,1, 8, 11, 17.
Are you allowed to reorder values.


这篇关于二进制搜索树疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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