Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
151 lines (136 loc) · 4.81 KB

File metadata and controls

151 lines (136 loc) · 4.81 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import java.util.Objects;
public class AVLTree<T extends Comparable<T>> extends BinarySearchTree<T> {
public AVLTree(LinkedList<T> initialLinkedList) {
super(initialLinkedList);
}
public boolean isValid(){
boolean b1 = super.isValid();
boolean b2 = supervise() == null;
return b1 && b2;
}
public void addData(T data){
super.addData(data);
Node node = supervise();
_adjustTree(node);
}
private void _adjustTree(Node node){
Node parent = getParent(node);
switch (calculateRotateType(node)){
case "right":
if(parent == null){
headNode = _rightRotate(headNode);
}else {
if(parent.leftChild.equals(node)){
parent.leftChild = _rightRotate(node);
}else{
parent.rightChild = _rightRotate(node);
}
}
break;
case "leftThenRight":
if(parent == null){
headNode = _leftThenRightRotate(headNode);
}else {
if(parent.leftChild.equals(node)){
parent.leftChild = _leftThenRightRotate(node);
}else{
parent.rightChild = _leftThenRightRotate(node);
}
}
break;
case "left":
if(parent == null){
headNode = _leftRotate(headNode);
}else {
if(parent.leftChild.equals(node)){
parent.leftChild = _leftRotate(node);
}else{
parent.rightChild = _leftRotate(node);
}
}
break;
case "rightThenLeft":
if(parent == null){
headNode = _rightThenLeftRotate(headNode);
}else {
if(parent.leftChild.equals(node)){
parent.leftChild = _rightThenLeftRotate(node);
}else{
parent.rightChild = _rightThenLeftRotate(node);
}
}
break;
}
}
public void deleteData(T data){
super.deleteData(data);
Node node = supervise();
_adjustTree(node);
}
private String calculateRotateType(Node node){
if(_getLevels(node.leftChild) > _getLevels(node.rightChild)){
if(_getLevels(node.leftChild.leftChild) > _getLevels(node.leftChild.rightChild)){
// If insert into leftchild's left child tree.
return "right";
}else{
return "leftThenRight";
}
}else{
if(_getLevels(node.rightChild.rightChild) > _getLevels(node.rightChild.leftChild)){
// If insert into rightchild's right child tree.
return "left";
}else{
return "rightThenLeft";
}
}
}
private Node _rightRotate(Node head){
Node newNode = head.leftChild;
head.leftChild = newNode.rightChild;
newNode.rightChild = head;
return newNode;
}
private Node _leftRotate(Node head){
Node newNode = head.rightChild;
head.rightChild = newNode.leftChild;
newNode.leftChild = head;
return newNode;
}
private Node _rightThenLeftRotate(Node head){
head.rightChild = _rightRotate(head.rightChild);
return _leftRotate(head);
}
private Node _leftThenRightRotate(Node head){
head.leftChild = _leftRotate(head.leftChild);
return _rightRotate(head);
}
public Node supervise(){
return _supervise(headNode);
}
private Node _supervise(Node head){
if(_getLevels(head) <= 2){
return null;
}
Node result = null;
if(Math.abs(_getLevels(head.leftChild) - _getLevels(head.rightChild)) >= 2){
result = head;
}
Node leftResult = _supervise(head.leftChild);
Node rightResult = _supervise(head.rightChild);
if(leftResult != null){
result = leftResult;
}else if(rightResult != null){
result = rightResult;
}
return result;
}
public static void main(String[] args) {
MyArray<Integer> array = new MyArray<>(11);
array.buildArray(new Integer[]{5,2,-4,null,null,3,null,null,12,9,null,null,21,19,null,null,25,null,null});
AVLTree<Integer> tree = new AVLTree<>(new LinkedList<>(array));
System.out.print(tree.printTree("breadth"));
tree.deleteData(9);
System.out.print(tree.printTree("breadth"));
System.out.print("is valid? " + tree.isValid());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.