Repository navigation
Expand file tree
/
Copy pathMakeTree.java
More file actions
85 lines (76 loc) · 1.73 KB
/
Copy pathMakeTree.java
File metadata and controls
85 lines (76 loc) · 1.73 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Create a Tree made out of Nodes. Every time a new artist is found it gets its own Tree and every song
* made by that artist will be added to that specific Tree.
* @version 1.0
*
* @author S. Larsson<br> A. Ebeling<br> M. Tijanic
*/
public class MakeTree {
/**
* The start of the Tree.
*/
public Node root;
int size = 0;
/**
* Add a Node to the Tree.
* @param name Name of the artist.
*/
public void addNode(Item name) {
this.size++;
Node newNode = new Node(name);
if (root == null) {
root = newNode;
}
else {
Node focusNode = root;
Node parent;
while (true) {
parent = focusNode;
if (name.songName.compareTo(focusNode.element.songName) == 0) {
return;
}
else if (name.songName.compareTo(focusNode.element.songName) < 0) {
focusNode = focusNode.leftChild;
if (focusNode == null) {
parent.leftChild = newNode;
return;
}
}
else {
focusNode = focusNode.rightChild;
if (focusNode == null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
/**
* Remove everything in the Tree.
*/
public void makeEmpty() {
root.leftChild = null;
root.rightChild = null;
root = null;
}
/**
* Iterate through the tree. S
* tarts at root and goes through the whole Tree.
* @param focusNode The Node currently in focus.
*/
public void iterator(Node focusNode) {
if (focusNode != null) {
iterator(focusNode.leftChild);
System.out.println(focusNode.element);
iterator(focusNode.rightChild);
}
}
/**
* Get the size of the Tree (the number of Nodes).
* @return The Size.
*/
public int getSize() {
return size;
}
}