Data Structures and Algorithms - binary sort tree learning and Java implementation

Foreword

In the data structure and algorithms, each algorithm has advantages and disadvantages.
Stored in the order: not sort, search more difficult; stored in the order: sort, delete, insert difficulty
chain store: Whether ordering, are more difficult to find.
Based on the above three points, we have a binary sort tree.

Binary sort tree

Tree binary sort (Binary Sort Tree), Also known as a binary search tree (Binary Search Tree), also known as binary search tree. Binary sort tree or an empty tree or a binary tree having the following properties:
(1) If the left subtree is not empty, then the value of the left sub-tree, all the nodes are less than or equal to that of the root node ;
(2) If the right subtree is not empty, the right subtree of nodes all values are equal to or greater than the value of the root node;
(3) left and right sub-trees are binary sort tree;

Binary sort tree structure

package demo9;

public class BinarySortTree {
	Node root;
	/*
	 * 向二叉排序树中添加节点
	 */
	public void add(Node node) {
		//如果是一颗空树
		if(root==null) {
			root=node;
		}else {
			root.add(node);
		}
	}
	/*
	 * 中序遍历二叉排序树
	 */
	public void midShow() {
		if(root!=null) {
			root.midShow(root);
		}
	}
package demo9;

public class Node {
	int value;
	Node left;
	Node right;
	
	public Node (int value) {
		this.value=value;
	}
/*
 * 向子树中添加节点
 */
	public void add(Node node) {
		if(node==null) {
			return;
		}
			//判断传入的节点的值比当前子树的根节点的值是大是小
			//添加的节点比当前结点更小
			if(node.value<this.value) {
				//如果左节点为空
				if(this.left==null) {
					this.left=node;
					//如果不为空
				}else {
					this.left.add(node);
				}
				//右子树
			}else {
				if(this.right==null) {
					this.right=node;
					//如果不为空
				}else {
					this.right.add(node);
				}
			}
		}
/*
 * 中序遍历二叉排序,z中序遍历二叉排序树结果为从小到大的顺序
 */
	public void midShow(Node node) {
		if(node==null) {
			return;
		}
		midShow(node.left);
		System.out.println(node.value);
		midShow(node.right);
	}

Test categories:

public class TestBinarySortTree {
	public  static void main(String[] args) {
	int [] arr=new int [] {7,3,10,12,5,1,9};
	//创建一颗二叉排序树
	BinarySortTree bst=new  BinarySortTree();
	//循环添加
	for(int i:arr) {
		bst.add(new Node(i));
		}
	//查看树中的值
	bst.midShow();

After the above steps, we have successfully created a binary sort tree, and can be judged by a simple test to establish tree is correct, one of the more simple way is to traverse the tree by the order, due to the binary sort tree characteristics, inorder traversal results should be sorted in ascending series of values

Obtaining the minimum value and the maximum value of the tree

Because the characteristics of binary sort tree, which is the minimum value must be in the leftmost subtree, maximum inevitable in the rightmost sub-tree, of course, if it is empty then there is no tree, if it is only with the nodes of the tree, then the minimum and the maximum value is the root node itself

Find a binary sort tree

Find the node contains a certain value, it is relatively simple to find the specific implementation can look at the code:

/*
	 * 查找节点
	 */
	public Node search(int value) {
		if(root==null) {
			return null;
		}else {
			return root.search(value);
		}
	}
/**
	 * 查找节点
	 */
	public Node search(int value) {
		if(this.value==value){
			return this;
	}else if(value<this.value){
		if(left==null) {
			return null;
		}
		return left.search(value);
	}else{
		if(right==null) {
			return null;
		}
		return right.search(value);
		}
	}

Delete binary sort tree

Delete binary sort tree node is the most complex one place, mainly due to the deletion of the time, there are many cases

Deleted node has no left and right sub-tree
node is removed only the left subtree
deleted node has only the right subtree
deleted node has left and right subtrees
first three cases the better deal, make it directly to the father pointing to their children, Finally, a more complex, combined direct look at the code easier to understand comments:


	/**
	 * 删除节点
	 */
	public void delete(int value) {
		if(root==null) {
			return;
		}else {
			//找到这个节点
			Node target=search(value);
			//如果没有这个节点
			if(target==null) {
				return;
			}
			//找到他的父节点
			 Node parent=searchParent(value);
			//要删除的节点是叶子节点
			 if(target.left==null&&target.right==null) {
				 //要删除的节点是父节点的左子节点
				 if(parent.left.value==value) {
					 parent.left=null;
					 //要删除的节点是右节点
				 }else {
					 parent.right=null;
				 }
				 //要删除的节点有两个子节点 
			 }else if(target.left!=null&&target.right!=null) {
				 //删除右子树中值最小的节点,取刚获取到该节点的值
				 int min=deleteMin(target.right);
				 //替换目标节点中的值
				 target.value=min;
				//要删除的节点有一个左子节点或右字节点 
			 }else {
				 if(target.left!=null) {
					 //要删除的节点是左子节点
					 if(parent.left.value==value) {
						 parent.left=target.left;
						 //要删除的节点是右节点
					 }else {
						 parent.right=target.right;
					 }
					 
				//右子节点	 
				 }else {
					 if(parent.right.value==value) {
						 parent.right=target.right;
						 //要删除的节点是右节点
					 }else {
						 parent.right=target.right;
					 }
					 
				 }
			 }
		}
	}
	
	/**
	 * 删除一颗树中最小的值的节点
	 * @param right
	 * @return
	 */
	private int deleteMin(Node node) {
		Node target=node;
		//递归向左找
		while(target.left!=null) {
			target=target.left;
		}
		//删除最小值的这个节点
		delete(target.value);
		return target.value;
	}
	/*
	 * 搜索父节点
	 */
	public Node searchParent(int value) {
		if(root==null) {
			return null;
		}else {
			return root.searchParent(value);
		}
	}
}

/**
	 * 搜索父节点
	 */
	public Node searchParent(int value) {
		if((this.left!=null&&this.left.value==value)||(this.right!=null&&this.right.value==value)) {
			return this;
		}else {
			if(this.value>value&&this.left!=null) {
				return this.left.searchParent(value);
			}else if(this.value<value&&this.right!=null){
				return this.right.searchParent(value);
			}
			return null;
		}
	}
}

to sum up

Mainly reviews the basic principles of binary sort tree, it's not very complicated, and by way of implementation of the code, learned to create binary sort tree, insert, find and delete files. Which deletes the corresponding node can be said to be more complex, a most difficult to understand, in the learning process can be combined in order to draw their own way of understanding.

Published 28 original articles · won praise 5 · Views 3719

Guess you like

Origin blog.csdn.net/qq_42711899/article/details/104587269