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

Commit 95889ea

Browse files
committed
Лабораторная №31
1 parent 84593ed commit 95889ea

File tree

8 files changed

+282
-0
lines changed

8 files changed

+282
-0
lines changed

students/23K0120/23K0120-p31/pom.xml

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-p31</artifactId>
12+
<description>2-3 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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.mirea.practice.s23k0120.task1;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.util.Scanner;
6+
7+
public abstract class Main {
8+
public static void main(String[] args) throws FileNotFoundException {
9+
TwoThreeTree<Processor> processorTree = new TwoThreeTree<Processor>();
10+
try (Scanner scanner = new Scanner(new File("students/23K0120/23K0120-p31/src/main/java/ru/mirea/practice/s23k0120/task1/PROCS.txt"))) {
11+
while (scanner.hasNextLine()) {
12+
String line = scanner.nextLine();
13+
String[] splitLine = line.split(", ");
14+
Processor processor = new Processor(Integer.parseInt(splitLine[0]),
15+
splitLine[1],
16+
Double.parseDouble(splitLine[2]),
17+
Integer.parseInt(splitLine[3]),
18+
Double.parseDouble(splitLine[4]),
19+
Integer.parseInt(splitLine[5]),
20+
Integer.parseInt(splitLine[6]));
21+
processorTree.insert(processor);
22+
}
23+
}
24+
System.out.println(processorTree);
25+
}
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2, Intel Pentium 4, 2.0, 256, 0.400, 664, 734
2+
5, Intel Itanium, 0.800, 96, 0.266, 365, 701
3+
6, AMD Athlon XP, 1.6, 256, 0.266, 701, 634
4+
1, IBM Power 4, 1.3, 16384, 0.400, 814, 1169
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package ru.mirea.practice.s23k0120.task1;
2+
3+
public class Processor implements Comparable<Processor> {
4+
private final int key;
5+
private final String name;
6+
private final double tickFrequency;
7+
private final int cacheSize;
8+
private final double systemBusFrequency;
9+
private final int specInt;
10+
private final int specFp;
11+
12+
public Processor(int key, String name, double tickFrequency, int cacheSize, double systemBusFrequency, int specInt, int specFp) {
13+
this.key = key;
14+
this.name = name;
15+
this.tickFrequency = tickFrequency;
16+
this.cacheSize = cacheSize;
17+
this.systemBusFrequency = systemBusFrequency;
18+
this.specInt = specInt;
19+
this.specFp = specFp;
20+
}
21+
22+
public int getKey() {
23+
return key;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public double getTickFrequency() {
31+
return tickFrequency;
32+
}
33+
34+
public int getCacheSize() {
35+
return cacheSize;
36+
}
37+
38+
public double getSystemBusFrequency() {
39+
return systemBusFrequency;
40+
}
41+
42+
public int getSpecInt() {
43+
return specInt;
44+
}
45+
46+
public int getSpecFp() {
47+
return specFp;
48+
}
49+
50+
@Override
51+
public int compareTo(Processor o) {
52+
return Integer.compare(this.key, o.key);
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return String.valueOf(key);
58+
}
59+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ru.mirea.practice.s23k0120.task1;
2+
3+
public class TwoThreeTree<E extends Comparable<E>> {
4+
// Minimum degree
5+
private final int t;
6+
// Pointer to root node
7+
private TwoThreeTreeNode<E> root;
8+
9+
public TwoThreeTree() {
10+
this.t = 2;
11+
root = null;
12+
}
13+
14+
public TwoThreeTreeNode<E> search(E data) {
15+
return (root == null) ? null : root.search(data);
16+
}
17+
18+
// Function to insert a key into the B-tree
19+
public void insert(E data) {
20+
if (root == null) {
21+
root = new TwoThreeTreeNode<E>(true);
22+
root.dataArr.set(0, data);
23+
root.numOfKeys = 1;
24+
} else {
25+
if (root.numOfKeys == 2 * t - 1) {
26+
TwoThreeTreeNode<E> newRoot = new TwoThreeTreeNode<E>(false);
27+
newRoot.children.set(0, root);
28+
newRoot.splitChild(0, root);
29+
30+
int i = 0;
31+
32+
if (data.compareTo(newRoot.dataArr.get(0)) > 0) {
33+
i++;
34+
}
35+
36+
newRoot.children.get(i).insertNonFull(data);
37+
root = newRoot;
38+
} else {
39+
root.insertNonFull(data);
40+
}
41+
}
42+
}
43+
44+
// Function to print the entire B-tree
45+
public void printBTree() {
46+
if (root != null) {
47+
root.printInOrder();
48+
}
49+
50+
System.out.println();
51+
}
52+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package ru.mirea.practice.s23k0120.task1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class TwoThreeTreeNode<E extends Comparable<E>> {
7+
List<E> dataArr;
8+
int t;
9+
List<TwoThreeTreeNode<E>> children;
10+
int numOfKeys;
11+
boolean isLeaf;
12+
13+
public TwoThreeTreeNode(boolean isLeaf) {
14+
this.t = 2;
15+
this.isLeaf = isLeaf;
16+
17+
dataArr = new ArrayList<>();
18+
for (int i = 0; i < 2 * t - 1; i++) {
19+
dataArr.add(null);
20+
}
21+
22+
children = new ArrayList<>();
23+
for (int i = 0; i < 2 * t; i++) {
24+
children.add(null);
25+
}
26+
numOfKeys = 0;
27+
}
28+
29+
public TwoThreeTreeNode<E> search(E data) {
30+
int i = 0;
31+
32+
while (i < numOfKeys && data.compareTo(dataArr.get(i)) > 0) {
33+
i++;
34+
}
35+
36+
if (data.compareTo(dataArr.get(i)) == 0) {
37+
return this;
38+
}
39+
40+
if (isLeaf) {
41+
return null;
42+
}
43+
44+
return children.get(i).search(data);
45+
}
46+
47+
public void insertNonFull(E data) {
48+
int i = numOfKeys - 1;
49+
50+
if (isLeaf) {
51+
while (i >= 0 && data.compareTo(dataArr.get(i)) < 0) {
52+
dataArr.set(i + 1, dataArr.get(i));
53+
i--;
54+
}
55+
dataArr.set(i + 1, data);
56+
numOfKeys++;
57+
} else {
58+
while (i >= 0 && data.compareTo(dataArr.get(i)) < 0) {
59+
i--;
60+
}
61+
i++;
62+
63+
if (children.get(i).numOfKeys == 2 * t - 1) {
64+
splitChild(i, children.get(i));
65+
if (data.compareTo(dataArr.get(i)) > 0) {
66+
i++;
67+
}
68+
}
69+
children.get(i).insertNonFull(data);
70+
}
71+
}
72+
73+
public void splitChild(int ind, TwoThreeTreeNode<E> y) {
74+
TwoThreeTreeNode<E> z = new TwoThreeTreeNode<E>(y.isLeaf);
75+
z.numOfKeys = t - 1;
76+
77+
for (int i = 0; i < t - 1; i++) {
78+
z.dataArr.set(i, y.dataArr.get(i + t));
79+
}
80+
81+
if (!y.isLeaf) {
82+
for (int i = 0; i < t; i++) {
83+
z.children.set(i, y.children.get(i + t));
84+
}
85+
}
86+
87+
y.numOfKeys = t - 1;
88+
89+
for (int i = numOfKeys; i >= ind + 1; i--) {
90+
children.set(i + 1, children.get(i));
91+
}
92+
93+
children.set(ind + 1, z);
94+
95+
for (int i = numOfKeys - 1; i >= ind; i--) {
96+
dataArr.set(i + 1, dataArr.get(i));
97+
}
98+
99+
dataArr.set(ind, y.dataArr.get(t - 1));
100+
numOfKeys++;
101+
}
102+
103+
public void printInOrder() {
104+
int i;
105+
for (i = 0; i < numOfKeys; i++) {
106+
if (!isLeaf) {
107+
children.get(i).printInOrder();
108+
}
109+
System.out.print(dataArr.get(i) + " ");
110+
}
111+
if (!isLeaf) {
112+
children.get(i).printInOrder();
113+
}
114+
}
115+
}

students/23K0120/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<module>23K0120-p29</module>
4646
<module>23K0120-p30</module>
4747
<module>23K0120-p30-2</module>
48+
<module>23K0120-p31</module>
4849
<module>23K0120-p32</module>
4950
</modules>
5051
</project>

0 commit comments

Comments
 (0)