In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.