1 PackageRen.laughing.datastructure.baseImpl;2 3 ImportRen.laughing.datastructure.base.Node;4 /**5 * Binary tree Storage structure node definition: three-fork linked list6 * Three pointer fields include parent node, left child, right child7 * @authorLaughing_lz8 * @time April 13, 20169 */Ten Public classBintreenodeImplementsnode{ One PrivateObject data;//data fields A PrivateBintreenode parent;//parent Node - PrivateBintreenode Lchild;//left child - PrivateBintreenode Rchild;//Right Child the Private intHeight//The height of the subtree with this node as its root - Private intSize//the number of descendants of the node, including the node itself . - - PublicBintreenode () { + This(NULL); - } + A PublicBintreenode (Object data) { at This. Parent =NULL; - This. lchild =NULL; - This. rchild =NULL; - This. Size = 1; - This. Height = 0; - This. data =data; in } - to @Override + PublicObject GetData () { - returndata; the } * $ @OverridePanax Notoginseng Public voidsetData (Object obj) { -data =obj; the } + // has A Public Booleanhasparent () { the returnParent! =NULL; + } - Public BooleanHaslchild () { $ returnLchild! =NULL; $ } - Public BooleanHasrchild () { - returnRchild! =NULL; the } - // isWuyi Public Booleanisleaf () { the return!haslchild () &&!hasrchild (); - } Wu Public BooleanIslchild () { - returnHasparent () && This==Parent.lchild; About } $ Public BooleanIsrchild () { - returnHasparent () && This==Parent.rchild; - } - //Get A Public intgetheight () { + returnheight; the } - Public intGetSize () { $ returnsize; the } the PublicBintreenode Getlchild () { the returnLchild; the } - PublicBintreenode Getrchild () { in returnRchild; the } the PublicBintreenode getParent () { About returnparent; the } the the //operate + //★ Update Sub-tree heights with nodes as Root - Public voidupdateheight () { the intNEWH = 0;Bayi if(Haslchild ()) { theNEWH = Math.max (NEWH, Getlchild (). GetHeight () +1); the } - if(Hasrchild ()) { -NEWH = Math.max (NEWH, Getrchild (). GetHeight () +1); the } the if(NEWH = =height) { the return; the } -Height =NEWH; the if(Hasparent ()) { the This. GetParent (). Updateheight ();//★ Recursive Update parent Node height the }94 } the //update descendants of this node the Public voidupdatesize () { theSize = 1;98 if(Haslchild ()) { AboutSize = size+getlchild (). Size; - }101 if(Hasrchild ()) {102Size= size+getrchild (). Size;103 }104 if(Hasparent ()) { the This. GetParent (). Updatesize ();106 }107 }108 //disconnecting from parent nodes109 Public voidServer () { the if(Hasparent ()) {111 if( This==getParent (). Getlchild ()) { theGetParent (). Lchild =NULL;113 } the if( This==getParent (). Getrchild ()) { theGetParent (). rchild =NULL; the }117 getParent (). Updateheight ();118 getParent (). Updatesize ();119Parent =NULL; - }121 }122 PublicBintreenode Setlchild (bintreenode LC) {123Bintreenode Oldlchild =Lchild;124 if(Haslchild ()) { the lchild.server ();126 }127 if(LC! =NULL){ - lc.server ();129 This. lchild =LC; theLc.parent = This;131 This. Updateheight (); the This. Updatesize ();133 }134 returnOldlchild;135 }136 Publicbintreenode setrchild (Bintreenode RC) {137Bintreenode Oldrchild =Rchild;138 if(Hasrchild ()) {139 rchild.server (); $ }141 if(RC! =NULL){142 rc.server ();143 This. rchild =RC;144Rc.parent = This;145 This. Updateheight ();146 This. Updatesize ();147 }148 returnOldrchild;149 } Max}
9. Binary Tree storage structure node definition: three-fork linked list