-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
68 lines (62 loc) · 1.14 KB
/
BinaryTree.java
File metadata and controls
68 lines (62 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package game;
public class BinaryTree
{
Node root;
Node current;
public BinaryTree(int x, Storage story)
{
root = new Node(x, story);
}
public BinaryTree()
{
root = null;
}
public void add(int value, Storage story)
{
root = addRecursive(root, value, story);
}
private Node addRecursive(Node current, int value, Storage story)
{
if (current == null)
{
return new Node(value, story);
}
if (value < current.key)
{
current.left = addRecursive(current.left, value, story);
}
else if (value > current.key)
{
current.right = addRecursive(current.right, value, story);
}
else
{
// value already exists
return current;
}
return current;
}
}
class Node
{
int key;
String story;
String leLabel, rLabel;
String type = null;
Node left, right;
public Node(int x, Storage story)
{
key = x;
this.story = story.story;
this.type = story.type;
left = right = null;
leLabel = story.lText;
rLabel = story.rText;
}
public boolean isLeaf() {
if(this.left == null && this.right == null)
return true;
else
return false;
}
}