m***g 发帖数: 90 | 1 这个算法里生成的是 bst
12
/\
11 34
/\
22 45
/ \
43 67
/\
56 89
\
98
为啥不是:
12
/\
11 34
\ \
22 45
/ \
43 67
/\
56 89
\
98
bst的生成是先满足右子树么?
public static void main(String args[]) {
BinaryTreeTest b = new BinaryTreeTest();
int data[] = { 12, 11, 34, 45, 67, 89, 56, 43, 22, 98 };
BinaryTreeTest.BinaryTree root= b.new BinaryTree( data[0] );
System.out.print( "tree:" );
for (int i = 1; i < data.length; i++) {
root.insertTree( root, data[i] );
}
}
class BinaryTree {
int data;
BinaryTree leftpoiter;
BinaryTree rightpoiter;
BinaryTree(int data) {
this.data = data;
leftpoiter = null;
rightpoiter = null;
}
public void insertTree(BinaryTree root, int data) {
if (data >= root.data) {
if (root.rightpoiter == null) {
root.rightpoiter = new BinaryTree( data );
} else {
insertTree( root.rightpoiter, data );
}
} else {
if (root.leftpoiter == null) {
root.leftpoiter = new BinaryTree( data );
} else {
insertTree( root.leftpoiter, data );
}
}
}
} |
g*********s 发帖数: 1782 | 2 how come 22 is in 12's left sub-tree in ur second diagram?
【在 m***g 的大作中提到】 : 这个算法里生成的是 bst : 12 : /\ : 11 34 : /\ : 22 45 : / \ : 43 67 : /\ : 56 89
|
m***g 发帖数: 90 | 3 放的是右侧,难道只有一个子节点时默认是放在左侧?
thanks
【在 g*********s 的大作中提到】 : how come 22 is in 12's left sub-tree in ur second diagram?
|
g*********s 发帖数: 1782 | 4 gosh, pls review the definition of bst.
【在 m***g 的大作中提到】 : 放的是右侧,难道只有一个子节点时默认是放在左侧? : thanks
|
m***g 发帖数: 90 | 5 那么这是个 bst 么
12
/\
11 34
/\ /\
8 23 22 45
thanks
【在 g*********s 的大作中提到】 : gosh, pls review the definition of bst.
|
g*********s 发帖数: 1782 | 6 of course not!
frankly speaking u r not ready on job market. be much better prepared!
【在 m***g 的大作中提到】 : 那么这是个 bst 么 : 12 : /\ : 11 34 : /\ /\ : 8 23 22 45 : thanks
|
m***g 发帖数: 90 | 7 知道了,应该是从根节点开始≥放右,<放左,按这个递归下去...
of course not!
frankly speaking u r not ready on job market. be much better prepared!
【在 g*********s 的大作中提到】 : of course not! : frankly speaking u r not ready on job market. be much better prepared!
|