Java inserts, finds, traverses, maximums, and minimums on a two-fork search tree

Source: Internet
Author: User

1. First, a class of node objects is required. These objects contain data, data represents the contents of the store, and references to two child nodes of a node

Class Node {public int idata;public double ddata;public node leftchild;public node rightchild;public void Displaynode () {S Ystem.out.print ("{"); System.out.print (IData); System.out.print (","); System.out.print (DData); System.out.print ("}");}}

2. Insert a Node

Find a corresponding node from the root, which will be the parent node of the new node. When the parent node finds it, the new node can connect to its left or right child node, depending on whether the value of the new node is larger or smaller than the parent node's value.

Here is the Insert () method code:

public void Insert (int ID, double dd) {node NewNode = new Node (); newnode.idata = Id;newnode.ddata = dd;if (root = null) roo t = newnode;else {Node current = root; Node Parent;while (true) {parent = Current;if (ID < current.idata) {current = Current.leftchild;if (current = = null) {Pare Nt.leftchild = Newnode;return;}} else {current = current.rightchild;if (current = = null) {parent.rightchild = Newnode;return;}}} End While}//End ELSE}

Here, a new variable, the parent of current, is used to store the last node encountered that is not NULL. This must be done because current will become NULL in the lookup process to find that the previous node it has checked does not have a corresponding child node. If you do not store the parent, you lose the location where you inserted the new node.


3. Find a Node

Public node find (int key) {//Assuming the tree is not empty Node current = Root;while (current.idata! = key) {if (Key < current.idata) Current = CU Rrent.leftchild;elsecurrent = current.rightchild;if (current = = NULL) return null;} return current;}

Node found: If the while loop does not satisfy the condition and exits from the end of the loop, the current idata field and key are equal, and this node is found.

Node not found: If current equals null, the next child node cannot be found in the lookup sequence, reaching the end of the sequence without finding the node to be found, indicating that it does not exist, and returning nulll to indicate the situation.


4, Traversal tree (pre-sequence traversal, middle sequence traversal, post-order traversal)

/** * Pre-sequence traversal * @param localroot */public void Preorder (Node localroot) {if (localroot! = null) {System.out.print (localroot.id Ata+ "");p reorder (localroot.leftchild);p reorder (localroot.rightchild);}} /** * Middle Sequence traversal * @param localroot */public void inorder (Node localroot) {if (localroot! = null) {preorder (localroot.leftchild); System.out.print (localroot.idata+ "");p reorder (localroot.rightchild);}} /** * post-post traversal * @param localroot */public void Postorder (Node localroot) {if (localroot! = null) {Preorder (localroot.leftchild );p reorder (localroot.rightchild); System.out.print (localroot.idata+ "");}}

The simplest way to traverse a tree is to use a recursive method. A recursive method is used to traverse the whole tree to use a node as a parameter. Initializing this node is the root. For example, there are only three things to do in the middle order traversal:

1), call itself to traverse the node's Zuozi

2), Access this node

3), call itself to traverse the right subtree of the node.


5. Find maximum and minimum values

/** * The minimum value in the tree * @return */public node minimum () {node current;current = root; Node last = Null;while (current! = null) {last = Current;current = Current.leftchild;} return last;} /** * The maximum value in the tree * @return */public node maxmum () {node current;current = root; Node last = Null;while (current! = null) {last = Current;current = Current.rightchild;} return last;}


The following is the complete test code:

Package Bintree;class Node {public int idata;public double ddata;public node leftchild;public node rightchild;public void Displaynode () {System.out.print ("{"); System.out.print (IData); System.out.print (","); System.out.print (DData); System.out.print ("}");}} Class Tree {private Node root;public Tree () {root = null;} /** * Find node * @param key * @return */public node find (int key) {//Assuming the tree is not empty node current = Root;while (current.idata! = key) {if (Key < Current.idata) Current = Current.leftchild;elsecurrent = current.rightchild;if (current = = NULL) return null;} return current;} /** * Insert node * @param ID * @param dd */public void insert (int ID, double dd) {node NewNode = new node (); newnode.idata = Id;n Ewnode.ddata = dd;if (root = null) root = newnode;else {Node current = root; Node Parent;while (true) {parent = Current;if (ID < current.idata) {current = Current.leftchild;if (current = = null) {Pare Nt.leftchild = Newnode;return;}} else {current = current.rightchild;if (current = = null) {Parent.rightchild = NewnodE;return;}}} End While}//End else}/** * Pre-order traversal * @param localroot */public void Preorder (Node localroot) {if (localroot! = null) {Sys Tem.out.print (localroot.idata+ "");p reorder (localroot.leftchild);p reorder (localroot.rightchild);}} /** * Middle Sequence traversal * @param localroot */public void inorder (Node localroot) {if (localroot! = null) {preorder (localroot.leftchild); System.out.print (localroot.idata+ "");p reorder (localroot.rightchild);}} /** * post-post traversal * @param localroot */public void Postorder (Node localroot) {if (localroot! = null) {Preorder (localroot.leftchild );p reorder (localroot.rightchild); System.out.print (localroot.idata+ "");}} /** * The minimum value in the tree * @return */public node minimum () {node current;current = root; Node last = Null;while (current! = null) {last = Current;current = Current.leftchild;} return last;} /** * The maximum value in the tree * @return */public node maxmum () {node current;current = root; Node last = Null;while (current! = null) {last = Current;current = Current.rightchild;} return last;}} public class Treeapp {public static void Main (string[] args) {Tree Thetree = new Tree ();/** * 50 */* 25         75 */\ * 12 37 87 */\ * 30 43 93 * \ * */thetree.insert (1.5), Thetree.insert (1.2), Thetree.insert (1.7), Thetree.insert (1 2, 1.5); Thetree.insert (PNS, 1.2); Thetree.insert (1.7); Thetree.insert (1.5); Thetree.insert (33, 1.2); Thetree.insert (1.7), Thetree.insert (1.5), Thetree.insert (97, 1.5); System.out.println ("Insert Complete ~"),//Find root node Noderoot = Thetree.find (50);//Sequence Traversal thetree.inorder (noderoot); System.out.println ();//Minimum Value System.out.println ("Mini:" + Thetree.minimum (). iData);//Max System.out.println ("Max:" + Thetree.maxmum (). iData);}}


Java inserts, finds, traverses, maximums, and minimums on a two-fork search tree

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.