Skip to content

Commit 9b0694e

Browse files
committed
start new Heapsort-Implementation
1 parent 4d835f3 commit 9b0694e

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package de.moritzf.sorting.logic.sorting;
2+
3+
import java.util.ArrayList;
4+
import java.util.StringJoiner;
5+
6+
public class HeapSortNodeValue {
7+
8+
private ArrayList<Integer> numbers = new ArrayList<>();
9+
10+
public HeapSortNodeValue(int initialNumbers) {
11+
numbers.add(initialNumbers);
12+
}
13+
14+
public int getNumber() {
15+
return numbers.get(numbers.size() - 1);
16+
}
17+
18+
public void setNumber(int number) {
19+
this.numbers.add(number);
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return new StringJoiner(", ", this.getClass().getSimpleName() + "[", "]")
25+
.add("numbers = " + numbers)
26+
.toString();
27+
}
28+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package de.moritzf.sorting.logic.sorting;
2+
3+
public class NewHeapSort extends SortingAlgorithm{
4+
5+
6+
@Override
7+
public void doAllSteps() {
8+
9+
}
10+
11+
@Override
12+
public boolean doStep() {
13+
return false;
14+
}
15+
16+
@Override
17+
public int getInputSize() {
18+
return 0;
19+
}
20+
21+
@Override
22+
public void reset() {
23+
24+
}
25+
26+
@Override
27+
public String protocol2LaTeX() {
28+
return null;
29+
}
30+
31+
@Override
32+
public boolean undoStep() {
33+
return false;
34+
}
35+
36+
@Override
37+
public String getName() {
38+
return "Heapsort";
39+
}
40+
41+
@Override
42+
public int getStepLimit() {
43+
return 0;
44+
}
45+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package de.moritzf.sorting.logic.sorting;
2+
3+
public class NewHeapStep {
4+
5+
TreeNode<HeapSortNodeValue> currentNode;
6+
7+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package de.moritzf.sorting.logic.sorting;
2+
3+
4+
import java.util.*;
5+
6+
public class TreeNode<T> {
7+
private T value;
8+
private TreeNode<T> parent;
9+
private List<TreeNode<T>> children = new ArrayList<>();
10+
11+
public TreeNode(T value) {
12+
this.value = value;
13+
}
14+
15+
public T getValue() {
16+
return this.value;
17+
}
18+
19+
public void addChild(TreeNode<T> child) {
20+
this.children.add(child);
21+
child.parent = this;
22+
}
23+
24+
public void removeChild(TreeNode<T> child) {
25+
this.children.remove(child);
26+
child.setParent(null);
27+
28+
}
29+
30+
public void setParent(TreeNode<T> parent) {
31+
if (this.parent != null) {
32+
this.parent.children.remove(this);
33+
}
34+
if (parent != null) {
35+
parent.addChild(this);
36+
}
37+
this.parent = parent;
38+
}
39+
40+
public String toString() {
41+
return this.value.toString();
42+
}
43+
44+
public TreeNode<T> getNode(int index) {
45+
Queue<TreeNode<T>> queue = new LinkedList<>();
46+
queue.add(this.getRootNode());
47+
int breathSearchNiv = 1;
48+
while (queue.poll() != null && breathSearchNiv != index) {
49+
this.getChildren().forEach(child -> queue.add(child));
50+
queue.remove();
51+
breathSearchNiv++;
52+
}
53+
return queue.poll();
54+
}
55+
56+
57+
private int getNodeNumber() {
58+
Queue<TreeNode<T>> queue = new LinkedList<>();
59+
queue.add(this.getRootNode());
60+
int breathSearchNiv = 1;
61+
while (queue.poll() != null && !this.equals(queue.poll())) {
62+
this.getChildren().forEach(child -> queue.add(child));
63+
queue.remove();
64+
breathSearchNiv++;
65+
}
66+
return breathSearchNiv;
67+
68+
}
69+
70+
public List<TreeNode<T>> getChildren() {
71+
return new ArrayList<>(this.children);
72+
}
73+
74+
public TreeNode<T> getRootNode() {
75+
TreeNode<T> parent = this;
76+
77+
while (parent.parent != null) {
78+
parent = parent.parent;
79+
}
80+
81+
return parent;
82+
}
83+
84+
85+
}

0 commit comments

Comments
 (0)