[Javase] Data structure (binary lookup tree-insert node)

Source: Internet
Author: User
Tags comparable

Binary search tree,also known as binary searching tree, is a special two-fork tree, and the node value of Zuozi is less than the node value of the right subtree.

Define a binary lookup tree

Defines the binary tree bstree, which protects the mroot of the root node of the two fork tree bstnode type , defines the inner class Bstnode

Contains several basic information about the two-fork tree:

key--keyword used to sort the nodes of a two-fork lookup tree

left--the child that points to the current node

right--the child pointing to the current node

Parent--points to the parents of the current node

Define Insert Node Method insert (t key), parameter:T key the object to insert

Create a Node object, instantiate a bstnode object, construct a parameter:T Object

Define overloaded methods Insert (Bstree bstree,bstnode bstnode) method, Parameters:bstree Tree object,Bstnode Node Object

Insert a node in two steps,

1. Find the parent node location of the node

2. Insert the node to the left or right position of the parent node

 Public classBstree<textendsComparable<t>> {    PrivateBstnode<t>Mroot; /*** Define binary tree * *@authorTaoshihan *@param<T> **/     Public classBstnode<textendsComparable<t>> {         PublicT Key;  PublicBstnode Parent, left, right;  PublicBstnode (T key, Bstnode parent, Bstnode left, Bstnode right) { This. Key =key;  This. Parent =parent;  This. left =Left ;  This. right =Right ; }    }     Public voidInsert (Bstree bstree, Bstnode Bstnode) {Bstnode parent=NULL; Bstnode x=Bstree.mroot; //to find the insertion position of the Bstnode, the x pointer refers to the         while(X! =NULL) {Parent= x;//The position of X as the parent class of the node            intFlag =BstNode.key.compareTo (X.key); if(Flag < 0) {x=X.left; }Else{x=X.right; }        }        //InsertBstnode.parent =parent; if(parent==NULL) {Bstree.mroot=Bstnode; }Else{            intFlag =BstNode.key.compareTo (Parent.key); if(Flag < 0) {Parent.left=Bstnode; } Else{parent.right=Bstnode; }                }    }    /*** Insert Root node * *@paramKey*/     Public voidInsert (T key) {Bstnode<T> z =NewBstnode<t> (Key,NULL,NULL,NULL); //If the new node fails, it is returned.         if(Z! =NULL) Insert ( This, z); }    /** print "binary lookup tree" * * Key--node key value * Direction--0, indicating that the node is the root node;     *-1, which indicates that the node is the left child of its parent junction;     * 1, which indicates that the node is the right child of its parent node. */    Private voidPrint (bstnode<t> tree, T key,intdirection) {                if(Tree! =NULL) {            if(direction==0)//tree is the root nodeSystem.out.printf ("%2d is root\n", Tree.key); Else                //Tree is a branch nodeSystem.out.printf ("%2d is%2d ' s%6s child\n", Tree.key, Key, Direction==1? " Right ":" Left "); Print (Tree.left, Tree.key,-1); Print (Tree.right,tree.key,1); }    }     Public voidPrint (bstree<t>tree) {        if(Tree.mroot! =NULL) {print (Tree.mroot, Tree.mRoot.key,0); }    }    /**     * @paramargs*/     Public Static voidMain (string[] args) {Bstree tree=NewBstree (); Tree.insert (3); Tree.insert (1); Tree.insert (2); Tree.insert (5); Tree.insert (4); Tree.insert (6);    Tree.print (tree); }}

Output:

3 is root
1 is 3 ' s left child
2 is 1 ' s right child
5 is 3 ' s right child
4 is 5 ' s left child
6 is 5 ' s right child

[Javase] Data structure (binary lookup tree-insert node)

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.