JAvaカスタムツリー


今日学んだのは二叉樹に関する知識です.二叉樹は木の一種で、彼は結点ごとに最大2つの子結点しかないので、二叉樹と呼ばれています.チェーン時計は実際には木の特殊な状況に似ています.二叉樹にはいろいろな種類がありますが、その中で有名なのは二叉検索樹とホフマン樹です.
参照
ツリーは、図論では、ツリーが連通する無ループ図であり、各頂点の度が2以下であると定義されている.二叉木があるのに根の接点を満たす度は2以下である.ルートノードがある場合、各頂点は一意の親ノードと最大2つのサブノードを定義します.しかしながら、左ノードと右ノードを区別するのに十分な情報はない.連通性を考慮しなければ,図中に複数の連通成分を許容する構造を森林と呼ぶ.
次は私のツリーのノードクラスコードです.
public class TNode {
	
	private Object obj;
	private TNode parent;
	private TNode left;
	private TNode right;
	
	public TNode(Object obj){
		this.obj = obj;
	}
	
	
	public TNode(){
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	public TNode getParent() {
		return parent;
	}

	public void setParent(TNode parent) {
		this.parent = parent;
	}

	public TNode getLeft() {
		return left;
	}

	public void setLeft(TNode left) {
		this.left = left;
	}

	public TNode getRight() {
		return right;
	}

	public void setRight(TNode right) {
		this.right = right;
	}
}

次はツリーのクラスです(配列をツリーに形成することが実装されています)
public class BinaryTree {

	private static TNode root;

	public BinaryTree() {

	}

	/**
	 *           
	 * 
	 * @param obj
	 */
	public BinaryTree(Object obj) {
		root = new TNode(obj);
	}

	/**
	 *                
	 * 
	 * @param Array
	 *                  
	 */
	public void ArraytoTree(int[] Array) {
		if(Array.length == 0){
			throw new RuntimeException("     0,        !");
		}
		int first = Array[0];
		root = new TNode(first);
		for (int i = 1; i < Array.length; i++) {
			addofBST(root, Array[i]);
		}
	}

	/**
	 *                  
	 * 
	 * @param node
	 * @param value
	 */
	public void addofBST(TNode node, int value) {
		TNode current;
		if ((Integer) node.getObj() >= value) {
			if (node.getLeft() != null) {
				current = node.getLeft();
				addofBST(current, value);
			}else{
				current = new TNode(value);
				current.setParent(node);
				node.setLeft(current);
			}
		}else{			
			if (node.getRight() != null) {
				current = node.getRight();
				addofBST(current, value);
			}else{
				current = new TNode(value);
				current.setParent(node);
				node.setRight(current);
			}
		}
	}
		

	/**
	 *             
	 * 
	 * @param node
	 *                   
	 * @param style
	 *              ,1    ,0    ,-1    
	 */
	public void printTree(TNode node, int style) {
		if (node != null) {
			if (style == 1) {
				//      
				Object obj = node.getObj();
				System.out.println(obj);
				//         ,   
				TNode left = node.getLeft();
				printTree(left, style);
				//         ,   
				TNode right = node.getRight();
				printTree(right, style);
			} else if (style == 0) {
				//         ,   
				TNode left = node.getLeft();
				printTree(left, style);
				//      
				Object obj = node.getObj();
				System.out.println(obj);
				//         ,   
				TNode right = node.getRight();
				printTree(right, style);
			} else if (style == -1) {
				//         ,   
				TNode left = node.getLeft();
				printTree(left, style);
				//         ,   
				TNode right = node.getRight();
				printTree(right, style);
				//      
				Object obj = node.getObj();
				System.out.println(obj);
			}
		}
	}

}