Implement binary sorting tree in Java

Source: Internet
Author: User

In computer science, tree is a very important data structure and is widely used. For example, the directory structure in Linux can be regarded as a tree, in addition, a tree is a solution for storing a large amount of data. The binary sorting tree is a special case of a tree. Each of its nodes can have only two subnodes, at the same time, the left subtree has fewer nodes than its parent node, and the right subtree has more nodes than its parent node. The binary tree is widely used in search, at the same time, a variant of the binary sorting tree (the red and black trees) is the basis for implementing treemap and treeset in Java. Below is the definition of the binary sorting tree. Two classes are used. One is the node class, which represents the nodes in the tree, and the other is the name class, which represents the data of the nodes, the name class implements the comparable interface to compare the node size.

Public class binarysearchtree {private node root; private int size; Public binarysearchtree (node root) {This. root = root; size ++;} public int getsize () {return this. size;} public Boolean contains (name) {return contains (name, this. root); // return false;} private Boolean contains (name N, node root) {If (root = NULL) {return false;} int compare = n. compareto (root. element); If (compare> 0) {If (root. right! = NULL) {return contains (n, root. Right) ;}else {return false ;}} else if (compare <0) {If (root. Left! = NULL) {return contains (n, Root. left) ;}else {return false ;}} else {return true ;}} public Boolean insert (name N) {Boolean flag = insert (n, this. root); If (FLAG) size ++; return flag;} private Boolean insert (name N, node root) {If (root = NULL) {This. root = new node (n); Return true;} else if (root. element. compareto (n)> 0) {If (root. left! = NULL) {return insert (n, Root. left);} else {root. left = new node (n); Return true ;}} else if (root. element. compareto (n) <0) {If (root. right! = NULL) {return insert (n, Root. right);} else {root. right = new node (n); Return true ;}} else {root. frequency ++; return true ;}} public Boolean remove (name) {root = remove (name, this. root); If (root! = NULL) {size --; return true;} return false;} private node remove (name, node root) {int compare = root. element. compareto (name); If (compare = 0) {If (root. frequency> 1) {root. frequency --;} else {/** based on the type of the deleted node, divided into the following situations ** ① if the deleted node is a leaf node, directly Delete ** ② If the deleted node contains a subnode, direct the pointer to the node to its subnode ** ③ if the deleted node contains two subnodes, find the largest node with the left word count and replace it with **/If (root. left = NULL & root. right = NULL) {root = NULL;} else if (root. left! = NULL & root. Right = NULL) {root = root. Left;} else if (root. Left = NULL & root. Right! = NULL) {root = root. Right;} else {// The deleted node contains two subnodes: node newroot = root. Left; while (newroot. Left! = NULL) {newroot = newroot. left; // find the maximum node of the Left subtree} root. element = newroot. element; root. left = remove (root. element, Root. left) ;}} else if (compare> 0) {If (root. left! = NULL) {root. Left = remove (name, root. Left) ;}else {return NULL ;}} else {If (root. Right! = NULL) {root. right = remove (name, Root. right);} else {return NULL;} return root;} Public String tostring () {// The return tostring (Root) Order of the nodes in the tree can be output through the middle-order traversal );} private string tostring (node N) {string result = ""; if (n! = NULL) {If (N. Left! = NULL) {result + = tostring (N. Left);} result + = n. Element + ""; if (N. Right! = NULL) {result + = tostring (N. Right) ;}} return result ;}}

In the binary sorting tree operation, the most difficult to process when a node is deleted is divided into many cases for processing separately. Below are the definitions of the node class and name class:

 
Class node {public name element; Public node left; Public node right; Public int frequency = 1; Public node (name N) {This. element = n ;}} class name implements comparable <Name> {private string firstname; private string lastname; public name (string firstname, string lastname) {This. firstname = firstname; this. lastname = lastname;} public int compareto (name N) {int result = This. firstname. compareto (N. firstname); return r Esult = 0? This. lastname. compareto (N. lastname): result;} Public String tostring () {return firstname + "-" + lastname ;}}

The last is the binary sorting tree test:

Public static void main (string [] ARGs) {// system. out. println ("sunzhenxing"); node root = new node (new name ("sun", "zhenxing5"); binarysearchtree BST = new binarysearchtree (Root); BST. insert (new name ("sun", "zhenxing3"); BST. insert (new name ("sun", "zhenxing7"); BST. insert (new name ("sun", "zhenxing2"); BST. insert (new name ("sun", "zhenxing4"); BST. insert (new name ("sun", "zhenxing6"); BST. insert (new name ("sun", "zhenxing8"); system. out. println (BST); BST. remove (new name ("sun", "zhenxing2"); system. out. println (BST); BST. remove (new name ("sun", "zhenxing7"); system. out. println (BST );}

The test output is:

Sun-zhenxing2 sun-zhenxing3 sun-zhenxing4 sun-zhenxing5 sun-zhenxing6 sun-zhenxing7
Sun-zhenxing3 sun-zhenxing4 sun-zhenxing5 sun-zhenxing6 sun-zhenxing7
Sun-zhenxing3 sun-zhenxing4 sun-zhenxing5 sun-zhenxing6

Related Article

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.