二叉树的一些非递归算法(JAVA)

import java.util.ArrayList;

public class Tree {

 static class Queue
 {
  ArrayList data = new ArrayList();
  
  public void putQ(Node a)
  {
   data.add(a);
  }
  public Node getQ()
  {
   if(data.size() > 0)
    return (Node)data.remove(0);
   else
    return null;
  }
 }
 static class Stack
 {
  ArrayList data = new ArrayList();
  
  public void push(Node a)
  {
   data.add(a);
  }
  public Node pop()
  {
   if(data.size() > 0)
    return (Node)data.remove(data.size()-1);
   else
    return null;
  }
  
  public Node getTop()
  {
   if(data.size() > 0)
    return (Node)data.get(data.size()-1);
   else
    return null;
  }
 }
 
 static class Node
 {
  Node(String value)
  {
   this.value = value;
  }
  String value;
  Node left;
  Node right;
  Node brother;
  int layer;//from 1
 }
 
 Node root;
 /**
  * @param args
  */
 public static void main(String[] args) {
  Tree tree = new Tree();
  tree.buildTree();
  System.out.println("-----layer---------");
  tree.printTree(tree.root,1);
  tree.connectBrother();
  System.out.println("------brother--------");
  tree.printBrother(tree.root);
  System.out.println("-------pre-------");
  tree.preOrder();
  System.out.println("-------mi-------");
  tree.miOrder();
  System.out.println("------last--------");
  tree.lastOrder();
  System.out.println("--------------");
 }
 
 void buildTree()
 {
  Node a = new Node("a");
  Node b = new Node("b");
  Node c = new Node("c");
  Node d = new Node("d");
  Node e = new Node("e");
  Node f = new Node("f");
  Node g = new Node("g");
  Node h = new Node("h");
  Node i = new Node("i");
  this.root = a;
  a.left = b;
  a.right = c;
  
  b.left = d;
  b.right = e;
  
  d.left = h;
  
  c.left = f;
  c.right = g;
  
  f.right = i;
 }
 
 //计算每个节点所处的层次
 void printTree(Node node,int layer)
 {
  if(node != null)
  {
   node.layer = layer;
   System.out.println(node.value + "****" + node.layer);
  }
  if(node.left != null) printTree(node.left,layer+1);
  if(node.right != null) printTree(node.right,layer+1);
 }
 //将所有兄第横向连接起来(需要先计算出每个节点所处的层次)
 void connectBrother()
 {
  Queue q = new Queue();
  Node cur = root;
  Node up = root;
  
  Node node = null;
  q.putQ(cur);
  
  while((node=q.getQ()) != null)
  {
   if(node.layer == up.layer)
   {
    up.brother = node;
   }
   if(node.left!=null)
   {
    q.putQ(node.left);
   }
   if(node.right!=null)
   {
    q.putQ(node.right);
   }
   up = node;
  }
  
 }
 
 
 void printBrother(Node node)
 {
  if(node != null)
  {
   if(node.brother != null)
    System.out.println(node.value + "--" + node.brother.value);
   else
    System.out.println(node.value + "--" + node.brother);
  }
  if(node.left != null) printBrother(node.left);
  if(node.right != null) printBrother(node.right);
 }
 //先序
 void preOrder()
 {
  Stack s = new Stack();
  Node cur = root;
  s.push(cur);
  
  while((cur=s.pop())!=null)
  {
   System.out.println(cur.value);
   if(cur.right!=null) s.push(cur.right);
   if(cur.left!=null) s.push(cur.left);
  }
 }
 //中序
 void miOrder()
 {
  Stack s = new Stack();
  Node p = root;
  while((p != null)||(s.data.size() > 0))
  {
   while(p != null)
   {
    s.push(p);
    p = p.left;
   }
   p = s.pop();
   System.out.println(p.value);
   p = p.right;
  }
 }
 //后序
 void lastOrder()
 {
  Stack s = new Stack();
  
  Node p = root;
  Node pre = null;
  while((p != null)||(s.data.size() > 0))
  {
   while(p != null)
   {
    s.push(p);
    p = p.left;
   }
   p = s.getTop();
   
   if(p.right == null || p.right == pre)
   {
    System.out.println(p.value);
    s.pop();
    pre = p;
    p = null;
   }
   else
   {
    p = p.right;
   }
  }
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值