Data structure (Java language)--binarysearchtree simple implementation

Source: Internet
Author: User

One of the important applications of binary trees is their use in lookups. The nature of making a two-fork tree a binary lookup tree is that for each node x in the tree, the value of all items in its left subtree is greater than the items in X. Note that this means that all elements of the tree can be sorted in a consistent way.

A brief description of the operations that are typically performed on a two-fork lookup tree is now given. Note that because of the recursive definition of the tree, it is usually a routine to write these operations recursively. Because the average depth of the binary lookup tree is O (logn), it is generally not necessary to worry about running out of stack space.

A binary lookup tree requires that all items be sorted. To write a generic class, we need to provide an interface to represent this property. This interface is comparable, which tells us that two items in a tree can always be compared using the CompareTo () method. This allows us to determine all possible relationships, especially with CompareTo () returning to equal () to determine equality. The Binarysearchtree class also contains a nested class Binarynode, which is used to represent the nodes of the tree.

Here is the relevant code and implementation:

public class Binarysearchtree<anytype extends Comparable<? Super anytype>> {Private binarynode<anytype> root;private static class Binarynode<anytype> {AnyType Element Binarynode<anytype> left; Binarynode<anytype> right; Binarynode (AnyType theelement) {This (theelement, NULL, NULL);} Binarynode (AnyType theelement, binarynode<anytype> lt,binarynode<anytype> rt) {element = Theelement;left = lt;right = RT;}} Public Binarysearchtree () {makeempty ();} /** * Make the tree empty tree */public void Makeempty () {root = null;} /** * Whether the tree is empty tree * * @return is empty */public boolean isEmpty () {return root = null;} /** * Whether the tree has a node with parameter values * * @param value * Element value * @return contains the element */public Boolean contains (AnyType value) {return Contains (value, root);} /** * A node and its child nodes have nodes with parameter values * * @param value * Element value * @param node * node * @return */private Boolean Contains (AnyType value, binarynode<anytype> node) {if (node = = null) {return false;} int compareresult =Value.compareto (node.element); if (Compareresult < 0) {//Insert node value is less than node value, recursively finds the left subtree under return contains (value, Node.left);} else if (Compareresult > 0) {//Insert node value is greater than node value, recursively finds the right subtree under return contains (value, node.right);} else {return true;}} /** * Find the minimum element value of the tree * * @return The minimum element value */public AnyType findmin () {if (IsEmpty ()) {throw new NullPointerException ();} return Findmin (root). element;} /** * Find the smallest element in a node and its subtree * * @param node * parent node * @return Smallest element */private binarynode<anytype> findmin (Bin Arynode<anytype> node) {if (node = = null) {return null;} else if (Node.left = = null) {return node;} Return Findmin (node.left);} /** * Find the maximum element value of the tree * * @return The maximum element value */public AnyType Findmax () {if (IsEmpty ()) {throw new NullPointerException ();} return Findmavalue (root). element;} /** * Find the largest element in a node and its subtree * * @param node * parent node * @return Maximum element */private binarynode<anytype> Findmavalue (Bin Arynode<anytype> node) {if (node = = null) {return null;} else if (node.right = = null) {return node;} Return Findmavalue (node.right);} /** * Insert an element into the tree * * @param value * Insert element value */public void Insert (AnyType value) {root = insert (value, root);} /** * Insert an element under a node * * @param value * Element value * @param node * parent node * @return element inserted by */private binarynode& Lt anytype> Insert (AnyType value, binarynode<anytype> node) {if (node = = null) {return new Binarynode<anytype&gt ;(value);} int compareresult = Value.compareto (node.element), if (Compareresult < 0) {Node.left = insert (value, node.left);} else I F (Compareresult > 0) {node.right = insert (value, node.right);} return node;} /** * Remove an element from the tree * * @param value * element value */public void Remove (AnyType value) {root = Remove (value, root);} /** * Delete element under a node * * @param value * Element value * @param node * parent node * @return Delete element nodes */private binarynode& Lt Anytype> Remove (AnyType value, binarynode<anytype> node) {if (node = = null) {return node;} int compareresult = Value.compareto (node.element);(Compareresult < 0) {node.left = remove (value, node.left);} else if (Compareresult > 0) {node.right = remove (value, node.right);} else if (node.left! = null && node.right ! = null) {node.element = Findmin (node.right). Element;node.right = Remove (node.element, node.right);} else {node = (node.l EFT! = NULL)? Node.left:node.right;} return node;} /** * Traverse the output tree */public void Printtree () {if (IsEmpty ()) {System.out.println ("Empty tree");} else {printtree (root)}} /** * Sequence traversal outputs a node under element * * @param node * node */private void Printtree (binarynode<anytype> node) {if (node! = NULL) {Printtree (node.left); System.out.print (Node.element + "");p Rinttree (node.right);}} public static void Main (string[] args) {binarysearchtree<integer> tree = new binarysearchtree<integer> (); Tree.insert (8); Tree.insert (6); Tree.insert (2); Tree.insert (4); Tree.insert (1); Tree.insert (3); Tree.printtree (); Tree.remove (2); Tree.remove (6); Tree.insert (5); Tree.insert (7); System.out.println ("\ n------------"); Tree.printtree ();}} 
Execution Result:


1 2 3 4 6 8
------------
1 3 4 5 7 8

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure (Java language)--binarysearchtree simple implementation

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.