Tree of data structures (Java implementation version)

Source: Internet
Author: User

Implementation features:

1. Implement the tree construction;

2. It realizes the pre-order recursive traversal of the tree, the sequential recursive traversal and the middle sequence recursive traversal.

1 classbinarytreenode{2         intdata;3Binarytreenode Leftnode =NULL, Rightnode =NULL;4         5          Public voidSetbinarytreenode (intdata) {6              This. data =data;7         }8          Public voidSetleftnode (Binarytreenode leftnode) {9              This. Leftnode =Leftnode;Ten         } One          Public voidSetrightnode (Binarytreenode rightnode) { A              This. Rightnode =Rightnode; -         } -     } the  - classbinarytree{ - binarytreenode[] BTN; - Binarytreenode Roonode; +     intnodesize; -      +      PublicBinaryTree (int[] arraynode) { ANodesize =arraynode.length; atBTN =NewBinarytreenode[nodesize]; -          -         //Convert arraynode elements into nodes -          for(inti = 0; i < nodesize; i++){ -Btn[i] =NewBinarytreenode (); - Btn[i].setbinarytreenode (Arraynode[i]); in             if(i = = 0){ -Roonode =Btn[i]; to             } +         } -         //complement the left and right sub-tree nodes of the two-fork tree the          for(intj = 0; J <= (NodeSize-2)/2; J + +){ *Btn[j].setleftnode (btn[2*j + 1]); $Btn[j].setrightnode (Btn[2*j + 2]);Panax Notoginseng         } -     } the     //Recursive method pre-order traversal +     voidPreorder (Binarytreenode btn) { ABinarytreenode root =btn; the         if(Root! =NULL){ + Printnode (root); - inorder (root.leftnode); $ inorder (root.rightnode); $         } -     } -     //recursive method of sequential traversal the     voidinorder (Binarytreenode btn) { -Binarytreenode root =btn;Wuyi          the         if(Root! =NULL){ - inorder (root.leftnode); Wu Printnode (root); - inorder (root.rightnode); About         } $     } -     //Recursive method for sequential traversal -     voidpostorder (Binarytreenode btn) { -Binarytreenode root =btn; A          +         if(Root! =NULL){ the Postorder (root.leftnode); - Postorder (root.rightnode); $ Printnode (root); the         } the     } the     //Print node Information the     Static voidPrintnode (Binarytreenode btn) { -         intA =Btn.data; in System.out.println (a); the     } the } About  the  Public classTree { the      Public Static voidMain (string[] args) { the         int[] Arraynode =New int[]{1,2,3,4,5,6,7,8,9}; +BinaryTree BT =NewBinaryTree (arraynode); -System.out.println ("Inorder:"); the Bt.inorder (bt.roonode);BayiSystem.out.println ("Preorder:"); the Bt.preorder (bt.roonode); theSystem.out.println ("Postorder:"); - Bt.postorder (bt.roonode); -     } the}

Tree of data structures (Java implementation version)

Related Article

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.