-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNode.java
64 lines (49 loc) · 1.17 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
package BinaryTree;
public class Node {
Node left;
Node right;
int root;
public Node (int value) {
this.root = value;
this.right = new Node();
this.left = new Node ();
}
public Node() {
this.root = (Integer) 0;
}
public void setRight (int value){
this.right = new Node(value);
}
public void setLeft (int value){
this.left = new Node(value);
}
public void add(int value) {
if (value < this.root){
if (this.left.root == (Integer) 0) this.setLeft(value);
else this.left.add(value);
}
else if (value > this.root){
if (this.right.root == (Integer) 0) this.setRight(value);
else this.right.add(value);
}
}
public void tranverse () {
if (this.left.root != 0)
this.left.tranverse();
System.out.println(this.root);
if (this.right.root != 0 )
this.right.tranverse();
}
public boolean contains (int search) {
if (search == this.root){
return true;
}
if (search < this.root && this.root != 0){
return this.left.contains(search);
}
else if (search > this.root && this.root != 0){
return this.right.contains(search);
}
return false;
}
}