二插排序树各种算法

3
1
#
2
#
#
5
4
#
#
#

//以上是输入
3 1 2 5 4 
3 1 2 5 4 6 
3  1  2  5  4  6  
1 2 3 4 5 6 
1 2 3 4 5 6 
2 1 4 6 5 3 
2 1 4 6 5 3 
5
null
1
-1
1
1
6
4
1
3 1 0 2 5 4 6 
5 4 1 0 2 6 

//测试结果

package binarySortTree;



public class  SNode<T extends Comparable<T>> implements Comparable<SNode<T>>{

private T data;
private SNode<T> lchild;
private SNode<T> rchild;



public SNode(T data, SNode<T> lchild, SNode<T> rchild) {
this.data = data;
this.lchild = lchild;
this.rchild = rchild;
}


public SNode(){}

public SNode(T data){
this(data,null,null);
}


public T getData() {
return data;
}




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




public SNode<T> getLchild() {
return lchild;
}




public void setLchild(SNode<T> lchild) {
this.lchild = lchild;
}




public SNode<T> getRchild() {
return rchild;
}




public void setRchild(SNode<T> rchild) {
this.rchild = rchild;
}





@Override
public int hashCode() {
return data.hashCode();
}


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SNode other = (SNode) obj;
if (data == null) {
if (other.data != null)
return false;
} else if (!data.equals(other.data))
return false;
return true;
}


@Override
public int compareTo(SNode<T> o) {
return this.getData().compareTo(o.getData());
}


}

package binarySortTree;


import java.util.Scanner;
import java.util.Stack;




/*
 *  二叉排序树,也可以成为二叉查找树
 * 它的性质如下:
 * 1.若它的左子树不为空,则左子树上所有的节点均小于其根节点
 * 2.若它的右子树不为空,则右子树上所有的节点的值均大于根节点
 * 3.它的左右子树也分别为二叉排序树
 * */
public class BinarySortTree<T extends Comparable<T>> {


private SNode<T> root;



public BinarySortTree() {
}

public BinarySortTree(SNode<T> root) {
this.root = root;
}


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

//第归的创建一棵树
public void createBinarySearchTree(){
this.root=createBinarySearchTree(root);
}
private SNode<T> createBinarySearchTree(SNode<T> root){
Scanner sc=new Scanner(System.in);
String data=sc.nextLine();
if(data.equalsIgnoreCase("#")){
return null;
}else{
if(root==null)root=new SNode(data);
root.setLchild(createBinarySearchTree(root.getLchild()));
root.setRchild(createBinarySearchTree(root.getRchild()));
return root;
}
}

//添加新的结点 第归的添加新的结点
public void insertSNode(T data){
this.root=insertSNode(root,new SNode(data));
}
private SNode<T> insertSNode(SNode<T> root,SNode<T> element){
if(root==null)root=element ;
else if(element.compareTo(root.getLchild())<0){
root.setLchild((insertSNode(root.getLchild(),element)));
}else{
root.setRchild((insertSNode(root.getRchild(),element)));
}
return root;
}

//非第归的添加元素
/*
* 找到要插入结点的父结点
* 然后将要插入的结点与父结点进行比较
* */
public void addSNode(T data){
this.root=addSNode(root, new SNode((T)data));
}


private SNode<T> addSNode(SNode<T> root,SNode<T> element){
SNode<T> node=root;
SNode<T> parent=null;
while(node!=null){
parent=node;
if(element.getData().compareTo(node.getData())<0){
node=node.getLchild();
}else{
node=node.getRchild();
}
}
if(parent==null)node=element;
else if(element.getData().compareTo(parent.getData())<0){
parent.setLchild(element);
}else{
parent.setRchild(element);
}
return root;
}

//返回指定结点的父结点
public SNode<T> getParentValue(T data){
SNode<T> node=getParent(root,new SNode(data));
if(node!=null){
return node;
}
return null;
}
private SNode<T> getParent(SNode<T> root,SNode<T> element){
if(root==null)return null;
if(element.equals(root.getLchild())||element.equals(root.getRchild())){
return root;
}
SNode<T> node=null;
if((node=getParent(root.getLchild(),element))!=null){
return node;
}else{
return getParent(root.getRchild(),element);
}
}



//先序遍历
private void preOrder(SNode<T> root){
if(root!=null){
System.out.print(root.getData()+" ");
preOrder(root.getLchild());
preOrder(root.getRchild());
}
}

public void PreOrder(){
preOrder(root);
}
//先序遍历的非第归形式
public void nrPreOrderTraverse(){
nrPreOrderTraverse(root);
}
private void nrPreOrderTraverse(SNode<T> root){
Stack<SNode<T>> stack=new Stack<SNode<T>>();
SNode<T> node=root;
while(node!=null || !stack.isEmpty()){
while(node!=null){
System.out.print(node.getData()+"  ");
stack.push(node);
node=node.getLchild();
}
node=stack.pop();
node=node.getRchild();
}
}

//中序遍历的第归形式     
public void inOrderTraverse(){
inOrderTraverse(root);
}

private void inOrderTraverse(SNode<T> root){
if(root!=null){
inOrderTraverse(root.getLchild());
System.out.print(root.getData()+" ");
inOrderTraverse(root.getRchild());
}
}

//中序遍历的非第归形式
public void nrinOrderTraverse(){
nrinOrderTraverse(root);
}
private void nrinOrderTraverse(SNode<T> root){
Stack<SNode<T>> stack=new Stack<SNode<T>>();
SNode<T> node=root;
while(node!=null || !stack.isEmpty()){
  while(node!=null){
stack.push(node);
node=node.getLchild();
  }
node=stack.pop();
System.out.print(node.getData()+" ");
node=node.getRchild();
        }
}

//后序遍历的第归形式
public void lastOrderTraverse(){
lastOrderTraverse(root);
}
private void lastOrderTraverse(SNode<T> root){
if(root!=null){
lastOrderTraverse(root.getLchild());
lastOrderTraverse(root.getRchild());
System.out.print(root.getData()+" ");
}
}


//后序遍历的非第归形式
public void nrLastOrderTraverse(){
nrLastOrderTraverse(root);
}

private void nrLastOrderTraverse(SNode<T> root){
Stack<SNode<T>> stack=new Stack<SNode<T>>();
SNode<T> node=root;
SNode<T> preNode=null;
while(node!=null || !stack.isEmpty()){
while(node!=null){
stack.push(node);
node=node.getLchild();
}
node=stack.peek();
if(node.getRchild()==null || node.getRchild()==preNode){
System.out.print(node.getData()+" ");
node=stack.pop();
preNode=node;
node=null;
}else{
node=node.getRchild();
}
}
}

//查找指定的元素第归的查找
public int searchKey(T data){
return searchKey(root,data);
}

private int searchKey(SNode<T> root,T data){
if(root==null)return -1;
if(root.getData().equals(data)){
return 1;
}else if(data.compareTo(root.getData())<0){
return searchKey(root.getLchild(),data);
}else{
return searchKey(root.getRchild(),data);
}
}
//找到当前的值并返回当前的指针
public SNode<T> searchKeyAndReturnNode(T data){
return searchKeyAndReturnNode(root,data);
}
private SNode<T> searchKeyAndReturnNode(SNode<T> root,T data){
if(root==null)return null;
if(root.getData().equals(data)){
return root;
}else if(data.compareTo(root.getData())<0){
return searchKeyAndReturnNode(root.getLchild(),data);
}else{
return searchKeyAndReturnNode(root.getRchild(),data);
}
}
//非第归的查找
public int iterator_tree_search(T data){
return iterator_tree_search(root,data);
}
private int iterator_tree_search(SNode<T> root,T data){
SNode<T> node=root;
while(node!=null && !data.equals(node.getData())){
if(data.compareTo(node.getData())<0){
node=node.getLchild();
}else{
node=node.getRchild();
}
}
if(node!=null){
return 1;
}else{
return -1;
}
}

//查找二插排序树的最小值
public T getMinValue(){
return getMinValueNode(root).getData();
}

private SNode<T> getMinValueNode(SNode<T> root){
SNode<T> node=root;
if(node==null)return null;
while(node.getLchild()!=null){
node=node.getLchild();
}
return node;
}


//获得二插排序树的最大值
public T getMaxValue(){
return getMaxValueNode(root).getData();
}
private SNode<T> getMaxValueNode(SNode<T> root){
SNode<T> node=root;
while(node.getRchild()!=null){
node=node.getRchild();
}
return node;
}

//获得某个值的后继结点的值
public T getTreeSuccessorValue(T data){
return getTreeSuccessor(data).getData();
}

//获得某个值的后继结点
/*
*首先获取当前结点
*如果当前的结点有右结点则直接返回右结点的最小值的结点
*得到当前结点的父结点
*如果父结点不等于空并且父结点的右孩子是当前的结点
*   当前结点=父结点
*   父结点=父结点的父结点
*   返回 父结点
* */
public SNode<T> getTreeSuccessor(T data){
SNode<T> current=this.searchKeyAndReturnNode(data);//返回当前值的结点
if(current==null)return null;
if(current.getRchild()!=null){
return this.getMinValueNode(current.getRchild());
}
SNode<T> parent=this.getParent(root, current);
while(parent!=null&&parent.getRchild()==current){
current=parent;
parent=this.getParent(root, current);
}
return parent;
}

//获得某个值的前驱的结点
/*
* 首先获取当前值的结点
* 如果当前结点的值为null则返回null
* 如果 当前结点的左孩子不为空,则返回左孩子结点的最大值
* 得到当前结点的父结点
* 只要父结点不等于空且父结点的左孩子等于当前结点
*   当前结点=父结点
*   父结点=父结点的父结点
*  返回父结点
*  
* */
public T getTreePreSuccessorValue(T data){
return getTreePreSuccessor(data).getData();
}
public SNode<T> getTreePreSuccessor(T data){
SNode<T> current=this.searchKeyAndReturnNode(data);
if(current==null) return null;
if(current.getLchild()!=null){
return this.getMaxValueNode(current.getLchild());
}
SNode<T> parent=this.getParent(root, current);
while(parent!=null && parent.getLchild()==current){
current=parent;
parent=this.getParent(root, current);
}
return parent;
}
/*
* 删除树中的结点步骤
* 1首先获取当前的结点
* 如果当前的结点的左右孩子都为空,则直接删除
* 如果左孩子不为空或者右孩子不为空,则将这个孩子替换为该结点
* 如果左孩子和右孩子都不为空,则将该结点的左孩子插入到右孩子上,并指向右孩子
* */
public void deleteNode(T data){
this.root=deleteNode(root,new SNode(data));
}
private SNode<T> deleteNode(SNode<T> root,SNode<T> element){
SNode<T> p=null;
SNode<T> current=this.searchKeyAndReturnNode(root, element.getData());
if(current==null)return null;
SNode<T> parent=this.getParent(root, current);
p=current;
if(current.getLchild()!=null){
current=this.addSNode(current.getRchild(),current.getLchild());
}

//current=current.getRchild();
if(parent==null){
root=current;
}else if(p.equals(parent.getRchild())){
parent.setRchild(current);
}else{
parent.setLchild(current);
}
p.setLchild(null);
p.setRchild(null);
p=null;
return root;
}


 
public static void main(String[] args){
BinarySortTree<String> bs=new BinarySortTree<String>();
bs.createBinarySearchTree();
bs.PreOrder();
System.out.println();
bs.insertSNode("6");
bs.PreOrder();
System.out.println();
bs.nrPreOrderTraverse();
System.out.println();
bs.nrinOrderTraverse();
System.out.println();
bs.inOrderTraverse();
System.out.println();
bs.lastOrderTraverse();
System.out.println();
bs.nrLastOrderTraverse();
System.out.println();
System.out.println(bs.getParentValue("6").getData());
System.out.println(bs.getParentValue("3"));
System.out.println(bs.searchKey("3"));
System.out.println(bs.searchKey("8"));
System.out.println(bs.iterator_tree_search("1"));
System.out.println(bs.getMinValue());
System.out.println(bs.getMaxValue());
System.out.println(bs.getTreeSuccessorValue("3"));
System.out.println(bs.getTreePreSuccessorValue("2"));
bs.addSNode("0");
bs.PreOrder();
System.out.println();
bs.deleteNode("3");
bs.PreOrder();

}



}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值