Chapter8:二叉树的基本使用

package Chapter08;

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

public class BinaryTree {

 /**
  * @二叉树
  * 完整的查找,插入,显示,遍历(前序,中序,后序),删除代码
  */
 public static void main(String[] args) {
  int value;
  Tree theTree = new Tree();
  
  theTree.insert(50, 1.5);
  theTree.insert(25, 1.2);
  theTree.insert(75, 1.7);
  theTree.insert(12, 1.5);
  theTree.insert(37, 1.2);
  theTree.insert(43, 1.7);
  theTree.insert(30, 1.5);
  theTree.insert(33, 1.2);
  theTree.insert(87, 1.7);
  theTree.insert(93, 1.5);
  theTree.insert(97, 1.5);
  
  while(true){
   sop("Enter first letter of show, ");
   sop("insert, find, delete, or traverse: ");
   Scanner sc = new Scanner(System.in);
   char choice = sc.nextLine().charAt(0);
   
   switch(choice){
   case 's':          //显示
    theTree.displayTree();
    break;
   case 'i':          //插入
    sop("Enter value to insert: ");
    value = sc.nextInt();
    theTree.insert(value, value+0.9);
    break;
   case 'f':          //查找
    sop("Enter value to find: ");
    value = sc.nextInt();
    Node found = theTree.find(value);
    if(found!=null){
     sop("found ");
     found.display();
     sop("\n");
    }else{
     sop("not found: "+ value+"\n");
    }
    break;
   case 'd':          //删除
    sop("Enter value to delete: ");
    value = sc.nextInt();
    boolean didDelete = theTree.delete(value);
    if(didDelete)
     sop("delete "+value +"\n");
    else
     sop("could not delete "+value+" not found! \n");
    break;
   case 't':          //遍历
    sop("Enter type 1,2,3 to show the tree: ");
    value = sc.nextInt();
    theTree.traverse(value);
    break;
   default:
    sop("valid Entry\n");
   }
   
  }
 
 }

 private static void sop(Object o) {
  System.out.print(o);
 }

}
class Node{
 public int iData;
 public double dData;
 public Node leftChild;
 public Node rightChild;
 
 public Node(int id,double dd){
  iData = id;
  dData = dd;
 }
 
 public void display(){
  System.out.print("{ "+iData +" , "+dData +" }");
 }
}
class Tree{
 private Node root;
 
 //查找
 public Node find(int key){
  Node current = root;
  while(current.iData!=key){
   if(current.iData < key)
    current = current.rightChild;
   else
    current = current.leftChild;
   if(current == null)
    return null;
  }
  return current;
 }
 //插入
 public void insert(int id,double dd){
  Node newNode = new Node(id,dd);
  
  if(root == null)
   root = newNode;
  
  else{
   Node current = root;
   Node parent = current;

   while(true){
    parent = current;
    if(current.iData<id){
     current = current.rightChild;
     if(current == null){
      parent.rightChild = newNode;
      return;
     }
    }
    else{
     current = current.leftChild;
     if(current == null){
      parent.leftChild = newNode;
      return;
     }
    }
   }
  }
 }
 
 //删除
 public boolean delete(int key){
  Node current = root;
  Node parent = root;
  boolean isLeftChild = true;
  
  //第一步,找到要删除的节点
  while(current.iData!=key){
   parent = current;
   if(current.iData>key){
    current = current.leftChild;
    isLeftChild = true;
   }
   else{
    current = current.rightChild;
    isLeftChild = false;
   }
   if (current == null )
    return false;
  }
    
  //第二步(情况一,叶子节点)
  if(current.leftChild==null && current.rightChild==null){
   if(current == root)
    root = null;
   else
   if(isLeftChild)
    parent.leftChild = null;
   else
    parent.rightChild = null;
   
  }
  //第二步(情况二,只有一个子节点)
  else if(current.leftChild==null){
   if(current == root)
    root = current.rightChild;
   else if(isLeftChild)
    parent.leftChild = current.rightChild;
   else
    parent.rightChild = current.rightChild;
  }
  else if(current.rightChild == null){
   if(current == root)
    root = current.leftChild;
   else if(isLeftChild)
    parent.leftChild = current.leftChild;
   else
    parent.rightChild = current.leftChild;
  }
  //第二步(情况三,有两个子节点)
  else{
   Node successor = getSuccessor(current);
   if(current == root)
    root = successor;
   else if(isLeftChild)
    parent.leftChild = successor;
   else
    parent.rightChild = successor;
   
   successor.leftChild = current.leftChild;
  }
  return true;
 }
 private Node getSuccessor(Node delNode) {
  Node successorParent = delNode;
  Node successor = delNode;
  Node current = delNode.rightChild;
  while(current!=null){
   successorParent = current;
   successor = current;
   current = current.leftChild;
  }
  if(successor!= delNode.rightChild){
   successorParent.leftChild = successor.rightChild;
   successor.rightChild = delNode.rightChild;
  }
  return successor;
 }
 
 //遍历
 public void traverse(int traverseType){
  switch(traverseType){
  case 1: System.out.println("Pre_Order: traversal: ");
    preOrder(root);
    break;
  case 2: System.out.println("In_Order: traversal: ");
    InOrder(root);
    break;
  case 3: System.out.println("Post_Order: travaersal: ");
    PostOrder(root);
    break;
  }
  System.out.println();
 }
 private void preOrder(Node localRoot) {
  if(localRoot!=null){
   System.out.print(localRoot.iData+" ");
   preOrder(localRoot.leftChild);
   preOrder(localRoot.rightChild);
  }
  
 }
 private void InOrder(Node localRoot) {
  if(localRoot!=null){
   InOrder(localRoot.leftChild);
   System.out.print(localRoot.iData+" ");
   InOrder(localRoot.rightChild);
  }
 }
 private void PostOrder(Node localRoot) {
  if(localRoot!=null){
   PostOrder(localRoot.leftChild);
   PostOrder(localRoot.rightChild);
   System.out.print(localRoot.iData+" ");
  }
  
 }
 
 //显示树
 public void displayTree(){
  Stack globalStack = new Stack();
  globalStack.push(root);
  int nBlanks = 32;
  boolean isRowEmpty = false;
  System.out.println("---------------------------------------");
  while(isRowEmpty==false){
   Stack localStack = new Stack();
   isRowEmpty = true;
   
   for(int i=0;i<nBlanks;i++){
    System.out.print(" ");
   }
   while(globalStack.isEmpty()==false){
    Node temp = (Node) globalStack.pop();
    if(temp!=null){
     System.out.println(temp.iData);
     localStack.push(temp.leftChild);
     localStack.push(temp.rightChild);
     
     if(temp.leftChild!=null || temp.rightChild!=null)
      isRowEmpty = false;
    }
    else{
     System.out.println("--");
     localStack.push(null);
     localStack.push(null);
     
    }
    for(int i=0;i<nBlanks*2-2;i++){
     System.out.print(" ");
    }
   }
   System.out.println();
   nBlanks = nBlanks/2;
   while(localStack.isEmpty()==false)
    globalStack.push(localStack.pop());
  }
  System.out.println();
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值