10.*/
11.public final class BinarySearch {
12.
13.public static int find(int[] a, int key) {
14. return find(a, 0, a.length - 1, key);
15.}
16.
17.// 非递归实现
18.public static int find(int[] a, int fromIndex, int toIndex, int key) {
19. int low = fromIndex;
20. int high = toIndex;
21.
22. while (low <= high) {
23. // 无符号右移位逻辑运算
24. int mid = (low + high) >>> 1;
25. int midVal = a[mid];
26.
27. if (midVal < key)
28. low = mid + 1;
29. else if (midVal > key)
30. high = mid - 1;
31. else
32. return mid; // key found
33. }
34. return -(low + 1); // key not found.
35.}
36.
37.// 递归实现
38.public static int search(int[] a, int fromIndex, int toIndex, int key) {
39. if(fromIndex > toIndex) {
40. return -1;
41. }
42. int mid = (fromIndex + toIndex) >>> 1;
43. if(a[mid] < key) {
44. return search(a, mid + 1, toIndex, key);
45. } else if(a[mid] > key) {
46. return search(a, fromIndex, mid - 1, key);
47. } else {
48. return mid;
49. }
50.}
Java多叉树
树的节点类:
Java代码
1.package TestTwo;
2.
3.import java.util.ArrayList;
4.import java.util.List;
5.
6.//多叉树的节点
7.public class ManyTreeNode {
8.
9. //节点的内容
10. private NodeBean data ;
11. //节点列表
12. private List childList;
13.
14. //构造函数
15. public ManyTreeNode(){
16. data = new NodeBean();
17. childList = new ArrayList();
18. }
19.
20. //构造函数 可以指定key的值
21. public ManyTreeNode(int key){
22. data = new NodeBean();
23. data.setKey(key);
24. childList = new ArrayList();
25. }
26.
27.}
packageTestTwo;
importjava.util.ArrayList;
importjava.util.List;
//多叉树的节点
publicclassManyTreeNode{
//节点的内容
privateNodeBeandata;
//节点列表
privateListchildList;
//构造函数
publicManyTreeNode(){
data=newNodeBean();
childList=newArrayList();
}
//构造函数可以指定key的值
publicManyTreeNode(intkey){
data=newNodeBean();
data.setKey(key);
childList=newArrayList();
}
}
多叉树类:
Java代码
1.package TestTwo;
2.
3.//多叉树
4.public class ManyNodeTree {
5.
6. //树根
7. private ManyTreeNode root;
8.
9. //构造函数
10. public ManyNodeTree(){
11. root = new ManyTreeNode();
12. root.getData().setNodeName("root");
13. }
14.
15. //构造函数
16. public ManyNodeTree(int key){
17. root = new ManyTreeNode();
18. root.getData().setKey(key);
19. root.getData().setNodeName("root");
20. }
21.
22.
23. //遍历多叉树
24. public String iteratorTree(ManyTreeNode treeNode){
25.
26. StringBuilder sb = new StringBuilder();
27.
28. if (treeNode !
= null) {
29.
30. if ("root".equals(treeNode.getData().getNodeName())) {
31. sb.append(treeNode.getData().getKey() + ",");
32. }
33.
34. for (ManyTreeNode index :
treeNode.getChildList()) {
35.
36. sb.append(index.getData().getKey() + ",");
37.
38. if (index.getChildList() !
= null && index.getChildList().size() > 0 ) {
39.
40. sb.append(iteratorTree(index));
41.
42. }
43. }
44. }
45.
46. return sb.toString();
47. }
48.
49.
50. //构造多叉树
51. public static ManyNodeTree createTree(){
52.
53. //用构造函数指定根节点的值
54. ManyNodeTree tree = new ManyNodeTree(60);
55.
56. //第一层的节点
57. ManyTreeNode node1 = new ManyTreeNode(40);
58. ManyTreeNode node2 = new ManyTreeNode(50);
59. ManyTreeNode node3 = new ManyTreeNode(30);
60.
61. tree.getRoot().getChildList().add(0, node1);
62. tree.getRoot().getChildList().add(1, node2);
63. tree.getRoot().getChildList().add(2, node3);
64.
65. //第二层的节点
66. ManyTreeNode node21 = new ManyTreeNode(85);
67. ManyTreeNode node22 = new ManyTreeNode(70);
68. ManyTreeNode node23 = new ManyTreeNode(15);
69. ManyTreeNode node24 = new ManyTreeNode(102);
70. ManyTreeNode node25 = new ManyTreeNode(83);
71. ManyTreeNode node26 = new ManyTreeNode(9);
72.
73. tree.getRoot().getChildList().get(0).getChildList().add(0,node21);
74. tree.getRoot().getChildList().get(0).getChildList().add(1,node22);
75. tree.getRoot().getChildList().get(0).getChildList().add(2,node23);
76.
77. tree.getRoot().getChildList().get
(1).getChildList().add(0,node24);
78. tree.getRoot().getChildList().get
(1).getChildList().add(1,node25);
79.
80. tree.getRoot().getChildList().get
(2).getChildList().add(0,node26);
81.
82. //第二层的节点
83. ManyTreeNode node31 = new ManyTreeNode(15);
84. ManyTreeNode node32 = new ManyTreeNode(20);
85. ManyTreeNode node33 = new ManyTreeNode(100);
86. ManyTreeNode node44 = new ManyTreeNode(60);
87.
88. tree.getRoot().getChildList().get(0).getChildList().get(0).getChildList().add(0,node31);
89. tree.getRoot().getChildList().get(0).getChildList().get(0).getChildList().add(1,node32);
90. tree.getRoot().getChildList().get(0).getChildList().get(0).getChildList().add(2,node33);
91.
92. tree.getRoot().getChildList().get(0).getChildList().get
(2).getChildList().add(0,node44);
93.
94. return tree;
95.
96. }
97.
98. /**
99. * @param args
100. */
101. public static void main(String[] args) {
102. ManyNodeTree testTree = ManyNodeTree.createTree();
103. String result = testTree.iteratorTree(testTree.getRoot());
104. System.out.println(result);
105. }
106.
107.}
108.
109.NodeBean类
110.
111.public class NodeBean {
112.
113. private int key;
114. private String nodeName;
115.
116. public String getNodeName() {
117. return nodeName;
118. }
119.
120. public void setNodeName(String nodeName) {
121. this.nodeName = nodeName;
122. }
123.
124. public int getKey() {
125. return key;
126. }
127.
128. public void setKey(int key) {
129. this.key = key;
130. }
131.}
Java二叉树遍历
java二叉树遍历
/**二叉树节点*/
publicclassBTNode{
privatecharkey;
privateBTNodeleft,right;
publicBTNode(charkey){
thi