2013년 8월 13일 화요일

자바 트리의 운행(Queue를 이용한 Level Order)

오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(신입사원채용무료교육, 오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷)  

/** 트리의 운행 Example
*  Queue를 이용한 Level Order 운행법 예제
*/
import java.util.*;  //for Random Class

class BinaryNode    {   
  int  element;                  // The data in the node
  BinaryNode left;          // Left child
  BinaryNode right;        // Right child

  BinaryNode(int element) { //생성자
  this( element, null, null );
  }
  BinaryNode(int element, BinaryNode left, BinaryNode right){
            this.element  = element;
            this.left    = left;
            this.right    = right;
  }
}

class LevelOrderTraversal {
ArrayQueue queue = new ArrayQueue(20);
public BinaryNode insert(BinaryNode tree, int n) {
if (tree == null) {
tree = new BinaryNode(n);
tree.right= tree.left=null;
}
else if(n > tree.element) {
tree.right = insert(tree.right, n);
}
else {
tree.left = insert(tree.left, n);
}
return tree; 
}

public void levelorder(Binbsp;queue.put(node.left);
if (node.right!=null) queue.put(node.right);
}
}
}

public class LevelOrderTest {
public static void main(String[] args){
BinaryNode rootint i=0; i<5; i++) {
nansu = r.nextInt()%100;
                            &nut.print("\nLevel Order Traversal : ");
tr.levelorder(root);
System.out.println("");
}
}

댓글 없음:

댓글 쓰기