数据结构探险篇 六.平衡二叉树的实现

数据结构

节点类
public class SearchTreeNode <T> {

	SearchTreeNode <T> pTree;//双亲
	SearchTreeNode <T> lChildTree;//左子树
	SearchTreeNode <T> rChildTree;//右子树
	int deep;//以改节点为根的树的深度
	int bf;//平衡因子  -1代表左子树深了一层 	+1代表右子树深了一层
	int id;//关键字
	T data;//数据
	
	public SearchTreeNode(int id,T d) {
		pTree = null;
		lChildTree = null;
		rChildTree = null;
		bf = 0;
		deep = 1;
		this.id = id;
		data = d;
	}
}
平衡二叉树类
public class SearchTree <T> {

	private SearchTreeNode<T> root;//根节点
	private int creaseId;//自增的关键字
	private int len;//当前树的节点数
	private int floor;//当前树的层数
	
	public SearchTree() {
		root = null;
		len = 0;
		floor = 0;
		creaseId = 0;
	}
}

api

向树中添加一个节点
public boolean addNode(T data){
	SearchTreeNode<T> Node = new SearchTreeNode<T>(creaseId++, data);
	return  addNode(Node);
}

public boolean addNode(SearchTreeNode<T> Node){
	//树为空则将此节点赋为根节点
	if(len == 0){
		root = Node;
		len++;
		return true;
	}
	
	//获取添加的节点的父节点
	SearchTreeNode<T> pNode = getParentNode(Node.getId());
	Node.setpTree(pNode);
	
	//判断此节点是父节点的左子树还是右子树
	if(pNode.getId() > Node.getId()){
		//System.out.println(Node.getData()+"是"+pNode.getData()+"节点的左子树");
		
		//父节点的左子树
		pNode.lChildTree = Node;
		constructBalance(pNode, 0);
		//pNode.setBf(pNode.getBf() - 1);
	}else{
		//System.out.println(Node.getData()+"是"+pNode.getData()+"节点的右子树");
		
		//父节点的右子树
		pNode.rChildTree = Node;
		constructBalance(pNode, 1);
		//pNode.setBf(pNode.getBf() + 1);
	}
	
	//更新节点数与层数
	len++;
	return true;
}
构建平衡树的递归函数
//parentNode为影响的树的根节点
//flag为0代表添加左子树,为1代表添加右子树
public void constructBalance(SearchTreeNode<T> parentNode,int flag){
	
	//旋转分四钟情况
	//1.左子树过深时,左子树的平衡因子为 -1,此时直接将左子树进行右旋
	//2.右子树过深时,右子树的平衡因子为 1,此时直接将右子树进行左旋
	//3.左子树过深时,左子树的平衡因子为 1,此时先将左子树的右子树先进行左旋,再进行右旋
	//4.右子树过深时,右子树的平衡因子为 -1,此时先将右子树的左子树先进行右旋,再进行左旋
	SearchTreeNode<T> now = parentNode;
	SearchTreeNode<T> nowL = parentNode.getlChildTree();
	SearchTreeNode<T> nowR = parentNode.getrChildTree();		
	
	if(flag == 0){
		//添加左节点
		if(parentNode.getBf() >= 0){
			//在平衡范围内,平衡因子-1
			parentNode.setBf(now.getBf() - 1);
		}else{
			//判断左子树的平衡因子
			if(-1 == nowL.getBf()){
				//进行右旋转
				rightRotate(nowL);
			}else if(1 == nowL.getBf()){
				SearchTreeNode<T> temp = nowL.getrChildTree();
				//先进行左旋转,再进行右旋转
				leftRotate(temp);
				rightRotate(temp);
			}
			//修改平衡因子
			now.setBf(0);
			nowL.setBf(0);
			
			return ;
		}
	}else if(flag == 1){
		//添加右节点
		if(parentNode.getBf() <= 0){
			//在平衡范围内,平衡因子+1
			parentNode.setBf(now.getBf() + 1);
		}else{
			//判断右子树的平衡因子
			if(1 == nowR.getBf()){
				//进行左旋转
			 	leftRotate(nowR);	
			}else if(-1 == nowR.getBf()){
				SearchTreeNode<T> temp = nowR.getlChildTree();
				//先进行右旋转,再进行左旋转
				rightRotate(temp);
				leftRotate(temp);
			}
			
			//修改平衡因子
			now.setBf(0);
			nowR.setBf(0);
			return ;
		}
	}
	
	//判断parentNode节点是它父节点的左子树还是右子树
	if(now.getpTree() == null){
		return ;
	}
	
	if(now.getpTree().getlChildTree() == parentNode){
		//是左子树
		constructBalance(now.getpTree(), 0);
	}else{
		//是右子树
		constructBalance(now.getpTree(),1);
	}
	
	
}	
节点的左旋转
public void leftRotate(SearchTreeNode<T> Node){
	if(Node == root){
		return;
	}
	//旋转节点的爷爷节点
	SearchTreeNode<T> ppTreeNode = Node.getpTree().getpTree();
	//旋转节点的父节点
	SearchTreeNode<T> pTreeNode = Node.getpTree();
	//旋转节点的左节点
	SearchTreeNode<T> lTreeNode = Node.getlChildTree();
	
	//判断父节点是爷爷节点的左节点还是右节点
	if(ppTreeNode != null){
		if(ppTreeNode.getlChildTree() == pTreeNode){
			//将爷爷节点的左节点设置为当前节点
			ppTreeNode.setlChildTree(Node);
		}else{
			//将爷爷节点的右节点设置为当前节点
			ppTreeNode.setrChildTree(Node);
		}
	}else{
		//爷爷节点为空,证明父节点为根节点
		//将根节点设置为旋转节点
		root = Node;
	}
	//将爷爷节点设为旋转节点的父节点
	Node.setpTree(ppTreeNode);
	//将父节点的右节点设为旋转节点的左节点
	if(lTreeNode != null){
		//将旋转节点的左节点的父节点设置为父节点
		lTreeNode.setpTree(pTreeNode);
	}
	pTreeNode.setrChildTree(lTreeNode);
	//将父节点的父节点设为旋转节点
	pTreeNode.setpTree(Node);
	//将旋转节点的左节点设为父节点
	Node.setlChildTree(pTreeNode);
}
节点的右旋转
public void rightRotate(SearchTreeNode<T> Node){
	if(Node == root){
		return;
	}
	//旋转节点的爷爷节点
	SearchTreeNode<T> ppTreeNode = Node.getpTree().getpTree();
	//旋转节点的父节点
	SearchTreeNode<T> pTreeNode = Node.getpTree();
	//旋转节点的左节点
	SearchTreeNode<T> rTreeNode = Node.getrChildTree();
	
	//判断父节点是爷爷节点的左节点还是右节点
	if(ppTreeNode != null){
		if(ppTreeNode.getlChildTree() == pTreeNode){
			//将爷爷节点的左节点设置为当前节点
			ppTreeNode.setlChildTree(Node);
		}else{
			//将爷爷节点的右节点设置为当前节点
			ppTreeNode.setrChildTree(Node);
		}
	}else{
		//爷爷节点为空,证明父节点为根节点
		//将根节点设置为旋转节点
		root = Node;
	}
	//将爷爷节点设为旋转节点的父节点
	Node.setpTree(ppTreeNode);
	//将父节点的左节点设为旋转节点的右节点
	if(rTreeNode != null){
		//将旋转节点的右节点的父节点设置为父节点
		rTreeNode.setpTree(pTreeNode);
	}
	pTreeNode.setlChildTree(rTreeNode);
	//将父节点的父节点设为旋转节点
	pTreeNode.setpTree(Node);
	//将旋转节点的右节点设为父节点
	Node.setrChildTree(pTreeNode);
}
前序遍历某树
public void preTraver(SearchTreeNode<T> Node){
	if(Node != null){
		System.out.print(Node.getData());
	}
	
	if(Node.getlChildTree() != null){
		preTraver(Node.getlChildTree());
	}
	
	if(Node.getrChildTree() != null){
		preTraver(Node.getrChildTree());
	}
}
中序遍历树
public void midTraver(SearchTreeNode<T> Node){
	if(Node.getlChildTree() != null){
		midTraver(Node.getlChildTree());
	}
	
	if(Node != null){
		System.out.print(Node.getData());
	}
	
	if(Node.getrChildTree() != null){
		midTraver(Node.getrChildTree());
	}
}
后序遍历树
public void afterTraver(SearchTreeNode<T> Node){
	if(Node != null){
		//System.out.println("now :"+Node.getData());
		if(Node.getlChildTree() != null){
			afterTraver(Node.getlChildTree());
		}
		
		if(Node.getrChildTree() != null){
			afterTraver(Node.getrChildTree());
		}
		
		System.out.print(Node.getData());
	}
}
根据关键字查找某一个节点
public SearchTreeNode<T> getNode(int id){
	//根据完全二叉树节点有序的性质去查找某一结点
	//1.从底向上查找,从根节点到目标节点的访问路径
	int nowId = root.getId();
	//判断当前查找的节点是否为根节点
	if(nowId == id){
		return root;
	}
	
	SearchTreeNode<T> temp = root;
	while(nowId != id){
		//判断是左节点还是右节点
		if(id > nowId){
			temp = temp.rChildTree;
		}else{
			temp = temp.lChildTree;
		}
		
		if(temp == null){
			System.out.println("该节点未找到");
			return null;
		}
		
		nowId = temp.getId();
	}
	
	return temp;
}
根据关键字给想插入的某新一结点,查找符合条件的父节点
public SearchTreeNode<T> getParentNode(int id){
	SearchTreeNode<T> temp = root;
	
	
	//树判空
	if(len <= 0){
		return null;
	}
	
	
	
	while(true){
		if(temp.getId() > id){
			//比父节点小,则去看父节点的左节点是否为空
			//为空则正好插入,该节点为目标节点
			if(temp.getlChildTree() == null){
				return temp;
			}else{
				temp = temp.getlChildTree();
			}
		}else{
			//比父节点大,则去看父节点的右节点是否为空
			//为空则正好插入,该节点为目标节点
			if(temp.getrChildTree() == null){
				return temp;
			}else{
				temp = temp.getrChildTree();
			}
		}
	}
}

全部代码

package book_1_3;


public class SearchTree <T> {

	private SearchTreeNode<T> root;//根节点
	private int creaseId;//自增的关键字
	private int len;//当前树的节点数
	private int floor;//当前树的层数
	
	public SearchTree() {
		root = null;
		len = 0;
		floor = 0;
		creaseId = 0;
	}
	
	//向树中添加一个节点
	public boolean addNode(T data){
		SearchTreeNode<T> Node = new SearchTreeNode<T>(creaseId++, data);
		return  addNode(Node);
	}
	
	public boolean addNode(SearchTreeNode<T> Node){
		//树为空则将此节点赋为根节点
		if(len == 0){
			root = Node;
			len++;
			return true;
		}
		
		//获取添加的节点的父节点
		SearchTreeNode<T> pNode = getParentNode(Node.getId());
		Node.setpTree(pNode);
		
		//判断此节点是父节点的左子树还是右子树
		if(pNode.getId() > Node.getId()){
			//System.out.println(Node.getData()+"是"+pNode.getData()+"节点的左子树");
			
			//父节点的左子树
			pNode.lChildTree = Node;
			constructBalance(pNode, 0);
			//pNode.setBf(pNode.getBf() - 1);
		}else{
			//System.out.println(Node.getData()+"是"+pNode.getData()+"节点的右子树");
			
			//父节点的右子树
			pNode.rChildTree = Node;
			constructBalance(pNode, 1);
			//pNode.setBf(pNode.getBf() + 1);
		}
		
		//更新节点数与层数
		len++;
		//floor = (int)(Math.log(len)/Math.log(2)) + 1;
		return true;
				
				
				
				
				
//		//树为空则将此节点赋为根节点
//		if(len == 0){
//			root = Node;
//			len++;
//			return true;
//		}
//		//获取添加的节点的父节点
//		SearchTreeNode<T> pNode = getParentNode(Node.getId());
//		Node.setpTree(pNode);
//		
//		//判断此节点是父节点的左子树还是右子树
//		if(pNode.getId() > Node.getId()){
//			//父节点的左子树
//			pNode.lChildTree = Node;
//			pNode.setBf(pNode.getBf() - 1);
//		}else{
//			//父节点的右子树
//			pNode.rChildTree = Node;
//			pNode.setBf(pNode.getBf() + 1);
//		}
//		
//		//更新节点数与层数
//		len++;
//		//floor = (int)(Math.log(len)/Math.log(2)) + 1;
//		return true;
	}

	//构建平衡树的递归函数
	//parentNode为影响的树的根节点
	//flag为0代表添加左子树,为1代表添加右子树
	public void constructBalance(SearchTreeNode<T> parentNode,int flag){
		
		//旋转分四钟情况
		//1.左子树过深时,左子树的平衡因子为 -1,此时直接将左子树进行右旋
		//2.右子树过深时,右子树的平衡因子为 1,此时直接将右子树进行左旋
		//3.左子树过深时,左子树的平衡因子为 1,此时先将左子树的右子树先进行左旋,再进行右旋
		//4.右子树过深时,右子树的平衡因子为 -1,此时先将右子树的左子树先进行右旋,再进行左旋
		SearchTreeNode<T> now = parentNode;
		SearchTreeNode<T> nowL = parentNode.getlChildTree();
		SearchTreeNode<T> nowR = parentNode.getrChildTree();		
		
		if(flag == 0){
			//添加左节点
			if(parentNode.getBf() >= 0){
				//在平衡范围内,平衡因子-1
				parentNode.setBf(now.getBf() - 1);
			}else{
				//判断左子树的平衡因子
				if(-1 == nowL.getBf()){
					//进行右旋转
					rightRotate(nowL);
				}else if(1 == nowL.getBf()){
					SearchTreeNode<T> temp = nowL.getrChildTree();
					//先进行左旋转,再进行右旋转
					leftRotate(temp);
					rightRotate(temp);
				}
				//修改平衡因子
				now.setBf(0);
				nowL.setBf(0);
				
				return ;
			}
		}else if(flag == 1){
			//添加右节点
			if(parentNode.getBf() <= 0){
				//在平衡范围内,平衡因子+1
				parentNode.setBf(now.getBf() + 1);
			}else{
				//判断右子树的平衡因子
				if(1 == nowR.getBf()){
					//进行左旋转
				 	leftRotate(nowR);	
				}else if(-1 == nowR.getBf()){
					SearchTreeNode<T> temp = nowR.getlChildTree();
					//先进行右旋转,再进行左旋转
					rightRotate(temp);
					leftRotate(temp);
				}
				
				//修改平衡因子
				now.setBf(0);
				nowR.setBf(0);
				return ;
			}
		}
		
		//判断parentNode节点是它父节点的左子树还是右子树
		if(now.getpTree() == null){
			return ;
		}
		
		if(now.getpTree().getlChildTree() == parentNode){
			//是左子树
			constructBalance(now.getpTree(), 0);
		}else{
			//是右子树
			constructBalance(now.getpTree(),1);
		}
		
		
	}

	//删除某一节点后构建二叉树
	public void constructBalance_afterDelete(SearchTreeNode<T> now) {
		
	}
	
	//删除某一节点
	public void delNode(int id){
		SearchTreeNode<T> targe = getNode(id);

		//当只有一个节点时,直接置空树
		if(len == 1){
			root = null;
			len = 0;
		}
		
		boolean is_root = false;//是否根节点
		boolean is_leftChild = true;//是否左孩子
		int child_num = 0;//孩子的数量
		SearchTreeNode<T> nowL = targe.getlChildTree();
		SearchTreeNode<T> nowR = targe.getrChildTree();
		SearchTreeNode<T> nowP = targe.getpTree();
		SearchTreeNode<T> temp;
		
		//判断删除的是否是根节点
		if(root == targe){
			is_root = true;
		}
		
		//判断删除节点是它父节点的左子树还是右子树
		if(targe.getpTree().getrChildTree() == targe){
			is_leftChild = false;
		}
		
		//判断删除节点有几个孩子
		if(null != targe.getlChildTree()){
			child_num++;
		}
		if(null != targe.getrChildTree()){
			child_num++;
		}
	
		//根据孩子的数量确定删除的策略
		switch (child_num) {
			case 1:
				//自己拿唯一的孩子代替自己的位置
					
				if(null != nowL){
					//目标节点仅有左孩子
					
					//把孩子节点的父节点设为目标节点的父节点
					nowL.setpTree(nowL.getpTree());
					//把目标节点的父节点的子节点设为左孩子节点
					if( !is_root){
						if(is_leftChild){
							nowP.setlChildTree(nowL);
							//判断左子树缩短以后右子树会不会过长
							if(nowP.getBf() == 1){
								//将目标节点的右节点左旋
								leftRotate(nowP.getrChildTree());
								constructBalance_afterDelete(nowP.getpTree());
							}else if(nowP.getBf() == -1){
								constructBalance_afterDelete(nowP);
							}
						}else{
							nowP.setrChildTree(nowL);
							//判断右子树缩短以后左子树会不会过长
							if(nowP.getBf() == -1){
								//将目标节点的左节点右旋
								rightRotate(nowP.getlChildTree());
								constructBalance_afterDelete(nowP.getpTree());
							}else if(nowP.getBf() == -1){
								constructBalance_afterDelete(nowP);
							}
						}
						
					}else{
						root = nowL;						
					}
					
				}else{
					//目标节点仅有右孩子
					
					//把孩子节点的父节点设为目标节点的父节点
					nowR.setpTree(nowP);
					//把目标节点的父节点的子节点设为右孩子节点
					if( !is_root){
						if(is_leftChild){
							nowP.setlChildTree(nowL);
							//判断左子树缩短以后右子树会不会过长
							if(nowP.getBf() == 1){
								//将目标节点的右节点左旋
								leftRotate(nowP.getrChildTree());
								constructBalance_afterDelete(nowP.getpTree());
							}else if(nowP.getBf() == -1){
								constructBalance_afterDelete(nowP);
							}
						}else{
							nowP.setrChildTree(nowL);
						}
						
					}else{
						root = nowR;						
					}
				}
				break;
			case 2:
				
				break;
				
			case 0:
	
				break;
		}
	}

	
	
	//检查是否为二叉平衡树,如果不是,则找出最小的二叉非平衡树,将其平衡
	public void checkBalance(SearchTreeNode <T> now){
		if(null == now || len <= 1 || (null == now.getlChildTree() && null == now.getrChildTree())){
			return ;
		}
		
		//该树是平衡二叉树,继续判断左右子树是不是平衡二叉树
		checkBalance(now.lChildTree);
		checkBalance(now.rChildTree);
		
		int deepL = 0;//左子树的深度
		int deepR = 0;//右子树的深度
		
		if(now.getlChildTree() != null){
			deepL = now.getlChildTree().getDeep();
		}

		if(null != now.getrChildTree()){
			deepR = now.getrChildTree().getDeep();
		}
		
		int deepDiff = Math.abs(deepL - deepR);
		//判断是否为平衡二叉树
		if( 1 >= deepDiff){
			//该树不是平衡二叉树
			
			SearchTreeNode <T> nowL = now.getlChildTree();
			SearchTreeNode <T> nowR = now.getrChildTree();
			
			if(deepL > deepR){
				//左子树比右子树深
				
				deepL = 0;
				deepR = 0;
				if(nowL.getlChildTree() != null){
					deepL = now.getlChildTree().getDeep();
				}

				if(null != nowL.getrChildTree()){
					deepR = now.getrChildTree().getDeep();
				}
				
				if(deepL > deepR){
					//左子树的左子树比左子树的右子树深
					//直接将左子树右旋转
					rightRotate(nowL);
				}else{
					//先将左子树的右子树左旋转,再右旋转
					leftRotate(nowL.getrChildTree());
					rightRotate(nowL.getrChildTree());
				}
			}else{
				//右子树比左子树深
				
				deepL = 0;
				deepR = 0;
				if(nowR.getlChildTree() != null){
					deepL = now.getlChildTree().getDeep();
				}

				if(null != nowR.getrChildTree()){
					deepR = now.getrChildTree().getDeep();
				}
				
				if(deepR > deepL){
					//右子树的右子树比右子树的左子树深
					//直接将右子树左旋转
					leftRotate(nowR);
				}else{
					//先将右子树的左子树右旋转,再左旋转
					rightRotate(nowR.getlChildTree());
					leftRotate(nowR.getlChildTree());
				}
			}
			
		}
	}
	
	
	//节点的左旋转
	public void leftRotate(SearchTreeNode<T> Node){
		if(Node == root){
			return;
		}
		//旋转节点的爷爷节点
		SearchTreeNode<T> ppTreeNode = Node.getpTree().getpTree();
		//旋转节点的父节点
		SearchTreeNode<T> pTreeNode = Node.getpTree();
		//旋转节点的左节点
		SearchTreeNode<T> lTreeNode = Node.getlChildTree();
		
		//判断父节点是爷爷节点的左节点还是右节点
		if(ppTreeNode != null){
			if(ppTreeNode.getlChildTree() == pTreeNode){
				//将爷爷节点的左节点设置为当前节点
				ppTreeNode.setlChildTree(Node);
			}else{
				//将爷爷节点的右节点设置为当前节点
				ppTreeNode.setrChildTree(Node);
			}
		}else{
			//爷爷节点为空,证明父节点为根节点
			//将根节点设置为旋转节点
			root = Node;
		}
		//将爷爷节点设为旋转节点的父节点
		Node.setpTree(ppTreeNode);
		//将父节点的右节点设为旋转节点的左节点
		if(lTreeNode != null){
			//将旋转节点的左节点的父节点设置为父节点
			lTreeNode.setpTree(pTreeNode);
		}
		pTreeNode.setrChildTree(lTreeNode);
		//将父节点的父节点设为旋转节点
		pTreeNode.setpTree(Node);
		//将旋转节点的左节点设为父节点
		Node.setlChildTree(pTreeNode);
	}

	//节点的右旋转
	public void rightRotate(SearchTreeNode<T> Node){
		if(Node == root){
			return;
		}
		//旋转节点的爷爷节点
		SearchTreeNode<T> ppTreeNode = Node.getpTree().getpTree();
		//旋转节点的父节点
		SearchTreeNode<T> pTreeNode = Node.getpTree();
		//旋转节点的左节点
		SearchTreeNode<T> rTreeNode = Node.getrChildTree();
		
		//判断父节点是爷爷节点的左节点还是右节点
		if(ppTreeNode != null){
			if(ppTreeNode.getlChildTree() == pTreeNode){
				//将爷爷节点的左节点设置为当前节点
				ppTreeNode.setlChildTree(Node);
			}else{
				//将爷爷节点的右节点设置为当前节点
				ppTreeNode.setrChildTree(Node);
			}
		}else{
			//爷爷节点为空,证明父节点为根节点
			//将根节点设置为旋转节点
			root = Node;
		}
		//将爷爷节点设为旋转节点的父节点
		Node.setpTree(ppTreeNode);
		//将父节点的左节点设为旋转节点的右节点
		if(rTreeNode != null){
			//将旋转节点的右节点的父节点设置为父节点
			rTreeNode.setpTree(pTreeNode);
		}
		pTreeNode.setlChildTree(rTreeNode);
		//将父节点的父节点设为旋转节点
		pTreeNode.setpTree(Node);
		//将旋转节点的右节点设为父节点
		Node.setrChildTree(pTreeNode);
	}
	
	//前遍历某树
	public void preTraver(SearchTreeNode<T> Node){
		if(Node != null){
			System.out.print(Node.getData());
		}
		
		if(Node.getlChildTree() != null){
			preTraver(Node.getlChildTree());
		}
		
		if(Node.getrChildTree() != null){
			preTraver(Node.getrChildTree());
		}
	}

	//中序遍历树
	public void midTraver(SearchTreeNode<T> Node){
		if(Node.getlChildTree() != null){
			midTraver(Node.getlChildTree());
		}
		
		if(Node != null){
			System.out.print(Node.getData());
		}
		
		if(Node.getrChildTree() != null){
			midTraver(Node.getrChildTree());
		}
	}

	//后序遍历树
	public void afterTraver(SearchTreeNode<T> Node){
		if(Node != null){
			//System.out.println("now :"+Node.getData());
			if(Node.getlChildTree() != null){
				afterTraver(Node.getlChildTree());
			}
			
			if(Node.getrChildTree() != null){
				afterTraver(Node.getrChildTree());
			}
			
			System.out.print(Node.getData());
		}
	}
	
	//根据关键字查找某一个节点
	public SearchTreeNode<T> getNode(int id){
		//根据完全二叉树节点有序的性质去查找某一结点
		//1.从底向上查找,从根节点到目标节点的访问路径
		int nowId = root.getId();
		//判断当前查找的节点是否为根节点
		if(nowId == id){
			return root;
		}
		
		SearchTreeNode<T> temp = root;
		while(nowId != id){
			//判断是左节点还是右节点
			if(id > nowId){
				temp = temp.rChildTree;
			}else{
				temp = temp.lChildTree;
			}
			
			if(temp == null){
				System.out.println("该节点未找到");
				return null;
			}
			
			nowId = temp.getId();
		}
		
		return temp;
	}

	//根据关键字给想插入的某新一结点,查找符合条件的父节点
	public SearchTreeNode<T> getParentNode(int id){
		SearchTreeNode<T> temp = root;
		
		
		//树判空
		if(len <= 0){
			return null;
		}
		
		
		
		while(true){
			if(temp.getId() > id){
				//比父节点小,则去看父节点的左节点是否为空
				//为空则正好插入,该节点为目标节点
				if(temp.getlChildTree() == null){
					return temp;
				}else{
					temp = temp.getlChildTree();
				}
			}else{
				//比父节点大,则去看父节点的右节点是否为空
				//为空则正好插入,该节点为目标节点
				if(temp.getrChildTree() == null){
					return temp;
				}else{
					temp = temp.getrChildTree();
				}
			}
		}
	}

	
	
	
	
	
	
	public SearchTreeNode<T> getRoot() {
		return root;
	}

	public void setRoot(SearchTreeNode<T> root) {
		this.root = root;
	}

	public int getCreaseId() {
		return creaseId;
	}

	public void setCreaseId(int id) {
		this.creaseId = id;
	}

	public int getLen() {
		return len;
	}

	public void setLen(int len) {
		this.len = len;
	}

	public int getFloor() {
		return floor;
	}

	public void setFloor(int floor) {
		this.floor = floor;
	} 
}





package book_1_3;

public class SearchTreeNode <T> {

	SearchTreeNode <T> pTree;//双亲
	SearchTreeNode <T> lChildTree;//左子树
	SearchTreeNode <T> rChildTree;//右子树
	int deep;//以改节点为根的树的深度
	int bf;//平衡因子  -1代表左子树深了一层 	+1代表右子树深了一层
	int id;//关键字
	T data;//数据
	
	public SearchTreeNode(int id,T d) {
		pTree = null;
		lChildTree = null;
		rChildTree = null;
		bf = 0;
		deep = 1;
		this.id = id;
		data = d;
	}

	public SearchTreeNode<T> getpTree() {
		return pTree;
	}

	public void setpTree(SearchTreeNode<T> pTree) {
		this.pTree = pTree;
	}

	public SearchTreeNode<T> getlChildTree() {
		return lChildTree;
	}

	public void setlChildTree(SearchTreeNode<T> lChildTree) {
		this.lChildTree = lChildTree;
	}

	public SearchTreeNode<T> getrChildTree() {
		return rChildTree;
	}

	public void setrChildTree(SearchTreeNode<T> rChildTree) {
		this.rChildTree = rChildTree;
	}

	public int getBf() {
		return bf;
	}

	public void setBf(int bf) {
		this.bf = bf;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getDeep() {
		return deep;
	}

	public void setDeep(int deep) {
		this.deep = deep;
	}
	
	
}


package book_1_3;

public class Test_SearchTree {

	public static void main(String[] args) {
		
		SearchTree<Integer> st = new SearchTree<Integer>();
		
		st.addNode(new SearchTreeNode<Integer>(5, 5));
		st.addNode(new SearchTreeNode<Integer>(1, 1));
		st.addNode(new SearchTreeNode<Integer>(2, 2));
		st.addNode(new SearchTreeNode<Integer>(7, 7));
		st.addNode(new SearchTreeNode<Integer>(6, 6));
		st.addNode(new SearchTreeNode<Integer>(8, 8));
		

		st.preTraver(st.getRoot());
		System.out.println();
		st.midTraver(st.getRoot());
		System.out.println();
		st.afterTraver(st.getRoot());
		
		System.out.println("\n=================");
//		
		System.out.println(st.getRoot().getData());
		System.out.println(st.getRoot().getlChildTree().getData());
		System.out.println(st.getRoot().getrChildTree().getData());
		
//		st.addNode(new SearchTreeNode<Integer>(5, 5));
//		st.addNode(new SearchTreeNode<Integer>(1, 1));
//		st.addNode(new SearchTreeNode<Integer>(2, 2));
//		st.addNode(new SearchTreeNode<Integer>(7, 7));
//		st.addNode(new SearchTreeNode<Integer>(6, 6));
//		st.addNode(new SearchTreeNode<Integer>(8, 8));

//		System.out.println("    \t"+st.getRoot().getData());
//		System.out.print("    "+st.getRoot().getlChildTree().getData());
//		System.out.println("           "+st.getRoot().getrChildTree().getData());
//		System.out.print("         "+st.getRoot().getlChildTree().getrChildTree().getData());
//		System.out.print("    "+st.getRoot().getrChildTree().getlChildTree().getData());
//		System.out.println("    "+st.getRoot().getrChildTree().getrChildTree().getData());
//		
//		st.preTraver(st.getRoot());
//		System.out.println();
//		st.midTraver(st.getRoot());
//		System.out.println();
//		st.afterTraver(st.getRoot());
//		
//		System.out.println("\n===========================");
//		System.out.println("===========================");
//		System.out.println("===========================");
//
//
//		
//		st.leftRotate(st.getRoot().getrChildTree());
//		
//		System.out.println("    \t"+st.getRoot().getData());
//		System.out.print("    "+st.getRoot().getlChildTree().getData());
//		System.out.println("       "+st.getRoot().getrChildTree().getData());
//		System.out.print("  "+st.getRoot().getlChildTree().getlChildTree().getData());
//		System.out.println("    "+st.getRoot().getlChildTree().getrChildTree().getData());
//		System.out.println("    "+st.getRoot().getlChildTree().getlChildTree().getrChildTree().getData());
//		
//		st.preTraver(st.getRoot());
//		System.out.println();
//		st.midTraver(st.getRoot());
//		System.out.println();
//		st.afterTraver(st.getRoot());
//		
//		System.out.println("\n===========================");
//		System.out.println("===========================");
//		System.out.println("===========================");
//		
//		st.rightRotate(st.getRoot().getlChildTree());
//		
//		System.out.println("    \t"+st.getRoot().getData());
//		System.out.print("    "+st.getRoot().getlChildTree().getData());
//		System.out.println("           "+st.getRoot().getrChildTree().getData());
//		System.out.print("         "+st.getRoot().getlChildTree().getrChildTree().getData());
//		System.out.print("    "+st.getRoot().getrChildTree().getlChildTree().getData());
//		System.out.println("    "+st.getRoot().getrChildTree().getrChildTree().getData());
//		
//		st.preTraver(st.getRoot());
//		System.out.println();
//		st.midTraver(st.getRoot());
//		System.out.println();
//		st.afterTraver(st.getRoot());
//		
		
		
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值