-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVLTree.java
140 lines (132 loc) · 3.66 KB
/
AVLTree.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
/**
* Implementation of AVL tree data structure that holds Node ojbects
* that stores an integer as their data.
* @author Isaac Mast
*/
public class AVLTree {
// declare instance variables
private Node root;
/**
* Constructs an empty AVL tree.
*/
public AVLTree() {
this.root = null;
}
/**
* Constructs an AVL tree, setting root to a new Node that contains
* the int value, data
* @param data - the int value to be stored in the AVLtree's root node
*/
public AVLTree(int data) {
this.root = new Node(data);
}
/**
* Gets the root Node of the AVLTree
* @return root - the root Node of the AVLTree
*/
public Node getRoot() {
return this.root;
}
/**
* Creates a new Node that contains an integer, data, and
* inserts it into the AVL tree.
* @param data - int value to be stored in the new Node object
* @throws NodeAlreadyExistsException - if the tree contains a node
* that already has an int value equal to data
*/
public void insert(int data) throws NodeAlreadyExistsException {
try {
Node current = root;
Node parent = current;
if (root == null) {
root = new Node(data);
System.out.println(AVLTreeTest.SUCCESS);
} else {
while (current != null && data != current.getData()) {
if (data < current.getData()) {
parent = current;
current = current.getLeftChild();
} else {
parent = current;
current = current.getRightChild();
}
}
if (current != null && data == current.getData()) {
System.out.println(AVLTreeTest.FAILED);
throw new NodeAlreadyExistsException(NodeAlreadyExistsException.MESSAGE + data);
} else if (data < parent.getData() && parent.getLeftChild() == current) {
parent.setLeftChild(new Node(data));
System.out.println(AVLTreeTest.SUCCESS);
} else {
parent.setRightChild(new Node(data));
System.out.println(AVLTreeTest.SUCCESS);
}
}
} catch (NodeAlreadyExistsException e) {
System.out.println(e.toString());
}
}
/**
* Finds a Node in the AVL tree that contains the integer, data
* @return true if a Node is found in the AVL tree that contains
* the int value, data
* @return false if a Node is not found in the AVL tree that
* contains the int value, data
*/
public boolean find(int data) {
Node current = root;
while (current != null && data != current.getData()) {
if (data < current.getData()) {
current = current.getLeftChild();
} else {
current = current.getRightChild();
}
}
if (current == null) {
return false;
}
return true;
}
/**
* Performs a pre-order traversal of the AVLTree and prints out
* the data of each Node
* @param current - the visited Node in the AVLTree
*/
public void preOrder(Node current) {
if (current == null) {
System.out.print("null ");
} else {
System.out.print(current.getData() + " ");
preOrder(current.getLeftChild());
preOrder(current.getRightChild());
}
}
/**
* Performs an in-order traversal of the AVLTree and prints out
* the data of each Node
* @param current - the visited Node in the AVLTree
*/
public void inOrder(Node current) {
if (current == null) {
System.out.print("null ");
} else {
inOrder(current.getLeftChild());
System.out.print(current.getData() + " ");
inOrder(current.getRightChild());
}
}
/**
* Performs a post-order traversal of the AVLTree and prints out
* the data of each Node
* @param current - the visited Node in the AVLTree
*/
public void postOrder(Node current) {
if (current == null) {
System.out.print("null ");
} else {
postOrder(current.getLeftChild());
postOrder(current.getRightChild());
System.out.print(current.getData() + " ");
}
}
}