package com.test;
import java.util.*;
/**
-
@ClassName TreeTwoThree * @Description TODO
-
@Author M
-
@Date 22:32 2022/12/18
-
@Version 1.0 **/
public class TreeTwoThree {
public static void main(String[] args) {
TTTree<Integer> tree = new TTTree<>();
for(Integer data : Arrays.asList(4,3,6,5,7,8,9,10,11)){
tree.add(data);
}
System.out.println(tree.root.items);
System.out.println(tree.indexList());
}
}
class TTTree<T extends Comparable<T>>{
static class Node<T extends Comparable<T>>{
/**
* 存储节点元素值
* */
List<T> items= new ArrayList<>(3);
/**
* 存储子节点
* ***/
List<Node> children=new ArrayList<>(3);
/**
* 父亲节点
* ***/
Node<T> parent;
public Node<T> searchBeLongToItems(T item){
/***
* 没有子节点,就临时插入到该节点
* **/
if(children.size()==0){
return this;
}
Node<T> pNode=null;
for (int i = 0; i <items.size() ; i++) {
T t = items.get(i);
int cmp=item.compareTo(t);
//找到值相等的情况
if(cmp==0){
pNode=this;
break;
}
if(cmp<0){
pNode=children.get(i).searchBeLongToItems(item);
break;
}
}
//属于最大区间的情况
if(null==pNode&&item.compareTo(items.get(items.size()-1))>0){
pNode=children.get(children.size()-1).searchBeLongToItems(item);
}
return pNode;
}
/**
* 中序遍历
* @author M
* @date 2022/12/19 0:04
* @param
* @return
* @Version1.0
**/
public void infixList(List<T> dst){
if(this.children.size()==3){
this.children.get(0).infixList(dst);
dst.add(this.items.get(0));
this.children.get(1).infixList(dst);
dst.add(this.items.get(1));
this.children.get(2).infixList(dst);
}
else if(this.children.size()==2){
this.children.get(0).infixList(dst);
dst.add(this.items.get(0));
this.children.get(1).infixList(dst);
}
else{
dst.add(this.items.get(0));
}
}
}
Node<T> root;
/**
* 添加节点
* @author M
* @date 2022/12/18 23:25
* @param
* @return
* @Version1.0
**/
public void add(T item){
if(this.root==null){
this.root=new Node<>();
this.root.items.add(item);
}else {
Node<T> tNode = this.root.searchBeLongToItems(item);
boolean flag=false;
for(int i=0;i<tNode.items.size();i++){
if(item.compareTo(tNode.items.get(i)) == 0){
flag = true;
break;
}
}
if(flag==true){
return;
}
tNode.items.add(item);
reBalance(tNode);
}
}
//平衡树
public void reBalance(Node<T> node){
if(node.items.size()==2){
//3节点
Collections.sort(node.items);
}else {
//4节点需要调整
Collections.sort(node.items);
Node<T> left =new Node<>();
left.items.add(node.items.get(0));
Node<T> right=new Node<>();
right.items.add(node.items.get(2));
//判断该节点有子节点的情况
if(node.children.size()>0){
left.children.add(node.children.get(0));
left.children.add(node.children.get(1));
right.children.add(node.children.get(2));
right.children.add(node.children.get(3));
node.children.get(0).parent=left;
node.children.get(1).parent=left;
node.children.get(2).parent=right;
node.children.get(3).parent=right;
}
//处理父节点
if(node.parent==null){
this.root=new Node<>();
this.root.items.add(node.items.get(1));
this.root.children.add(left);
left.parent=this.root;
this.root.children.add(right);
right.parent=this.root;
}else if(node.parent.items.size()==1){
node.parent.items.add(node.items.get(1));
Collections.sort(node.parent.items);
left.parent=right.parent=node.parent;
//判断当前节点是父亲的左节点还是右节点
//左节点
if(node.parent.children.get(0)==node){
node.parent.children.set(0,left);
node.parent.children.add(1,right);
}else {
//右节点
node.parent.children.set(1,left);
node.parent.children.add(right);
}
}else{
//3节点的情况
node.parent.items.add(node.items.get(1));
left.parent=right.parent= node.parent;
//判断当前节点属于父亲的哪个节点
if(node.parent.children.get(0)==node){
node.parent.children.set(0,left);
node.parent.children.add(1,right);
}else if(node.parent.children.get(1)==node){
node.parent.children.set(1,left);
node.parent.children.add(2,right);
}else {
node.parent.children.set(2,left);
node.parent.children.add(right);
}
reBalance(node.parent);
}
}
}
//中序遍历
public List<T> indexList(){
List<T> rst = new ArrayList<>();
this.root.infixList(rst);
return rst;
}
}