데이터 구조이진트리 / 중위운행을이용한 정렬JAVA1.이진트리의 정의트리(Binary Tree)란 직관적으로 데이터 사이의 관계를 계층적으로 나타내는 비선형 데이터 구조(Non-linear data structure)이다.트리에서 데이터 하나하나는 노드(node)로 정의되고, 이들 노드들은 가지(branch)에 의하여 계층적 관계로 연결되어 있어, 종적 또는 횡적 관계를 유지한다. 기본적으로 트리의 노드들은 계층적으로 가지에 의해 연결되어 경로(path)를 형성하는데, 그래프와는 달리 어떠한 경우에도 사이클(cycle)을 생성하지 않는다.트리에 대해서 알아 보았으니 이제 이진트리에 대해 정의해 보자. 이진 트리는 위에서 정의한 트리 중 자식노드를 두개이하로 가지는 것을 말한다. 이들 자식 노드는 좌측자식트리(Left Child)와 우측자식트리(Right Child)라 불리어진다.2. 이진트리 알고리즘■이진트리 클래스 정의public class BinaryTree {public BinaryTree leftChild; // 좌측 자식 노드public BinaryTree rightChild;// 우측 자식 노드public String data; // 데이터■이진트리를 인자로 받아서 복사본을 만들어 주는 메소드(실은 생성자)public BinaryTree(BinaryTree bt){if (bt.leftChild != null)leftChild = new BinaryTree(bt.leftChild);if (bt.rightChild != null)rightChild = new BinaryTree(bt.rightChild);if (data != null)data = new String(bt.data);}■Integer 데이터를 정렬하면서 트리를 구성하는 메소드 : 입력받은 숫자가 현재 자신의 데이터보다 작으면 좌측 자식으로 보내고, 크거나 같으면 우측 자식으로 보낸다.public void sortInteger(int num){if (data==null) {data = new String(Integer.toString(num));}else if (Integer.parseInt(data) > num){if (leftChild==null)leftChild = new BinaryTree(Integer.toString(num));elseleftChild.sortInteger(num);}else {if (rightChild==null)rightChild = new BinaryTree(Integer.toString(num));elserightChild.sortInteger(num);}}■전위운행 메소드 : 데이터읽기->좌측->우측public void preOrder(QueueInterface queue){queue.add(data);if (leftChild!=null)leftChild.preOrder(queue);if (rightChild!=null)rightChild.preOrder(queue);}■중위운행 메소드 : 좌측->데이터읽기->우측public void inOrder(QueueInterface queue){if (leftChild!=null)leftChild.inOrder(queue);queue.add(data);if (rightChild!=null)rightChild.inOrder(queue);}■후위운행 메소드 : 좌측->우측->데이터읽기public void postOrder(QueueInterface queue){if (leftChild!=null)leftChild.postOrder(queue);if (rightChild!=null)rightChild.postOrder(queue);queue.add(data);}}3.이진트리 설계■ 구현환경JBuilder 5.0을 사용하여 코딩하였고, 최대한 객체지향적인 구성을 하도록 노력하였다. 또한 웹상에서 볼 수 있도록 applet으로 구현하였다.■클래스 설계클래스 이름담당 동작부모 클래스Report4웹페이지에서 볼 수 있도록 applet로 구현한 클래스AppletQueueScrollPane입/출력된 숫자를 저장하기 위한 큐 (환상큐 아님)ScrollPaneQueueInterfaceBinaryTree이진트리의 속성과 연산들을 정의4.소스코드 (in java)■BinaryTree.javapublic class BinaryTree {public BinaryTree leftChild;public BinaryTree rightChild;public String data;public BinaryTree() {}public BinaryTree(String data){this.data = new String(data);}public BinaryTree(BinaryTree bt){if (bt.leftChild != null)leftChild = new BinaryTree(bt.leftChild);if (bt.rightChild != null)rightChild = new BinaryTree(bt.rightChild);if (data != null)data = new String(bt.data);}public int search(int num){int value = Integer.parseInt(this.data);int level;if (value==num)return 1;if (value