1 PackageIYou.neugle.search;2 3 Public classBstree_search {4 classBstree {5 Public intdata;6 PublicBstree left;7 PublicBstree right;8 }9 Ten Public Static voidMain (string[] args) { OneBstree_search BST =NewBstree_search (); A int[] Array =New int[] {50, 30, 70, 10, 40, 90, 80 }; -Bstree Bstree =BST. Createbstree (array); - the BST. Ldr_bst (bstree); - - Booleanbool = BST. Searchbstree (Bstree, 10); -System.out.println ("10 Whether in binary sort tree:" +bool); + -Bstree Adjustbstree = BST. Deletebstree (Bstree, 50); + BST. Ldr_bst (adjustbstree); A } at - //create a two-fork sort tree - PublicBstree Createbstree (int[] Array) { -Bstree Bstree =NULL; - for(inti = 0; i < Array.Length; i++) { - if(Bstree = =NULL) { inBstree =NewBstree (); -Bstree.data =Array[i]; to}Else { + This. Insertbstree (Bstree, array[i]); - } the } * returnBstree; $ }Panax Notoginseng - //recursive creation of left and right sub-trees the Public voidInsertbstree (Bstree Bstree,intkey) { + if(Bstree.data >key) { A if(Bstree.left = =NULL) { theBstree.left =NewBstree (); +BsTree.left.data =key; -}Else { $ Insertbstree (Bstree.left, key); $ } -}Else { - if(Bstree.right = =NULL) { theBstree.right =NewBstree (); -BsTree.right.data =key;Wuyi}Else { the Insertbstree (Bstree.right, key); - } Wu } - } About $ //Middle Sequence Traversal (recursive) - Public voidLdr_bst (Bstree bstree) { - if(Bstree! =NULL) { - Ldr_bst (bstree.left); A System.out.println (bstree.data); + Ldr_bst (bstree.right); the } - } $ the //find whether an element exists in a binary sort tree the Public BooleanSearchbstree (Bstree Bstree,intkey) { the if(Bstree = =NULL) { the return false; - } in if(Bstree.data = =key) { the return true; the } About if(Bstree.data >key) { the returnSearchbstree (Bstree.left, key); the}Else { the returnSearchbstree (Bstree.right, key); + } - } the Bayi //Delete a node in a binary sort tree the PublicBstree Deletebstree (Bstree bstree,intkey) { the if(Bstree = =NULL) { - return NULL; - } the the if(Bstree.data = =key) { the //first case: leaf node the if(Bstree.left = =NULL&& Bstree.right = =NULL) { -Bstree =NULL; the } the //second case: node has left dial hand nodes. the if(Bstree.left! =NULL&& Bstree.right = =NULL) {94Bstree =Bstree.left; the } the //Third case: node has right child nodes the if(Bstree.left = =NULL&& Bstree.right! =NULL) {98Bstree =Bstree.right; About } - //The fourth case: The node has both left dial hand nodes and the right child nodes.101 if(Bstree.left! =NULL&& Bstree.right! =NULL) {102Bstree temp =Bstree.right;103 //find the leftmost node in the right node104 while(Temp.left! =NULL) { thetemp =Temp.left;106 }107 //assigns the left node to the leftmost node in the right node108Temp.left =Bstree.left;109 theBstree =Bstree.right;111 } the 113 returnBstree; the } the the if(Bstree.data >key) {117Bstree.left =Deletebstree (Bstree.left, key);118}Else {119Bstree.right =Deletebstree (Bstree.right, key); - }121 122 returnBstree;123 }124}
Java data structures and algorithms------binary sort tree