-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.java
75 lines (66 loc) · 1.77 KB
/
Node.java
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
package com.tree;
import java.util.ArrayList;
import java.util.List;
public class Node {
public char value;
public int type;
public int num;
public List<Node> child = new ArrayList<>();
public Node() {
}
public Node(char value) {
this.value = value;
}
@Override
public String toString() {
return this.value + ":" + this.type + ":" + this.child + ";";
}
@Override
public boolean equals(Object o) {
if(o == null) {
return false;
}
if(o == this) {
return true;
}
if(!(o instanceof Node)) {
return false;
}
Node p = (Node) o;
return p.value == this.value;
}
public static void insert(Node root, String word, int type) {
if (word == null || word.isEmpty()) {
return;
}
Node pNode = root;
Node tmpNode = null;
for (int i = 0; i < word.length(); i++) {
tmpNode = new Node(word.charAt(i));
int idx = pNode.child.indexOf(tmpNode);
if(idx == -1) {
pNode.child.add(tmpNode);
idx = pNode.child.indexOf(tmpNode);
} else {
pNode.child.get(idx).num++;
}
pNode = pNode.child.get(idx);
}
pNode.type = type;
}
public static Node generateTree(String[] arr0,String[] arr1){
int len0 = arr0.length;
int len1 = arr1.length;
Node root = new Node();
for(int i=0;i<len0;i++){
Node.insert(root, arr0[i],0);
}
for(int i=0;i<len1;i++){
Node.insert(root, arr1[i],1);
}
return root;
}
public static void display(Node root) {
System.out.println(root);
}
}