avatar
初始化binary tree# JobHunting - 待字闺中
K*g
1
写了个关于binary tree的代码,想测试一下。但是想来想去,不知道怎么随机生成一
棵binary tree,谁有什么好的建议吗?多谢。
avatar
h*8
2
你只需要随机生成节点的值,然后一个一个插入到树里面就行了呀
avatar
K*g
3
怎么插到树里?关键不在于结点的值,而在于怎么写个方便的代码生成一棵树。

【在 h****8 的大作中提到】
: 你只需要随机生成节点的值,然后一个一个插入到树里面就行了呀
avatar
l*r
4
生成一个随机数列不就可以了。。汗w

【在 K******g 的大作中提到】
: 怎么插到树里?关键不在于结点的值,而在于怎么写个方便的代码生成一棵树。
avatar
c*n
5
The structure of a binary tree is fixed given the total number of nodes in
the tree, say N , so now you can easily construct the tree. I do not know
what is troubling you. perhaps you can post your code here, and we can tell
you what you need.

【在 K******g 的大作中提到】
: 怎么插到树里?关键不在于结点的值,而在于怎么写个方便的代码生成一棵树。
avatar
K*g
6
就说给定一个N(结点的总数),depth也是随机的,每个结点的值是随机的。每次调用
这个函数,就要随机生成各种不同形状的树。即使对相同的N和depth,树的形状也有很
多种。

tell

【在 c**********n 的大作中提到】
: The structure of a binary tree is fixed given the total number of nodes in
: the tree, say N , so now you can easily construct the tree. I do not know
: what is troubling you. perhaps you can post your code here, and we can tell
: you what you need.

avatar
a*y
7
所以说生成一个随机数列,不断add不就行了。。。

【在 K******g 的大作中提到】
: 就说给定一个N(结点的总数),depth也是随机的,每个结点的值是随机的。每次调用
: 这个函数,就要随机生成各种不同形状的树。即使对相同的N和depth,树的形状也有很
: 多种。
:
: tell

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。