Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- class Node {
- boolean leaf=true;
- Node parent=null;
- ArrayList<Integer> keys = new ArrayList<>();
- ArrayList<Node> sons = new ArrayList<>();
- public Node() {
- }
- public Node(int key) {
- keys.add(key);
- }
- }
- class bTree {
- Node root;
- public int stopien;
- public bTree(int value,int stopien) {
- this.stopien = stopien;
- this.root = new Node(value);
- }
- public void addValue(int value){
- addCurr(value,root);
- }
- public void print(){
- printR(root);
- }
- public void printR(Node curr){
- System.out.print( curr + " klucze: ");
- for(int i=0;i<curr.keys.size();i++){
- System.out.print(curr.keys.get(i) + " ");
- }
- System.out.print(" parent= " + curr.parent + " dzieci: ");
- for(int i=0;i<curr.sons.size();i++){
- System.out.print(curr.sons.get(i) + " ");
- }
- System.out.println(" ");
- for(int i=0; i< curr.sons.size();i++){
- printR(curr.sons.get(i));
- }
- }
- private void fixTree(int value,Node curr){
- if(curr.sons.size() >0){
- curr.leaf = false;
- }
- if(curr.keys.size() > 2*stopien-1){
- if(curr.parent != null){
- curr.parent.keys.add(curr.keys.get(stopien-1));
- Node left = new Node();
- curr.parent.sons.remove(curr);
- curr.parent.sons.add(left);
- left.parent = curr.parent;
- Node right = new Node();
- left.parent = curr.parent;
- curr.parent.sons.add(right);
- right.parent = curr.parent;
- for(int i=0;i<curr.keys.size();i++){
- if(i < stopien-1){
- left.keys.add(curr.keys.get(i));
- } else if (i > stopien-1){
- right.keys.add(curr.keys.get(i));
- }
- }
- for(int i=0;i<curr.sons.size();i++){
- if(curr.sons.get(i).keys.get(0) <= curr.keys.get(stopien-1)){
- left.sons.add(curr.sons.get(i));
- curr.sons.get(i).parent = left;
- }
- else {
- right.sons.add(curr.sons.get(i));
- curr.sons.get(i).parent = right;
- }
- }
- if(value <= curr.keys.get(stopien-1)){
- left.keys.add(value);
- }else{
- right.keys.add(value);
- }
- Collections.sort(curr.parent.keys);
- Collections.sort(left.keys);
- Collections.sort(right.keys);
- fixTree(value,root);
- } else{
- curr.parent = new Node(curr.keys.get(stopien-1));
- curr.parent.leaf = false;
- System.out.println("reeee");
- root = curr.parent;
- Node left = new Node();
- curr.parent.sons.add(left);
- left.parent = curr.parent;
- Node right = new Node();
- left.parent = curr.parent;
- curr.parent.sons.add(right);
- right.parent = curr.parent;
- for(int i=0;i<curr.keys.size();i++){
- if(i < stopien-1){
- left.keys.add(curr.keys.get(i));
- } else if (i > stopien-1){
- right.keys.add(curr.keys.get(i));
- }
- }
- for(int i=0;i<curr.sons.size();i++){
- if(curr.sons.get(i).keys.get(0) <= curr.keys.get(stopien-1)){
- left.sons.add(curr.sons.get(i));
- curr.sons.get(i).parent = left;
- }
- else {
- right.sons.add(curr.sons.get(i));
- curr.sons.get(i).parent = right;
- }
- }
- Collections.sort(curr.parent.keys);
- Collections.sort(left.keys);
- Collections.sort(right.keys);
- fixTree(value,root);
- }
- }
- else{
- for(int i=0;i<curr.sons.size();i++){
- fixTree(value,curr.sons.get(i));
- }
- }
- }
- private void addCurr(int value, Node curr){
- if(curr.sons.size() >0){
- curr.leaf = false;
- }
- if(curr.leaf){
- if(curr.keys.size() <= 2*stopien-2) {
- curr.keys.add(value);
- Collections.sort(curr.keys);
- return;
- } else {
- if(curr.keys.size() > 2*stopien-2){
- if(curr.parent != null){
- curr.parent.keys.add(curr.keys.get(stopien-1));
- Node left = new Node();
- curr.parent.sons.remove(curr);
- curr.parent.sons.add(left);
- left.parent = curr.parent;
- Node right = new Node();
- left.parent = curr.parent;
- curr.parent.sons.add(right);
- right.parent = curr.parent;
- for(int i=0;i<curr.keys.size();i++){
- if(i < stopien-1){
- left.keys.add(curr.keys.get(i));
- } else if (i > stopien-1){
- right.keys.add(curr.keys.get(i));
- }
- }
- for(int i=0;i<curr.sons.size();i++){
- if(curr.sons.get(i).keys.get(0) <= curr.keys.get(stopien-1)){
- left.sons.add(curr.sons.get(i));
- curr.sons.get(i).parent = left;
- }
- else {
- right.sons.add(curr.sons.get(i));
- curr.sons.get(i).parent = right;
- }
- }
- if(value <= curr.keys.get(stopien-1)){
- left.keys.add(value);
- }else{
- right.keys.add(value);
- }
- Collections.sort(curr.parent.keys);
- Collections.sort(left.keys);
- Collections.sort(right.keys);
- fixTree(value,root);
- } else{
- curr.parent = new Node(curr.keys.get(stopien-1));
- curr.parent.leaf = false;
- System.out.println("reeee");
- root = curr.parent;
- Node left = new Node();
- curr.parent.sons.add(left);
- left.parent = curr.parent;
- Node right = new Node();
- left.parent = curr.parent;
- curr.parent.sons.add(right);
- right.parent = curr.parent;
- for(int i=0;i<curr.keys.size();i++){
- if(i < stopien-1){
- left.keys.add(curr.keys.get(i));
- } else if (i > stopien-1){
- right.keys.add(curr.keys.get(i));
- }
- }
- for(int i=0;i<curr.sons.size();i++){
- if(curr.sons.get(i).keys.get(0) <= curr.keys.get(stopien-1)){
- left.sons.add(curr.sons.get(i));
- curr.sons.get(i).parent = left;
- }
- else {
- right.sons.add(curr.sons.get(i));
- curr.sons.get(i).parent = right;
- }
- }
- if(value <= curr.keys.get(stopien-1)){
- left.keys.add(value);
- }else{
- right.keys.add(value);
- }
- Collections.sort(curr.parent.keys);
- Collections.sort(left.keys);
- Collections.sort(right.keys);
- fixTree(value,root);
- }
- }
- }
- }else {
- for(int i=0;i<curr.keys.size();i++){
- if(value <= curr.keys.get(i)){
- addCurr(value, curr.sons.get(i));
- break;
- }
- }
- addCurr(value,curr.sons.get(curr.sons.size()-1));
- }
- }}
- public class Main {
- public static void main(String[] args) {
- bTree xd = new bTree(15,4);
- xd.addValue(16);xd.addValue(17);xd.addValue(18);xd.addValue(19);xd.addValue(20);xd.addValue(21);xd.addValue(22);xd.addValue(23);
- xd.addValue(24);xd.addValue(25);xd.addValue(26);
- // xd.addValue(7);
- xd.print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement