- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReversedTree.java
More file actions
Latest commit
124 lines (107 loc) · 3.38 KB
/
Copy pathReversedTree.java
File metadata and controls
124 lines (107 loc) · 3.38 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
importjava.util.List;
importjava.util.ArrayList;
publicclassReversedTree {
privateList<Node> leafs;
privateList<Node> upperLeafs;
publicReversedTree(Nodefirst) {
this.leafs = newArrayList<>();
this.upperLeafs = newArrayList<>();
this.leafs.add(first);
}
publicReversedTree(Linkablefirst) {
this(newNode(first));
}
/**
* Finds the maximal length of the ReversedTree
* @returns int that represents the maximal length
*/
publicintmaxLength() {
returnthis.maxLengthNode().size();
}
/**
* Finds the Node with the maximal length in the tree
* @return Node with the maximal length in the tree
*/
publicNodemaxLengthNode() {
NodemaxNode = this.leafs.get(0);
for (Nodeleaf : this.leafs) {
if (maxNode.size() < leaf.size()){
maxNode = leaf;
}
}
returnmaxNode;
}
/**
* Makes a copy of the tree's leafs
* @return List<Linkable> that contains the Linkable extracted from the leaf Node
*/
publicList<Linkable> copyOfLeafsElement() {
List<Linkable> another = newArrayList<>();
for (inti = 0; i <this.leafs.size(); i++) {
another.add(this.leafs.get(i).getData());
}
returnanother;
}
/**
* Appends and links two Node that wrap the given Linkables into the tree
* @param before Node not in the tree to link
* @param after Node must be in the tree
*/
publicvoidadd(Linkablebefore, Linkableafter) {
// Looking for the actual node to link
// in leafs
for (Nodecurrent : this.leafs) {
if (current.getData().equals(after)) {
this.add(newNode(before), current);
return;
}
}
// in upperLeafs
for (Nodecurrent : this.upperLeafs) {
if (current.getData().equals(after)) {
this.add(newNode(before), current);
return;
}
}
}
/**
* Appends and links two Node into the tree
* @param before Node not in the tree to link
* @param after Node must be in the tree
*/
privatevoidadd(Nodebefore, Nodeafter) {
if (this.leafs.contains(after)){
before.setNext(after);
this.putBack(after);
this.leafs.add(before);
} elseif (this.upperLeafs.contains(after)) {
before.setNext(after);
this.leafs.add(before);
} else {
System.out.println("Le " + after + " n'est pas dans le reversedTree. Il contient " + this);
}
}
/**
* Puts a Node from the leafs to the upper-leafs section
* @param Node to do the operation
*/
privatevoidputBack(Nodetarget) {
if (this.leafs.contains(target)) {
this.leafs.remove(target);
this.upperLeafs.add(target);
this.upperLeafs.remove(target.getNext());
}
}
publicNodegetNode(Stringtarget){
for(Noden : leafs){
if(n.getData().getName() == target){
returnn;
}
}
returnnull;
}
@Override
publicStringtoString() {
returnthis.leafs.toString();
}
}