Binary tree - Java implementation

package struct;

interface Tree{
	//insert element
	void insert(int value);
	// in-order traversal
	void inOrder();
	// preorder traversal
	void perOrder();
	// post-order traversal
	void postOrder();
	//level order traversal
	//void levelOrder();
	// find the minimum value
	int getMinValue();
	// find the minimum value
	int getMaxValue();
	//Delete the specified element
	boolean delete(int value);
	// find the number of elements
	int length();
	// find the height of the tree
	int height();
}

//factory class
class Factory1{
	//Constructor
	private Factory1(){}
	public static Tree getTreeInstance(){
		return new BinaryTreeImpl();
	}
}

class BinaryTreeImpl implements Tree{
	//root node
	private Node root;
	private int size;
	class Node{
		//define the left subtree
		private Node leftChild;
		//define right subtree
		private Node rightChild;
		private int data;
		//The following is the construction method
		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;
		}
	}
	
	//insert element
	public void insert(int data) {
		Node node = new Node(data);
		// empty tree
		if(root == null){
			root = node;
			root.leftChild = null;
			root.rightChild = null;
			size++;
		}else{
			// non-empty tree
			Node current = root;
			Node parent = null;
			while(true){
				if(data < current.data){
					parent = current;
					current = parent.leftChild;
					//The current element is smaller than the root node, the current node is empty
					if(current == null){
						parent.leftChild = node;
						size++;
						break;
					}
				}else if(data > current.data){
					parent = current;
					current = parent.rightChild;
					//The current element is greater than the root node, and the current node is empty, otherwise the loop continues to make the current current the root node
					if(current == null){
						parent.rightChild = node;
						size++;
						break;
					}
				}else{
					System.out.println("have same data in the binary tree;");
				}
			}//end of  while
		}
	}
	// in-order traversal
	public void inOrder() {
		System.out.println("Inorder traversal: ");
		inOrder1(root);
		System.out.println();
	}
	//Inorder traversal recursive function
	private void inOrder1(Node node){
		if( node == null){
			return;
		}
		inOrder1(node.leftChild);
		display(node);
		inOrder1(node.rightChild);
	}
	
	//print function
	private void display(Node node){
		System.out.print(node.data+" ");
	}
	
	// preorder traversal
	public void perOrder() {
		System.out.println("Preorder traversal: ");
		perOrder1(root);
		System.out.println();
	}
	// preorder traversal recursive function
	private void perOrder1(Node node){
		if(node == null){
			return;
		}
		display(node);
		perOrder1(node.leftChild);
		perOrder1(node.rightChild);
	}
	
	// post-order traversal
	public void postOrder() {
		System.out.println("Post-order traversal: ");
		postOrder1(root);
		System.out.println();
	}
	// Subsequent traversal recursive function
	private void postOrder1(Node node){
		if(node == null){
			return;
		}
		postOrder1(node.leftChild);
		postOrder1(node.rightChild);
		display(node);
	}
	//level order traversal
	/*
	public void levelOrder() {
	}
	*/
	// find the minimum value of the element in the tree
	public int getMinValue() {
		Node node = root;
		// empty tree has no minimum element
		if(root == null){
			return -1;
		}
		Node current = node.leftChild;
		while(true){
			if(current.leftChild == null){
				return current.data;
			}
			current = current.leftChild;
		}
	}
	//find the largest element in the tree
	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;
		}
	}
	//remove element from tree
	public boolean delete(int value) {
		return false;
	}
	// find the number of elements in the tree
	public int length(){
		return size;
	}
	// find the height of the tree
	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("==============Test insert function=======================");
		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"+"================Test inOrder function====================== ="+"\n");
		tree.inOrder();
		System.out.println("\n"+"===============测试perOrder函数====================="+"\n");
		tree.perOrder();
		System.out.println("\n"+"================Test postOrder function====================== ="+"\n");
		tree.postOrder ();
		System.out.println("\n"+"================Test getMinValue function====================== ="+"\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());
		}
}



The time is a bit tight, the deletion of the binary tree is more complicated, and I will slowly organize my thoughts. . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325986667&siteId=291194637