java 数据结构 二叉树
package sort; import java.util.Stack; public class TreeSort { public static void main(String[] args) { int[] arrTemp = {1,23,5,45,25,34,15,37,46,6,849,4,57,31,86,74,32,37,498,876,221,334,81,49,39,231,8,2,321,3,51}; Node order=new Node(); for(int itemp:arrTemp) { order.add(new Node(itemp)); } order.print(); } } class Node { int value; Node left; Node right; Node parent; Node() { } Node(int i) { value=i; } void add(Node now) { if(this == null) { value=now.value; left=now.left; right=now.right; parent=now.parent; } addSolve(now,this); } void addSolve(Node now,Node temp) { if(temp.value>now.value) { if(temp.left==null) { now.parent=temp; temp.left=now; } else { addSolve(now,temp.left); } } if(temp.value<now.value) { if(temp.right==null) { now.parent=temp; temp.right=now; } else { addSolve(now,temp.right); } } } void print() { Stack st = new Stack(); recursion(this,st); while (!st.empty()) { System.out.println(st.pop()); } } void recursion(Node temp,Stack st) { if(temp.right!=null) recursion(temp.right,st); st.push(temp.value); if(temp.left!=null) recursion(temp.left,st); } }