Skip to content

Commit 2cb6d5c

Browse files
committed
'update'
1 parent e8416e4 commit 2cb6d5c

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

group17/240094626/warm-up/src/com/coding/basic/impl/ArrayList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public boolean hasNext() {
155155

156156
@Override
157157
public Object next() {
158-
Object o = data[index++];
158+
Object o = get(index++);
159159
return o;
160160
}
161161

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.coding.basic.impl;
2+
3+
4+
/**
5+
* 二叉树简单实现(key为int类型)
6+
* @author 240094626
7+
*
8+
*/
9+
public class BinaryTree {
10+
private Node rootNode = null;
11+
12+
public Node insert(int key){
13+
return insert(key,null);
14+
}
15+
16+
public Node insert(int key ,Object o){
17+
Node newNode = new Node(key, o);
18+
if(rootNode == null){
19+
rootNode = newNode;
20+
return rootNode;
21+
}
22+
Node fatherNode = rootNode;
23+
Node currentNode = rootNode;
24+
while(currentNode != null){
25+
fatherNode = currentNode;
26+
if(key < currentNode.key){
27+
currentNode = currentNode.left;
28+
}else{
29+
currentNode = currentNode.right;
30+
}
31+
}
32+
if(key < fatherNode.key){
33+
fatherNode.left = newNode;
34+
}else{
35+
fatherNode.right = newNode;
36+
}
37+
return newNode;
38+
}
39+
40+
public Node getNode(int key){
41+
return get(rootNode, key);
42+
}
43+
44+
private Node get(Node n,int key){
45+
if(n == null){
46+
return null;
47+
}
48+
if(key < n.key){
49+
return get(n.left, key);
50+
}else if(key > n.key){
51+
return get(n.left, key);
52+
}
53+
return null;
54+
}
55+
56+
57+
58+
59+
private static class Node{
60+
61+
int key;
62+
Object data;
63+
Node left;
64+
Node right;
65+
66+
public Node(int key, Object data) {
67+
this.key = key;
68+
this.data = data;
69+
this.left = null;
70+
this.right = null;
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return "Node [key=" + key + ", data=" + data + "]";
76+
}
77+
78+
79+
}
80+
81+
82+
}

0 commit comments

Comments
 (0)