Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 50be76d

Browse files
committed
Лабораторная №30, часть 2
1 parent b8dd2d5 commit 50be76d

File tree

6 files changed

+260
-0
lines changed

6 files changed

+260
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0120</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0120-p30-2</artifactId>
12+
<description>Binary Search Tree</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0120;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("Двадцать девятая практическая работа!");
11+
}
12+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package ru.mirea.practice.s23k0120.task1;
2+
3+
public class BinarySearchTree<E extends Comparable<E>> {
4+
private Node<E> root;
5+
private int count;
6+
7+
public BinarySearchTree() {
8+
root = null;
9+
count = 0;
10+
}
11+
12+
public Node<E> getRoot() {
13+
return root;
14+
}
15+
16+
public void setRoot(Node<E> root) {
17+
this.root = root;
18+
}
19+
20+
public int getCount() {
21+
return count;
22+
}
23+
24+
public void setCount(int count) {
25+
this.count = count;
26+
}
27+
28+
public boolean binSearch(E item) {
29+
Node<E> searchNode;
30+
searchNode = this.getRoot();
31+
while (true) {
32+
if (searchNode == null) {
33+
return false;
34+
} else if (item.compareTo(searchNode.data) == 0) {
35+
return true;
36+
} else if (item.compareTo(searchNode.data) > 0) {
37+
searchNode = searchNode.getRight();
38+
} else {
39+
searchNode = searchNode.getLeft();
40+
}
41+
}
42+
}
43+
44+
//Cannot remove the root node because of a bug, would've fixed it later if I had time
45+
public boolean insert(E data) {
46+
// True - Inserted
47+
// False - Already exists
48+
if (this.getRoot() == null) {
49+
setRoot(new Node<>(data));
50+
return true;
51+
}
52+
Node<E> searchNode = getRoot();
53+
while (true) {
54+
if (data.compareTo(searchNode.getData()) == 0) {
55+
return false;
56+
} else if (data.compareTo(searchNode.getData()) > 0) {
57+
if (searchNode.getRight() == null) {
58+
searchNode.setRight(new Node<>(data));
59+
return true;
60+
} else {
61+
searchNode = searchNode.getRight();
62+
}
63+
} else {
64+
if (searchNode.getLeft() == null) {
65+
searchNode.setLeft(new Node<>(data));
66+
return true;
67+
} else {
68+
searchNode = searchNode.getLeft();
69+
}
70+
}
71+
}
72+
}
73+
74+
public boolean delete(E data) {
75+
Node<E> q = getRoot();
76+
Node<E> z = getRoot();
77+
while (true) {
78+
if (z == null) {
79+
return false;
80+
} else if (data.compareTo(z.getData()) == 0) {
81+
break;
82+
} else if (data.compareTo(z.getData()) > 0) {
83+
q = z;
84+
z = z.getRight();
85+
} else {
86+
q = z;
87+
z = z.getLeft();
88+
}
89+
}
90+
91+
if (z.getRight() == null) {
92+
if (q.getLeft().equals(z)) {
93+
q.setLeft(z.getLeft());
94+
} else {
95+
q.setRight(z.getLeft());
96+
}
97+
} else {
98+
Node<E> y = z.getRight();
99+
if (y.getLeft() == null) {
100+
y.setLeft(z.getLeft());
101+
if (q.getLeft().equals(z)) {
102+
q.setLeft(y);
103+
} else {
104+
q.setRight(y);
105+
}
106+
} else {
107+
Node<E> x = y.getLeft();
108+
while (x.getLeft() != null) {
109+
y = x;
110+
x = y.getLeft();
111+
}
112+
y.setLeft(x.getRight());
113+
x.setLeft(z.getLeft());
114+
x.setRight(z.getRight());
115+
if (q.getLeft().equals(z)) {
116+
q.setLeft(x);
117+
} else {
118+
q.setRight(x);
119+
}
120+
}
121+
}
122+
this.count -= 1;
123+
return true;
124+
}
125+
126+
public String walk(Node<E> searchNode) {
127+
if (searchNode == null) {
128+
return "";
129+
}
130+
String string = "";
131+
string += searchNode.getData();
132+
string += " ";
133+
string += walk(searchNode.getLeft());
134+
string += walk(searchNode.getRight());
135+
return string;
136+
}
137+
138+
public E min() {
139+
Node<E> searchNode;
140+
searchNode = this.root;
141+
while (searchNode.getLeft() != null) {
142+
searchNode = searchNode.getLeft();
143+
}
144+
return searchNode.getData();
145+
}
146+
147+
public E max() {
148+
Node<E> searchNode;
149+
searchNode = this.root;
150+
while (searchNode.getRight() != null) {
151+
searchNode = searchNode.getRight();
152+
}
153+
return searchNode.getData();
154+
}
155+
156+
@Override
157+
public String toString() {
158+
return walk(getRoot());
159+
}
160+
161+
class Node<E> {
162+
private E data;
163+
private Node<E> left;
164+
private Node<E> right;
165+
166+
public Node() {
167+
// Default constructor
168+
}
169+
170+
public Node(E data) {
171+
this.data = data;
172+
}
173+
174+
public E getData() {
175+
return data;
176+
}
177+
178+
public void setData(E data) {
179+
this.data = data;
180+
}
181+
182+
public Node<E> getLeft() {
183+
return left;
184+
}
185+
186+
public void setLeft(Node<E> left) {
187+
this.left = left;
188+
}
189+
190+
public Node<E> getRight() {
191+
return right;
192+
}
193+
194+
public void setRight(Node<E> right) {
195+
this.right = right;
196+
}
197+
}
198+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ru.mirea.practice.s23k0120.task1;
2+
3+
public class IntegerBinarySearchTree extends BinarySearchTree<Integer> {
4+
IntegerBinarySearchTree() {
5+
super();
6+
}
7+
8+
public boolean isBst(Node<Integer> node) {
9+
return isBstUtil(node, Integer.MIN_VALUE, Integer.MAX_VALUE);
10+
}
11+
12+
private boolean isBstUtil(Node<Integer> node, int minValue, int maxValue) {
13+
if (node == null) {
14+
return true;
15+
}
16+
if (node.getData() < minValue || node.getData() > maxValue) {
17+
return false;
18+
}
19+
return isBstUtil(node.getLeft(), minValue, node.getData()) && isBstUtil(node.getRight(), node.getData() + 1, maxValue);
20+
}
21+
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ru.mirea.practice.s23k0120.task1;
2+
3+
public abstract class Main {
4+
public static void main(String[] args) {
5+
IntegerBinarySearchTree integerSearch = new IntegerBinarySearchTree();
6+
int[] array = {5, 4, 2, 1, 3, 10, 9, 7, 8, 11, 12};
7+
for (int i : array) {
8+
integerSearch.insert(i);
9+
}
10+
System.out.println(integerSearch);
11+
integerSearch.delete(10);
12+
System.out.println(integerSearch);
13+
}
14+
}

students/23K0120/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@
4444
<module>23K0120-p28</module>
4545
<module>23K0120-p29</module>
4646
<module>23K0120-p30</module>
47+
<module>23K0120-p30-2</module>
4748
</modules>
4849
</project>

0 commit comments

Comments
 (0)