Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the binary search Tree of search algorithm by Java

Shulou Source: shulou.com Published: 2022-06-01 12:49:57 10月03日 Update

In this article Xiaobian for you to introduce in detail "Java how to achieve the search algorithm binary search tree", the content is detailed, the steps are clear, the details are handled properly, I hope this "Java how to achieve the search algorithm binary search tree" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

One and two binary search trees insert elements / * user:ypc * date:2021-05-18; * time: 15 int val; Node left; Node right; Node 09; * / class Node {int val; Node left; Node right; Node (int val) {this.val = val;}} public void insert (int key) {Node node = new Node (key); if (this.root = = null) {root = node } Node cur = root; Node parent = null; while (cur! = null) {if (cur.val = = key) {/ / System.out.println ("element already exists"); return;} else if (cur.val > key) {parent = cur; cur = cur.left } else {parent = cur; cur = cur.right;}} if (key > parent.val) {parent.right = node;} else {parent.left = node;}} II. Search the specified node public boolean search (int key) {Node cur = root While (cur! = null) {if (cur.val = = key) {return true;} else if (cur.val > key) {cur = cur.left;} else {cur = cur.right;}} return false Public void removenode1 (Node parent, Node cur) {if (cur.left = = null) {if (cur = = root) {root = cur.right;} else if (cur = = parent.right) {parent.left = cur.right;} else {parent.right = cur.right }} else if (cur.right = = null) {if (cur = = root) {root.left = cur;} else if (cur = = parent.right) {parent.right = cur.left;} else {parent.left = cur.left }} else {Node tp = cur; Node t = cur.right; while (t.left! = null) {tp = t; t = t.leftt;} if (tp.left = = t) {cur.val = t.val Tp.left = t.right.} if (tp.right = = t) {cur.val = t.vale; tp.right = t.right.} public void remove (int key) {Node cur = root; Node parent = null While (cur! = null) {if (cur.val = = key) {removenode1 (parent, cur); / / removenode2 (parent, cur); return;} else if (key > cur.val) {parent = cur; cur = cur.right } else {parent = cur; cur = cur.left;}} IV. Delete node mode 2: public void removenode2 (Node parent, Node cur) {if (cur.left = = null) {if (cur = = root) {root = cur.right } else if (cur = = parent.right) {parent.left = cur.right;} else {parent.right = cur.right;}} else if (cur.right = = null) {if (cur = = root) {root.left = cur } else if (cur = = parent.right) {parent.right = cur.left;} else {parent.left = cur.left;}} else {Node tp = cur; Node t = cur.left; while (t.right! = null) {tp = t T = t.Rightt;} if (tp.right = = t) {cur.val = t.vale; tp.right = t.leftt;} if (tp.left = = t) {cur.val = t.vale; tp.left = t.left 5. Run result / * user:ypc * date:2021-05-18; * time: 15 public static void main 09; * / class TestBinarySearchTree {public static void main (String [] args) {int a [] = {5, 3, 4, 1, 7, 8, 2, 6, 0, 9}; BinarySearchTree binarySearchTree = new BinarySearchTree (); for (int I = 0; I < a.presidthm; iDepression +) {binarySearchTree.insert (a [I]) } binarySearchTree.inOrderTree (binarySearchTree.root); System.out.println (); binarySearchTree.preOrderTree (binarySearchTree.root); binarySearchTree.remove (7); System.out.println (); System.out.println (after "method 1 deletion"); binarySearchTree.inOrderTree (binarySearchTree.root); System.out.println (); binarySearchTree.preOrderTree (binarySearchTree.root) }} read here, this article "how to realize the binary search tree of Java algorithm" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Search algorithms articles nodes elements contents methods appropriateness ideas new knowledge methods more steps knowledge knowledge points articles details results industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia OPPO Reno Huawei Docker