Concept
???? Binary sort tree, also called binary search tree. It is either an empty tree or a two-fork tree with the following properties:
① if its left subtree is not empty, the value of all nodes on the left 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 and right sub-trees are also two-fork sorting trees respectively.
Find, insert, and delete operations
package binaryTree;public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; }}
Example
Package Binarytree;import Java.util.arraylist;import Java.util.scanner;public class BST {//If the find succeeds P points to the data element node, otherwise p points to the lookup path On the last node visited; private static TreeNode p; /** * Find operation * Idea: According to the nature of binary sorting tree, the sub-tree is recursively searched; * * @param root * current node; * @param f * Parent nodes of the current node; * @param p * * @param key * Key value */private static Boolean Bst_se Arch (TreeNode Root, TreeNode F, int key) {if (root = = null) {p = f; return false; } else if (key = = Root.val) {p = root; return true; } else if (Key > Root.val) {return Bst_search (root.right, Root, key); } else {return Bst_search (root.left, Root, key); }}/** * insert operation; * Idea: First to find whether the original binary tree exists, there is no insertion, there is no insertion; * * @param key * Key value * * private static Boolean Bst_insert (TreeNode root, int key) {p = null; if (! Bst_search (Root, NULL, key) {//does not exist, then insert, p is the last node to find; TreeNode node = new TreeNode (key); if (p = = null) {//root node; root = node; } else if (P.val < key) {//node as right node; p.right = node; } else {//as left node; p.left = node; } return true; } else {//already exists; return false; }} private static void Clear () {p = null; }/** * Delete operation; * Sub-conditions: 1, the point to be deleted is the leaf node (directly deleted); 2, only the left or right branch (son of Father); 3. Branches exist (search for the middle sequence to delete the predecessor or successor node); * * @param ro OT * current node; * @param key * keywords; * @return */private static Boolean Bst_delete (TreeNode root, int key) {if (root = null) {///The tree is empty, there is no value for key keyword; return false; } else {if (key = = Root.val) {//found; return Deletenode (Root); } else if (Root.val > key) {//to the left subtree to find; return Bst_delete (Root.left, key); } else {//to the right subtree to find; return Bst_delete (Root.right, key); }}}/** * Delete node; * * @param root * @return */private static Boolean Deletenode (Tree Node root) {if (Root.left = = NULL && Root.right = = null) {//leaf node; root = null; } else if (Root.left = = null) {///left dial hand tree is empty, the root node of the right subtree is replaced; root = Root.right; } else if (root.right = = null) {//Right subtree is empty, then the root node of the left subtree is replaced; root = Root.left; } else {///left and right subtree exist;//First find the node in the middle sequence traversal of the junction, that is, the Zuozi of the node at the end; TreeNode f = root; TreeNode p = root node of f.left;//Zuozi; while (p.right! = null) {f = p; p = p.right; }//Find the final precursor F; Root.val = P.val; if (f = = root) {//Reconnect the left subtree of q; f.left = P.left; } else {//Reconnect right subtree of q; f.right = P.left; } p = null; } return TruE }/** * Middle sequence traversal * * @param root */private static void Inordertraverse (TreeNode root, Arraylist<int Eger> list) {if (root = null) {return; } inordertraverse (Root.left, list); List.add (Root.val); Inordertraverse (root.right, list); public static void Main (string[] args) {TreeNode root = null; arraylist<integer> list = new arraylist<integer> (); Int[] arr = {80, 56, 92, 34, 60, 86, 101, 22, 49, 58, 72, 42}; for (int i:arr) {if (root = null) {root = new TreeNode (i);//root node display given separately;; } else {Bst_insert (root, I); }}//original binary sort tree; inordertraverse (root, list); System.out.println ("Before deleting:" +list); Empty p; clear (); Delete 56 of this node; Bst_delete (root, 56); Clear list; List.clear (); The structure has not changed after deletion; Inordertraverse (root, list); SysTem.out.println ("Delete node 56 after:" +list); }}
Operation Result:
Binary sort Tree