java實現 二叉樹遍曆

來源:互聯網
上載者:User

哈~ 今年剛學Java,感覺和C#有異曲同工之妙,呵呵,準備開始寫博,記錄自己學Java的點滴……

 

//介面
public interface BinTrees {
 
 public void Firorder();
 public void Secorder();
 public void Inorder();
 
 public void Insert(Node node);
 
}

 

//介面
public interface Nodeinter {
 
 public void Append(Node node);
 public void AppendLeft(Node node);
 public void AppendRight(Node node);

}

 

class Node implements Nodeinter{
 
 public int nodeNata;
 public Node left;
 public Node right;
 
 public Node(int data)
 {
  this.nodeNata=data;
 }
 
 //追加結點
 public void Append(Node node)
 {
  if(node.nodeNata <= this.nodeNata)
  {
   this.AppendLeft(node);
  }
  else
  {
   this.AppendRight(node);
  }
  
 }
 
 //追加左子
 public void AppendLeft(Node node)
 {
  if(this.left==null)
  {
   this.left=node;
  }
  else
  {
   this.left.Append(node);
  }
 }
 //追加右子
 public void AppendRight(Node node)
 {
  if(this.right==null)
  {
   this.right=node;
  }
  else
  {
   this.right.Append(node);
  }
 } 
 
 //列印結點
 public void print()
 {
  if(this.nodeNata!='#')
  {
   System.out.println(this.nodeNata);
  }
  else
  {
   System.out.println('#');
  }
 }
}

 

public class BinTree implements BinTrees {
 
 public Node root=null;
 
 public BinTree(Node n)
 {
  this.root=n;
 }
 
 
 //添加結點
 public void Insert(Node node)
 {
  if(this.root==null)
  {
   this.root=node;
  }
  else
  {
   this.root.Append(node);
  }
 }
 
 public void Firorder()
 {
  this.FirOrder(root);
 }
 public void Secorder()
 {
  this.LasOrder(root);
 }
 public void Inorder()
 {
  this.InOrder(root);
 }
 
 //中序遍曆遞迴演算法
 public void InOrder(Node node)
 {
  if(node!=null)
  {
   this.InOrder(node.left);
   node.print(); 
   this.InOrder(node.right);
  }
 }
 
 //先序遍曆遞迴演算法
 public void FirOrder(Node node)
 {
  if(node!=null)
  {
   node.print();
   this.FirOrder(node.left);
   this.FirOrder(node.right);
  }
 }
 
 //後序遍曆遞迴演算法
 public void LasOrder(Node node)
 {
  if(node!=null)
  {
   this.LasOrder(node.left);
   this.LasOrder(node.right);
   node.print();
  }
 }
 

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Node n1=new Node(7);
  Node n2=new Node(9);
  Node n3=new Node(3);
  Node n4=new Node(8);
  Node n5=new Node(2);
  Node n6=new Node(6);
  
  
  BinTree bt=new BinTree(n1);
  bt.Insert(n2);
  bt.Insert(n3);
  bt.Insert(n4);
  bt.Insert(n5);
  bt.Insert(n6);
  
  
  
  bt.Inorder();
  bt.Firorder();
  bt.Secorder();
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.