(ii) 3 simple algorithm: two fork tree maximum node, decimal turn arbitrary binary, bit operation to realize the addition

Source: Internet
Author: User
Tags stringbuffer

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

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.