Find binary tree (insert, delete, search) implementation

Binary Search Tree, (also: Binary Search Tree, Binary Sort Tree) It is either an empty tree, or a binary tree with the following properties : If its left subtree is not empty, then the left The value of all nodes on the subtree is less than the value of its root node; if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node; its left , the right subtree is also a binary sorted tree , respectively .

 

As shown in the figure:

Based on this feature, it is easy to operate when searching. Start from the root node and search. If the value is greater than the node value, search to the right; if the value is less than the node value, search to the left; if the values ​​are exactly equal, it is found. Is it possible to write code just by looking at it? This search process is very similar to the binary search method, but that is an array structure and this is a tree structure.

      The operations of a binary search tree are basically summarized as: inserting a value, deleting a value, finding a value, and traversing a binary tree.

      Note: Here, deletion is the most troublesome.

      (I originally thought that writing data structures is still the best in C language, which can directly manipulate pointers, clear and efficient, but C has been lost for too long, and now the main purpose is to review the knowledge of data structures and algorithms, so I can only give up With the idea of ​​c, if you need to learn later, first use the most familiar java to implement the code)

      Let's take a look at the specific operation and logic, and paste the code.

 

public class SearchTree {
	private TreeNode root;

	public SearchTree() {
	}

	// inorder traversal (recursion)
	public void midOrder(TreeNode node) {
		if (node == null) {
			return;
		}
		// Continue to traverse the left and right child nodes of this node
		// Output the root node first, because this is a preorder traversal, the root is left and right
		midOrder(node.lchild);
		System.out.println("Binary tree node: " + node.data);
		midOrder(node.rchild);
	}

	public TreeNode putTreeNode(int key) {
		TreeNode node = null;
		TreeNode parent = null;
		if (root == null) {
			node = new TreeNode(0, key);
			root = node;
		}
		// If the root node already exists, start the judgment
		node = root;
		while (node != null) {
			parent = node;
			if (key > node.data) {
				// get the right node
				node = node.rchild;
			} else if (key < node.data) {
				// get the left node
				node = node.lchild;
			} else {
				// if equal, do nothing
				return node;
			}
		}
		// If it jumps out of the loop, it means that this node does not exist and needs to be created
		node = new TreeNode(0, key);
		// Find the parent node of node at this time
		if (key > parent.data) {
			parent.rchild = node;
		} else if (key < parent.data) {
			parent.lchild = node;
		}
		node.parent = parent;
		return node;
	}

	// delete node
	public void deleteNode(int key) {
		TreeNode node = searchNode(key);
		if (node == null) {
			throw new RuntimeException("Find the node does not exist!!");
		}
		delete (node);
	}

	public void delete(TreeNode node) {
		if (node == null) {
			throw new RuntimeException("Delete the node does not exist!!");
		}
		TreeNode parent = node.parent;
		// The node to be deleted has no descendants, delete it directly
		if (node.lchild == null && node.rchild == null) {
			if (parent.lchild == node) {
				parent.lchild = null;
			} else if (parent.rchild == node) {
				parent.rchild = null;
			}
			return;
		}
		// The deleted node has left or right
		if (node.lchild != null && node.rchild == null) {
			if (parent.lchild == node) {
				parent.lchild = node.lchild;
			} else if (parent.rchild == node) {
				parent.rchild = node.lchild;
			}
			return;
		}
		// Deleted node has right or left
		if (node.lchild == null && node.rchild != null) {
			if (parent.rchild == node) {
				parent.rchild = node.rchild;
			} else if (parent.lchild == node) {
				parent.lchild = node.rchild;
			}
			return;
		}
		// The deleted node has left and right children
		// Get the successor node of this node
		TreeNode next = getNextNode(node);// 45
		delete(next);
		node.data = next.data;
	}

	/**
	 * @param node
	 * Find successor nodes
	 * @return
	 */
	public TreeNode getNextNode(TreeNode node) {
		if (node == null) {
			return null;
		}
		if (node.rchild != null) {
			// Find the smallest keyword node of a node (the left tree finds the largest)
			return getMinTreeNode(node.rchild);
		}
		return null;
	}

	private TreeNode getMinTreeNode(TreeNode node) {
		// keep looking for the left child
		if (node == null) {
			return null;
		}
		TreeNode parent = null;
		while (node != null) {
			parent = node;
			node = node.lchild;
		}
		return parent;
	}

	// find node
	public TreeNode searchNode(int key) {
		if (root == null) {
			return null;
		}
		TreeNode node = root;
		while (node != null) {
			if (key > node.data) {
				node = node.rchild;
			} else if (key < node.data) {
				node = node.lchild;
			} else {
				return node;
			}
		}
		// If it jumps out of the above loop, it means that there is no such node. . .
		return null;
	}

	public class TreeNode {
		private int index;
		private int data;
		private TreeNode lchild;
		private TreeNode rchild;
		private TreeNode parent;

		public TreeNode(int index, int data) {
			this.index = index;
			this.data = data;
		}

	}

	public static void main(String[] args) {
		SearchTree searchTree = new SearchTree();
		int[] array = { 50, 30, 15, 45, 60, 55, 70, 58 };
		for (int data : array) {
			searchTree.putTreeNode(data);
		}
		searchTree.midOrder(searchTree.root);
		System.out.println("============");
		searchTree.deleteNode(60);
		searchTree.midOrder(searchTree.root);
	}
}

  

Guess you like

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