Netbeans/ JAVA write this code: document every line. Create a class for a binary
ID: 3678189 • Letter: N
Question
Netbeans/ JAVA write this code: document every line.
Create a class for a binary tree named BT Each node in the tree should have data, a reference to the node's left sub-tree, and a reference to the node's right sub-tree. For the purposes of this project, the data in the nodes can be integers The binary tree should have a height property, a size property, which is the number of nodes in the tree, and the following methods: 1. a null constructor 2. a constructor that builds a tree from a specified array of integers. The tree does not need to be a balanced tree note: remember to address the height and size properties when building a tree from a specified array 1. preorder iterate list all nodes in order according to the preorder traversal of the tree) 2. inorder iterate (list all nodes in order according to the inorder traversal of the tree) 3. postorder iterate (list all nodes in order according to the postorder traversal of the tree) 4. search given a key value, tell us whether or not the integer is in the tree. 5. max return the maximum value in the tree 6. min return the minimum value in the tree Test each method to ensure that it works properly.Explanation / Answer
***Please ignore the numbers***
public class Node<T> {
2
public int value;
3
public Node left;
4
public Node right;
5
6
public Node(int value) {
7
this.value = value;
8
}
9
10
}
public class BinarySearchTree {
2
public Node root;
3
4
public void insert(int value){
5
Node node = new Node<>(value);
6
7
if ( root == null ) {
8
root = node;
9
return;
10
}
11
12
insertRec(root, node);
13
14
}
15
16
private void insertRec(Node latestRoot, Node node){
17
18
if ( latestRoot.value > node.value){
19
20
if ( latestRoot.left == null ){
21
latestRoot.left = node;
22
return;
23
}
24
else{
25
insertRec(latestRoot.left, node);
26
}
27
}
28
else{
29
if (latestRoot.right == null){
30
latestRoot.right = node;
31
return;
32
}
33
else{
34
insertRec(latestRoot.right, node);
35
}
36
}
37
}
38
}
/**
2
* Returns the minimum value in the Binary Search Tree.
3
*/
4
public int findMinimum(){
5
if ( root == null ){
6
return 0;
7
}
8
Node currNode = root;
9
while(currNode.left != null){
10
currNode = currNode.left;
11
}
12
return currNode.value;
13
}
14
15
/**
16
* Returns the maximum value in the Binary Search Tree
17
*/
18
public int findMaximum(){
19
if ( root == null){
20
return 0;
21
}
22
23
Node currNode = root;
24
while(currNode.right != null){
25
currNode = currNode.right;
26
}
27
return currNode.value;
28
}
**
2
* Printing the contents of the tree in an inorder way.
3
*/
4
public void printInorder(){
5
printInOrderRec(root);
6
System.out.println("");
7
}
8
9
/**
10
* Helper method to recursively print the contents in an inorder way
11
*/
12
private void printInOrderRec(Node currRoot){
13
if ( currRoot == null ){
14
return;
15
}
16
printInOrderRec(currRoot.left);
17
System.out.print(currRoot.value+", ");
18
printInOrderRec(currRoot.right);
19
}
/**
2
* Printing the contents of the tree in a Preorder way.
3
*/
4
public void printPreorder() {
5
printPreOrderRec(root);
6
System.out.println("");
7
}
8
9
/**
10
* Helper method to recursively print the contents in a Preorder way
11
*/
12
private void printPreOrderRec(Node currRoot) {
13
if (currRoot == null) {
14
return;
15
}
16
System.out.print(currRoot.value + ", ");
17
printPreOrderRec(currRoot.left);
18
printPreOrderRec(currRoot.right);
19
}
/**
2
* Printing the contents of the tree in a Postorder way.
3
*/
4
public void printPostorder() {
5
printPostOrderRec(root);
6
System.out.println("");
7
}
8
9
/**
10
* Helper method to recursively print the contents in a Postorder way
11
*/
12
private void printPostOrderRec(Node currRoot) {
13
if (currRoot == null) {
14
return;
15
}
16
printPostOrderRec(currRoot.left);
17
printPostOrderRec(currRoot.right);
18
System.out.print(currRoot.value + ", ");
19
20
}
/**
2
* Represents a node in the Binary Search Tree.
3
*/
4
public class Node<T> {
5
//The value present in the node.
6
public int value;
7
8
//The reference to the left subtree.
9
public Node left;
10
11
//The reference to the right subtree.
12
public Node right;
13
14
public Node(int value) {
15
this.value = value;
16
}
17
18
}
19
20
/**
21
* Represents the Binary Search Tree.
22
*/
23
public class BinarySearchTree {
24
25
//Refrence for the root of the tree.
26
public Node root;
27
28
public BinarySearchTree insert(int value) {
29
Node node = new Node<>(value);
30
31
if (root == null) {
32
root = node;
33
return this;
34
}
35
36
insertRec(root, node);
37
return this;
38
}
39
40
private void insertRec(Node latestRoot, Node node) {
41
42
if (latestRoot.value > node.value) {
43
44
if (latestRoot.left == null) {
45
latestRoot.left = node;
46
return;
47
} else {
48
insertRec(latestRoot.left, node);
49
}
50
} else {
51
if (latestRoot.right == null) {
52
latestRoot.right = node;
53
return;
54
} else {
55
insertRec(latestRoot.right, node);
56
}
57
}
58
}
59
60
/**
61
* Returns the minimum value in the Binary Search Tree.
62
*/
63
public int findMinimum() {
64
if (root == null) {
65
return 0;
66
}
67
Node currNode = root;
68
while (currNode.left != null) {
69
currNode = currNode.left;
70
}
71
return currNode.value;
72
}
73
74
/**
75
* Returns the maximum value in the Binary Search Tree
76
*/
77
public int findMaximum() {
78
if (root == null) {
79
return 0;
80
}
81
82
Node currNode = root;
83
while (currNode.right != null) {
84
currNode = currNode.right;
85
}
86
return currNode.value;
87
}
88
89
/**
90
* Printing the contents of the tree in an inorder way.
91
*/
92
public void printInorder() {
93
printInOrderRec(root);
94
System.out.println("");
95
}
96
97
/**
98
* Helper method to recursively print the contents in an inorder way
99
*/
100
private void printInOrderRec(Node currRoot) {
101
if (currRoot == null) {
102
return;
103
}
104
printInOrderRec(currRoot.left);
105
System.out.print(currRoot.value + ", ");
106
printInOrderRec(currRoot.right);
107
}
108
109
/**
110
* Printing the contents of the tree in a Preorder way.
111
*/
112
public void printPreorder() {
113
printPreOrderRec(root);
114
System.out.println("");
115
}
116
117
/**
118
* Helper method to recursively print the contents in a Preorder way
119
*/
120
private void printPreOrderRec(Node currRoot) {
121
if (currRoot == null) {
122
return;
123
}
124
System.out.print(currRoot.value + ", ");
125
printPreOrderRec(currRoot.left);
126
printPreOrderRec(currRoot.right);
127
}
128
129
/**
130
* Printing the contents of the tree in a Postorder way.
131
*/
132
public void printPostorder() {
133
printPostOrderRec(root);
134
System.out.println("");
135
}
136
137
/**
138
* Helper method to recursively print the contents in a Postorder way
139
*/
140
private void printPostOrderRec(Node currRoot) {
141
if (currRoot == null) {
142
return;
143
}
144
printPostOrderRec(currRoot.left);
145
printPostOrderRec(currRoot.right);
146
System.out.print(currRoot.value + ", ");
147
148
}
149
}
150
151
public class BinarySearchTreeDemo {
152
153
public static void main(String[] args) {
154
BinarySearchTree bst = new BinarySearchTree();
155
bst .insert(40)
156
.insert(25)
157
.insert(78)
158
.insert(10)
159
.insert(3)
160
.insert(17)
161
.insert(32)
162
.insert(30)
163
.insert(38)
164
.insert(78)
165
.insert(50)
166
.insert(93);
167
System.out.println("Inorder traversal");
168
bst.printInorder();
169
170
System.out.println("Preorder Traversal");
171
bst.printPreorder();
172
173
System.out.println("Postorder Traversal");
174
bst.printPostorder();
175
176
System.out.println("The minimum value in the BST: " + bst.findMinimum());
177
System.out.println("The maximum value in the BST: " + bst.findMaximum());
178
}
}
Hope this will help you!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.