java实现二叉树及(层序,先序,中序,后序,节点查找)

import java.util.Deque;
import java.util.LinkedList;

public class BiTree<T> {
   private T Data;//节点数据
   private BiTree<T> leftChild;//左孩子
   private BiTree<T> rightChild;//右孩子

   public T getData() {
    return Data;
}
public void setData(T data) {
    Data = data;
}
public BiTree<T> getLeftChild() {
    return leftChild;
}
public void setLeftChild(BiTree<T> leftChild) {
    this.leftChild = leftChild;
}
public BiTree<T> getRightChild() {
    return rightChild;
}
public void setRightChild(BiTree<T> rightChild) {
    this.rightChild = rightChild;
}
public BiTree(){
       Data=null;
       leftChild=null;
       rightChild=null;

   }
public BiTree(T data){
       Data=data;
       leftChild=null;
       rightChild=null;

}
//插入左孩子,返回新节点
   public BiTree<T> insertLeftChild(T data){
       BiTree<T> newNode=new BiTree<T>();
       newNode.setData(data);
       //新节点的左孩子指向当前节点的左孩子
       newNode.leftChild=this.leftChild;
      //当前节点的左孩子指向新节点
      this.leftChild=newNode;

       return newNode;

   }
   //插入右孩子,返回新节点
   public BiTree<T> insertRightChild(T data){
       BiTree<T> newTree=new BiTree<T>();
       newTree.setData(data);
       //新节点的右孩子指向当前节点的右孩子
       newTree.rightChild=this.rightChild;
       //当前节点的右孩子指向新节点
       this.rightChild=newTree;
       return newTree;

   }
   //删除当前节点的右孩子,并返回被删除的右孩子节点
   public BiTree<T> deleteRightChild(){
         BiTree<T> oldNode=this.rightChild;
         this.rightChild=null;
         return oldNode;
   }
   //删除当前节点的左孩子,并返回被删除的左孩子节点
   public BiTree<T> deleteleftChild(){
       BiTree<T> oldNode=this.leftChild;
       this.leftChild=null;
       return oldNode;
   }
   //前序遍历
   public  void preOrder(BiTree<T> current){
       if(current==null){
           return;
       }
       System.out.print(" "+current.getData());
       preOrder(current.getLeftChild());
       preOrder(current.getRightChild());


   }
   //中序遍历
   public   void InOrder(BiTree<T> current){
       if(current==null){
           return;
       }
       InOrder(current.leftChild);
       System.out.print(" "+current.getData());
       InOrder(current.rightChild);

   }
   //后序遍历
   public  void PostOrder(BiTree<T> current){
       if(current==null){
           return;
       }
           PostOrder(current.leftChild);
           PostOrder(current.rightChild);
           System.out.print(" "+current.getData());


   }
   //层序遍历
   /*1.将头结点存入队列中 *2.判断队列是否为空,不为空取出队首元素,并打印此元素 *3.判断取出的节点的左孩子是否为空,不为空放入队列中 *4.判断取出的节点的右孩子是否为空,不为空放入队列中 *重复2,3,4 步骤直到队列为空结束*/
   public void CenOrder(BiTree<T> current){
          Deque<BiTree<T>> queue=new LinkedList<>();
          if(current!=null){
              queue.add(current);
          }

         while(!queue.isEmpty()){
            BiTree<T> node=queue.removeFirst();
          System.out.print(" "+node.getData());
            if(node.getLeftChild()!=null){
                queue.add(node.getLeftChild());
            }
            if(node.getRightChild()!=null){
                    queue.add(node.getRightChild());
              }

           }
   }

   public  BiTree<T> search(BiTree<T> current,T data){
       if(current==null){
           return null;
       }else{
           if(current.getData().equals(data))
               return current;
           BiTree<T> result=null;
           result=search(current.leftChild,data);
           if(result==null){
            result=search(current.rightChild,data);
           }
           if(result==null){
               System.out.println("找不到该节点");
           }
           return result;
       }
   }
    public static void main(String[] args) {
        BiTree<String> head=new BiTree<String>("root");
        BiTree<String> left=head.insertLeftChild("FG");
        BiTree<String> right=head.insertRightChild("TY");
        BiTree<String> left2=left.insertLeftChild("SS");
        BiTree<String> right2=left.insertRightChild("ER");
        BiTree<String> left3=right.insertLeftChild("Rign");
        BiTree<String> right3=right.insertRightChild("RGB");
        BiTree<String> left4=left2.insertLeftChild("f4");
        BiTree<String> left5=right2.insertLeftChild("data");
        BiTree<String> left7=right3.insertLeftChild("kll");
        BiTree<String> right5=left3.insertRightChild("KLJ");

        BiTree<String> right6=right2.insertRightChild("rj");

        BiTree<String> right7=right3.insertRightChild("ghhj");
        System.out.println("\n-------------------层序遍历--------------------->");
        head.CenOrder(head);
        System.out.println("\n----------------先序遍历---------------------->");
        head.preOrder(head);
        System.out.println("\n-----------------中序遍历--------------------->");
        head.InOrder(head);
        System.out.println("\n------------------后序遍历----------------------->");
        head.PostOrder(head);
        System.out.println("\n-----------------测试获取数据---------------------->");
        System.out.println(head.search(head,"f4" ).getData());

    }
}
    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/gao23191879/article/details/77456565
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞