Java Sorting Algorithm _ 009 Binary Search Tree

Source: Internet
Author: User
Package COM. arithmetic; // Binary Search Tree, an empty tree, or a binary tree of the following nature: // if its left subtree is not empty, then the values of all nodes on the left subtree are smaller than the value of its root node; // if its right subtree is not empty, then, the values of all nodes on the right subtree are greater than those on the root node. // Its left and right subtree are also binary sorting trees. Public class test_wzs009 {public static int max = 10; public static int [] DATA = {15, 2, 13, 6, 17, 25, 37, 7, 3, 18 }; // data array public static int counter = 1; public static void main (string ARGs []) {int I; // The cyclic count variable bntreearray bntree = new bntreearray (); // declare the binary tree array bntree. treedata [0] = data [0]; for (I = 1; I <Max; I ++) bntree. create (data [I]); // create a binary search tree int keyValue = 25; // call the binary search method if (bntree. binarysearc H (keyValue)> 0) // The number of output queries. out. println ("search time =" + bntree. binarysearch (keyValue); else // No data is found in the output system. out. println ("No found !! ") ;}} Class bntreearray {public static int maxsize = 20; public static int [] treedata = new int [maxsize]; public static int [] rightnode = new int [maxsize]; public static int [] leftnode = new int [maxsize]; Public bntreearray () {int I; // The cyclic count variable for (I = 0; I <maxsize; I ++) {treedata [I] = 0; rightnode [I] =-1; leftnode [I] =-1 ;}// ------------------------------------------------------ // create a binary tree //------------- ------------------------------------- Public void create (INT data) {int I; // The cyclic count variable int level = 0; // The number of classes in the tree int position = 0; for (I = 0; treedata [I]! = 0; I ++); treedata [I] = data; while (true) // find the node location {// determine whether the left subtree or right subtree if (data> treedata [level]) {// whether the right tree has the next level if (rightnode [level]! =-1) level = rightnode [level]; else {position =-1; // set it to right tree break ;}} else {// whether the left tree has the next level if (leftnode [level]! =-1) level = leftnode [level]; else {position = 1; // set to left Tree break; }}} if (position = 1) // create the left and right links of the node leftnode [level] = I; // link the left subtree elserightnode [level] = I; // link to the right subtree} // returns // Binary Search Method // using public static int binarysearch (INT keyValue) {int pointer; // the current node location int counter; // query times pointer = 0; counter = 0; While (pointer! =-1) {counter ++; // found the node to be searched if (treedata [pointer] = keyValue) return counter; // return query times else if (treedata [pointer]> keyValue) pointer = leftnode [pointer]; // locate elsepointer = rightnode [pointer] from the left subtree; // locate to the right subtree} return 0; // This node is not in this binary tree }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.