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)