-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedBinaryTree.java
174 lines (157 loc) · 5.14 KB
/
LinkedBinaryTree.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> {
protected static class Node<E> implements Position<E>{
private E element;
private Node<E> parent;
private Node<E> left;
private Node<E> right;
public Node(E e, Node<E> above, Node<E> leftChild, Node<E> rightChild){
element = e;
parent = above;
left = leftChild;
right = rightChild;
}
public E getElement(){
return element;
}
public Node<E> getParent(){
return parent;
}
public Node<E> getLeft(){
return left;
}
public Node<E> getRight(){
return right;
}
public void setElement(E e){
element = e;
}
public void setParent(Node<E> parentNode){
parent = parentNode;
}
public void setleft(Node<E> leftNode){
left = leftNode;
}
public void setRight(Node<E> rightNode){
right = rightNode;
}
}
protected Node<E> createNode(E e, Node<E> parent, Node<E> right, Node<E> left){
return new Node<E>(e, parent, left, right);
}
protected Node<E> root = null;
private int size = 0;
public LinkedBinaryTree(){}
protected Node<E> validate(Position<E> p) throws IllegalArgumentException{
if(!(p instanceof Node)){
throw new IllegalArgumentException("Not valid position type");
}
Node<E> node = (Node<E>) p;
if(node.getParent() == node){
throw new IllegalArgumentException("p no longer in the tree");
}
return node;
}
@Override
public int size(){
return size;
}
@Override
public Position<E> root(){
return root;
}
@Override
public Position<E> parent(Position<E> p) throws IllegalArgumentException{
Node<E> node = validate(p);
return node.getParent();
}
@Override
public Position<E> left(Position<E> p) throws IllegalArgumentException{
Node<E> node = validate(p);
return node.getLeft();
}
@Override
public Position<E> right(Position<E> p) throws IllegalArgumentException{
Node<E> node = validate(p);
return node.getRight();
}
public Position<E> addRoot(E e) throws IllegalStateException{
if(!isEmpty()){
throw new IllegalStateException("Tree is not empty");
}
root = createNode(e, null, null, null);
size = 1;
return root;
}
public Position<E> addLeft(Position<E> p, E e) throws IllegalArgumentException{
Node<E> parent = validate(p);
if(parent.getLeft() != null){
throw new IllegalArgumentException("p has already a left child");
}
Node<E> child = createNode(e, parent, null, null);
parent.setleft(child);
size++;
return child;
}
public Position<E> addRight(Position<E> p, E e) throws IllegalArgumentException{
Node<E> parent = validate(p);
if(parent.getRight() != null){
throw new IllegalArgumentException("p has already a right child");
}
Node<E> child = createNode(e, parent, null, null);
parent.setRight(child);
size++;
return child;
}
public E set(Position<E> p, E e) throws IllegalArgumentException{
Node<E> node = validate(p);
E temp = node.getElement();
node.setElement(e);
return temp;
}
public void attach(Position<E> p, LinkedBinaryTree<E> t1, LinkedBinaryTree<E> t2) throws IllegalArgumentException{
Node<E> node = validate(p);
if(isInternal(p)){
throw new IllegalArgumentException("p must be a leaf");
}
size += t1.size + t2.size;
if(!t1.isEmpty()){
t1.root.setParent(node);
node.setleft(t1.root);
t1.root = null;
t1.size = 0;
}
if(!t2.isEmpty()){
t2.root.setParent(node);
node.setRight(t2.root);
t2.root = null;
t2.size = 0;
}
}
public E remove(Position<E> p) throws IllegalArgumentException{
Node<E> node = validate(p);
if(numChildren(p) == 2){
throw new IllegalArgumentException("p has two children");
}
Node<E> child = (node.getLeft() != null ? node.getLeft() : node.getRight());
if(child != null){
child.setParent(node.getParent());
}
if(node == root){
root = child;
}else{
Node<E> parent = node.getParent();
if(node == parent.getLeft()){
parent.setleft(child);
}else{
parent.setRight(child);
}
}
size--;
E temp = node.getElement();
node.setElement(null);
node.setParent(node);
node.setRight(null);
node.setleft(null);
return temp;
}
}