1.------Pre-and post-secondary, recursive, find the maximum node of the binary tree
/** The largest node of the binary tree looks for the node with the largest value in the binary tree and returns it. */ Public classTreeNode { Public intvalue; PublicTreeNode Lefttreenode; PublicTreeNode Righttreenode; PublicTreeNode (intvalue, TreeNode lefttreenode, TreeNode righttreenode) { This. Value =value; This. Lefttreenode =Lefttreenode; This. Righttreenode =Righttreenode; } } Private StaticTreeNode Maxtreenode; /** Test Center : two fork tree-depth traversal-recursive-pre-order traversal * @param root * @return*/ PublicTreeNode maxnodepre (TreeNode root) {if(Root = =NULL) { return NULL; } Maxtreenode=Root; Nexttreenodepre (root); returnMaxtreenode; } Private voidNexttreenodepre (TreeNode TreeNode) {System. out. println (Treenode.value); if(Treenode.value >maxtreenode.value) {Maxtreenode=TreeNode; } if(Treenode.lefttreenode! =NULL) {nexttreenodepre (Treenode.lefttreenode); } if(Treenode.righttreenode! =NULL) {nexttreenodepre (Treenode.righttreenode); } } /** Test Center : two fork tree-depth traversal-recursive-middle sequence traversal * @param root * @return*/ PublicTreeNode maxnodemid (TreeNode root) {if(Root = =NULL) { return NULL; } Maxtreenode=Root; Nexttreenodemid (root); returnMaxtreenode; } Private voidNexttreenodemid (TreeNode TreeNode) {if(Treenode.lefttreenode! =NULL) {nexttreenodemid (Treenode.lefttreenode); } System. out. println (Treenode.value); if(Treenode.value >maxtreenode.value) {Maxtreenode=TreeNode; } if(Treenode.righttreenode! =NULL) {nexttreenodemid (Treenode.righttreenode); } } /** Test Center : two fork tree-depth traversal-recursion-post-post traversal * @param root * @return*/ PublicTreeNode maxnodeafter (TreeNode root) {if(Root = =NULL) { return NULL; } Maxtreenode=Root; Nexttreenodeafter (root); returnMaxtreenode; } Private voidNexttreenodeafter (TreeNode TreeNode) {if(Treenode.lefttreenode! =NULL) {nexttreenodeafter (Treenode.lefttreenode); } if(Treenode.righttreenode! =NULL) {nexttreenodeafter (Treenode.righttreenode); } System. out. println (Treenode.value); if(Treenode.value >maxtreenode.value) {Maxtreenode=TreeNode; } }
//2----------------------------------------------------------------------------------------------- /** * The binary conversion is given a decimal number n and an integer k, and the decimal number n is converted to a K-binary number. "Method": recursively divide and remainder * @param n * @param k * @return*/ PublicString Hexconversion (intNintk) {StringBuffer resultnumber=NewStringBuffer (); Tentok (Resultnumber, n, K); System. out. println ("N:k:result:"+ N +" "+ K +" "+resultnumber.tostring ()); returnresultnumber.tostring (); } Private voidTentok (StringBuffer StringBuffer,intNintk) {intintegral = n/K; intmode = nK; Stringbuffer.insert (0, mode); if(Integral >=k) {Tentok (StringBuffer, integral, k); } Else if(Integral >0) {Stringbuffer.insert (0, Integral); } } Private voidtesthexconversion () {hexconversion (3,4); Hexconversion (9,4); Hexconversion ( in,4); Hexconversion (3,5); Hexconversion (9,5); Hexconversion ( in,5); }
3------bit operation for addition
Public intAPLUSB (intAintb) {intSum_without_carry, carry; Sum_without_carry= A^b;//No rounding andcarry = (a&b) <<1;//Rounding if(carry==0) returnSum_without_carry; Else returnAplusb (Sum_without_carry, carry); } Public voidTestaplusb () {intresult = Aplusb (4,19); SYSTEM.OUT.PRINTLN (result); }
(ii) 3 simple algorithm: two fork tree maximum node, decimal turn arbitrary binary, bit operation to realize the addition