package com.PhTree;
import com.Tree.ErTree;
import java.util.ArrayList;
import java.util.List;
public class PHTree {
class TreeLink {
int data; // 节点中的数据
int height; // 节点的高度
TreeLink left; // 左儿子
TreeLink right; // 右儿子
TreeLink(int data) {
this( data, null, null );
}
TreeLink(int data, TreeLink left, TreeLink right ) {
this.data = data;
this.left = left;
this.right = right;
this.height = 0;
}
@Override
public String toString() {
return "TreeLink [data=" + data + ", height=" + height + ", left=" + left + ", right=" + right + "]";
}
}
private TreeLink root;
public void add(int e){
root=insert(new TreeLink(e), root);
}
//节点的插入操作
private TreeLink insert(TreeLink next, TreeLink node ) {
//如果node为空,则把next节点成功插入
if(node == null) {
node = next;
return node;
}
int compareResult = next.data - node.data;
if( compareResult <= 0 ) {
node.left = insert( next, node.left );//将next插入左子树中
if( height( node.left ) - height( node.right ) == 2 )//打破平衡
if( next.data - node.left.data < 0 )//LL型(左左型)
node = rotateWithLeftChild( node );
else //LR型(左右型)
node = doubleWithLeftChild( node );
}
else if( compareResult > 0 ) {
node.right = insert( next, node.right );//将next插入右子树中
if( height( node.right ) - height( node.left ) == 2 )//打破平衡
if( next.data - node.right.data > 0 )//RR型(右右型)
node = rotateWithRightChild( node );
else //RL型 (右左型)
node = doubleWithRightChild( node );
}
node.height = Math.max( height( node.left ), height( node.right ) ) + 1;//更新高度
return node;
}
//求节点的高度
private int height(TreeLink t) {
return t == null ? -1 : t.height;
}
//单旋转,适用于LL型
private TreeLink rotateWithLeftChild(TreeLink k2) {
TreeLink k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = Math.max( height( k2.left ), height( k2.right ) ) + 1;
k1.height = Math.max( height( k1.left ), k2.height ) + 1;
return k1;
}
//单旋转,适用于RR型
private TreeLink rotateWithRightChild(TreeLink k1) {
TreeLink k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = Math.max( height( k1.left ), height( k1.right ) ) + 1;
k2.height = Math.max( height( k2.right ), k1.height ) + 1;
return k2;
}
//双旋转,适用于LR型
private TreeLink doubleWithLeftChild(TreeLink k3) {
k3.left = rotateWithRightChild( k3.left );
return rotateWithLeftChild( k3 );
}
//双旋转,适用于RL型
private TreeLink doubleWithRightChild(TreeLink k1){
k1.right = rotateWithLeftChild( k1.right );
return rotateWithRightChild( k1 );
}
/**
* 前序對外
* @return
*/
public List frondData(){
list=new ArrayList();
if(root!=null)
frondRecursion(this.root);
return list;
}
/**
* 前序 递归 根左右
*/
private void frondRecursion(TreeLink root){
if(root!=null){
list.add(root.data);
frondRecursion(root.left);
frondRecursion(root.right);
}
}
/**
* 中序對外
* @return
*/
public List inData(){
list=new ArrayList();
if(root!=null)
inRecursion(this.root);
return list;
}
/**
*中序 递归 左根右
*/
List list=null;
private void inRecursion(TreeLink root){
if(root!=null){
inRecursion(root.left);
list.add(root.data);
inRecursion(root.right);
}
}
/**
* 后序對外
* @return
*/
public List afterData(){
list=new ArrayList();
if(root!=null)
afterRecursion(this.root);
return list;
}
/**
* 後序遍歷 左右根
* @param root
*/
private void afterRecursion(TreeLink root){
if(root!=null){
afterRecursion(root.left);
afterRecursion(root.left);
list.add(root.data);
}
}
@Override
public String toString() {
return "TreeLink [data=" + root + ", height=" + root + ", left=" + root + ", right=" + root + "]";
}
}
输出数组分段
package com.PhTree;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
PHTree pHTree = new PHTree();
int num[] = {50,51,53,52,54,56,44,65,98,54,21,15,36,45,46,48,49,59,6,3,5,1,2,9,8,99,100};
for(int i=1; i<num.length ; i++) {
pHTree.add(num[i]);
}
List<Integer> list = (List<Integer>)pHTree.inData();
list.add(-999999999);
List rl=new ArrayList();
int t=0,z = 0;
boolean ks=false;
for(int i : list){
if(!ks){
t=i;
ks=true;
}else if (z+1!=i){
rl.add(t+"-"+z);
t=i;
}
z=i;
}
System.out.println(list);
System.out.println(rl);
}
}