-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNode_Sec77_G7.java
More file actions
77 lines (61 loc) · 1.7 KB
/
Copy pathTreeNode_Sec77_G7.java
File metadata and controls
77 lines (61 loc) · 1.7 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
/**
* Afrah - 1090111
* Aysha - 1088000
* Mehejet - 10
*/
public class TreeNode_Sec77_G7<E extends Comparable<E>> {
E element;
String color;
TreeNode_Sec77_G7<E> left = null, right = null, parent = null;
public TreeNode_Sec77_G7(E element) {
this.element = element;
this.color = "RED";
}
public boolean hasLeft() {
return left != null;
}
public boolean hasRight() {
return right != null;
}
public boolean hasTwoChildren() {
return left != null && right != null;
}
public boolean isLeaf() {
return left == null && right == null;
}
public boolean isLeftChild() {
return this == parent.left;
}
// gets the other child of the parent node
public TreeNode_Sec77_G7<E> getSibling() {
if (parent == null) {
return null;
}
if (isLeftChild()) {
return parent.right;
}
return parent.left;
}
public TreeNode_Sec77_G7<E> getUncle() {
TreeNode_Sec77_G7<E> grandparent = parent.parent;
if (parent == null || grandparent == null)
return null;
if (parent.isLeftChild())
return grandparent.right; // uncle i.e parent's sibling
else
return grandparent.left;
}
public boolean hasRedChild() {
return (hasLeft() && left.color == "RED") || (hasRight() && right.color == "RED");
}
void moveDown(TreeNode_Sec77_G7<E> newNode) {
if (parent != null) {
if (isLeftChild())
parent.left = newNode;
else
parent.right = newNode;
}
newNode.parent = parent;
parent = newNode;
}
}