二叉树——Java实现

package struct;

interface Tree{
	//插入元素
	void insert(int value);
	//中序遍历
	void inOrder();
	//先序遍历
	void perOrder();
	//后序遍历
	void postOrder();
	//层序遍历
	//void levelOrder();
	//求最小值
	int getMinValue();
	//求最小值
	int getMaxValue();
	//指定元素删除
	boolean delete(int value);
	//求元素个数
	int length();
	//求树的高度
	int height();
}

//工厂类
class Factory1{
	//构造函数
	private Factory1(){}
	public static Tree getTreeInstance(){
		return new BinaryTreeImpl();
	}
}

class BinaryTreeImpl implements Tree{
	//根节点
	private Node root;
	private int size;
	class Node{
		//定义左子树
		private Node leftChild;
		//定义右子树
		private Node rightChild;
		private int data;
		//以下为构造方法
		public Node(int data){
			this.data = data;
		}
		public Node(Node leftChild, Node rightChild, int data) {
			super();
			this.leftChild = leftChild;
			this.rightChild = rightChild;
			this.data = data;
		}
	}
	
	//插入元素
	public void insert(int data) {
		Node node = new Node(data);
		//空树
		if(root == null){
			root = node;
			root.leftChild = null;
			root.rightChild = null;
			size++;
		}else{
			//非空树
			Node current = root;
			Node parent = null;
			while(true){
				if(data < current.data){
					parent = current;
					current = parent.leftChild;
					//当前元素小于根节点,当前节点为空
					if(current == null){
						parent.leftChild = node;
						size++;
						break;
					}
				}else if(data > current.data){
					parent = current;
					current = parent.rightChild;
					//当前元素大于根节点,且当前节点为空,否则继续循环使当前的current为根节点
					if(current == null){
						parent.rightChild = node;
						size++;
						break;
					}
				}else{
					System.out.println("have same data in the binary tree;");
				}
			}//end of  while
		}
	}
	//中序遍历
	public void inOrder() {
		System.out.println("中序遍历:");
		inOrder1(root);
		System.out.println();
	}
	//中序遍历递归函数
	private void inOrder1(Node node){
		if( node == null){
			return;
		}
		inOrder1(node.leftChild);
		display(node);
		inOrder1(node.rightChild);
	}
	
	//打印函数
	private void display(Node node){
		System.out.print(node.data+" ");
	}
	
	//前序遍历
	public void perOrder() {
		System.out.println("前序遍历:");
		perOrder1(root);
		System.out.println();
	}
	//前序遍历递归函数
	private void perOrder1(Node node){
		if(node == null){
			return;
		}
		display(node);
		perOrder1(node.leftChild);
		perOrder1(node.rightChild);
	}
	
	//后序遍历
	public void postOrder() {
		System.out.println("后序遍历:");
		postOrder1(root);
		System.out.println();
	}
	//后续遍历递归函数
	private void postOrder1(Node node){
		if(node == null){
			return;
		}
		postOrder1(node.leftChild);
		postOrder1(node.rightChild);
		display(node);
	}
	//层序遍历
	/*
	public void levelOrder() {
	}
	*/
	//求取树中元素最小值
	public int getMinValue() {
		Node node = root;
		//空树无最小元素
		if(root == null){
			return -1;
		}
		Node current = node.leftChild;
		while(true){
			if(current.leftChild == null){
				return current.data;
			}
			current = current.leftChild;
		}
	}
	//求树中最大元素
	public int getMaxValue(){
		Node node = root;
		if(node == null){
			return -1;
		}
		Node current = node.rightChild;
		while(true){
			if(current.rightChild == null){
				return current.data;
			}
			current = current.rightChild;
		}
	}
	//删除树中元素
	public boolean delete(int value) {
		return false;
	}
	//求树中元素个数
	public int length(){
		return size;
	}
	//求树的高度
	public int height(){
		if(root == null){
			return 0;
		}
		return height1(root);
	}
	private int height1(Node node) {
		if(node!=null){
			int lheight = height1(node.leftChild);
			int rheight = height1(node.rightChild);	
			return lheight > rheight ? lheight+1:rheight+1;
		}
		return 0;
	}
}
public class BinaryTree {
	public static void main(String[] args) {
		Tree tree = Factory1.getTreeInstance();
		System.out.println("===============测试insert函数=====================");
		tree.insert(2);
		tree.insert(1);
		tree.insert(5);
		tree.insert(20);
		tree.insert(3);
		tree.insert(7);
		tree.insert(0);
		tree.insert(10);
		System.out.println("\n"+"===============测试length函数====================="+"\n");
		System.out.println(tree.length());
		System.out.println("\n"+"===============测试inOrder函数====================="+"\n");
		tree.inOrder();
		System.out.println("\n"+"===============测试perOrder函数====================="+"\n");
		tree.perOrder();
		System.out.println("\n"+"===============测试postOrder函数====================="+"\n");
		tree.postOrder();
		System.out.println("\n"+"===============测试getMinValue函数====================="+"\n");
		System.out.println(tree.getMinValue());
		System.out.println("\n"+"===============测试getMaxValue函数====================="+"\n");
		System.out.println(tree.getMaxValue());
		System.out.println("\n"+"===============测试height函数====================="+"\n");
		System.out.println(tree.height());
		}
}



时间有点紧张,二叉树的删除较复杂,待我慢慢整理思绪。。。

猜你喜欢

转载自blog.csdn.net/qq_40409115/article/details/79965337