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)