본문 바로가기

Java/Java 알고리즘

[알고리즘] 4-2. 클래스 이진트리

반응형

1. Node<E> Class

protected static class Node<E> {
 protected E data;
 protected Node<E> left;
 protected Node<E> right;
 
 public Node(E data) {
  this.data = data;
  left = null;
  right = null;
 }
 
 public String toString() {
  return data.toString();
 }
}

2. BinaryTree<E> Class


public class BinaryTree<E> {
	protected static class Node<E> {
		protected E data;
		protected Node<E> left;
		protected Node<E> right;

		public Node(E data) {
			this.data = data;
			left = null;
			right = null;
		}

		public String toString() {
			return data.toString();
		}
	}

	protected Node<E> root;

	public BinaryTree() {
		root = null;
	}

	protected BinaryTree(Node<E> root) {
		this.root = root;
	}

	public BinaryTree(E data, BinaryTree<E> leftTree, 
	 BinaryTree<E> rightTree) { 
	 root = new Node<E>(data);
	 if (leftTree != null) 
	 root.left = leftTree.root;
	 else 
	 root.left = null;
	 if (rightTree != null) 
	 root.right = rightTree.root; 
	 else 
	 root.right = null; 
	 
	 public BinaryTree<E> getLeftSubtree() { 
	 if (root != null && root.left != null) 
	 return new BinaryTree<E>(root.left); 
	 else 
	 return null 
	}
	public BinaryTree<E> getLeftSubtree() { 
	// getLeftSubtree()와 대칭적
	}
	}

}
반응형