A binary tree is defined as a double tree. A double tree is composed of reference (pointer) data and data pointing to the left child node, right child node, and parent node.
Package Datastructure. tree. btree;/*** Binary Tree definition represented by the three-way link * @ author administrator **/public class bintreenode {private object data; // data domain private bintreenode parent; // parent node private bintreenode lchild; // left child private bintreenode rchild; // right child private int height; // The height of the subtree with this node as the root is private int size; // The number of children of the node (including the node itself) Public bintreenode () {This (null);} public bintreenode (Object E) {DATA = E; Height = 0; size = 1; Paren T = lchild = rchild = NULL ;} // ************** node interface method ************** // ***** obtain node data * /public object getdata () {return data;} public void setdata (Object OBJ) {DATA = OBJ;} // ----------- auxiliary method, determine the current node location ----------/*** determine whether a father exists * @ return */Public Boolean hasparent () {return parent! = NULL;}/*** determine whether a left child exists * @ return true if a left child node exists; otherwise, false */Public Boolean haslchild () {return NULL is returned! = Lchild;}/*** determine whether there are right children * @ return */Public Boolean hasrchild () {return null! = Rchild;}/*** determine whether it is a leaf node * @ return */Public Boolean isleaf () {return (! Haslchild ()&&! Hasrchild ();}/*** determine whether it is the left child of a node * @ return */Public Boolean islchild () {return (hasparent () & this = parent. lchild);}/*** determine whether it is the right child of a node * @ return */Public Boolean isrchild () {return (hasparent () & this = parent. rchild;} // -------------- height-related method -----------------/*** take the height of the node, that is, the height of the tree with the node as the root * @ return */Public int getheight () {return height;}/*** update the height of the current node and its ancestor */Public void updateheight () {int Newh = 0; // The new height is initialized to 0, and the height is equal to the greater if (haslchild () newh = math in the left and right subtree plus 1. max (newh, (lchild. getheight () + 1 ));////////////////??? If (hasrchild () newh = math. max (newh, (rchild. getheight () + 1); // compare the height of the first 0 and the height of the left child plus 1, if (newh = height) return; // if the height does not change, return Height = newh; // otherwise, update height if (hasparent () // recursively updates the height of the ancestor parent. updateheight ();} /********** size-related method ************* // **** Number of knots in the tree with the node as the root * @ return */Public int getsize () {return size;}/*** update the current node and descendant Number of the ancestor */Public void updatesize () {size = 1; // initialize to 1, the node itself if (has Lchild () size = size + lchild. getsize (); // Add the size of the Left subtree if (hasrchild () size = size + rchild. getsize (); // Add the size of the right subtree if (hasparent () parent. updatesize ();} /*********** parent-related methods *********** // ***** obtain the parent node * @ return */Public bintreenode getparent () {return parent;}/*** disconnect from parent */Public void sever () {If (! Hasparent () return; If (islchild () parent. lchild = NULL; elseparent. rchild = NULL; parent. updateheight (); // update the height of the parent node and its ancestor parent. updatesize (); // update the size of the parent node and its ancestor parent = NULL ;} // *********** lchild-related methods *********** // **** obtain the left child * @ return */Public bintreenode getlchild () {return lchild;}/*** sets the left child of the current node and returns the original left child * @ Param LC * @ return */Public bintreenode setlchild (bintreenode Lc) {bintreenode oldlc = This. lchild; I F (haslchild () {lchild. Sever () ;}// disconnect the relationship between the current left child and the node if (null! = Lc) {LC. sever (); // judge the relationship between LC and its parent node. This. lchild = Lc; // determine the parent-child relationship LC. parent = this; this. updateheight (); // update the height of the current node and its ancestor. This. updatesize (); // update the current node and its ancestor size} return oldlc; // return the original left child} // *********** rchild-related method ********** // **** to the right child * @ return */Public bintreenode getrchild () {return rchild;}/*** set the current node to the right child and return the original right child * @ Param rc * @ return */Public bintreenode setrchild (bintreenode RC) {bintreenode oldrc = This. R Child; If (hasrchild () {rchild. Sever () ;}// disconnect the relationship between the current right child and the node if (null! = RC) {RC. sever (); // disconnect the relationship between RC and its parent node. This. rchild = RC; // determine the parent-child relationship RC. parent = this; this. updateheight (); // update the height of the current node and its ancestor. This. updatesize (); // update the size of the current node and its ancestor} return oldrc; // return the original right child}/*** override tostring Method */Public String tostring () {return "" + data ;}}
Traversal of a binary tree represented by a triplicate chain
Package Datastructure. tree. btree; import Java. util. *; import Datastructure. common. strategy; import Datastructure. queue. queue; import Datastructure. queue. arrayqueue;/*** traversal of a binary tree represented by a three-way chain * @ author luoweifu **/public class binarytreeorder {private int leafsize = 0; private bintreenode root = NULL; strategy Strategy = new strategyequal ();/*** constructor, input the root node of the tree * @ Param node * root node of the tree */Public binarytreeorder (bintre Enode node) {This. root = node; strategy Strategy = new strategyequal ();} public bintreenode getroot () {return root ;} /*** forward traversal ** @ return returns an iterator container */Public iterator preorder () {list <bintreenode> List = new topology list (); preorderrecursion (this. root, list); return list. iterator ();}/*** recursively define pre-order traversal * @ Param RT * root node * @ Param list * container list */private void preorderrecursion (bintreenode RT, list List) {If (null = RT) return; // recursive base. The empty tree returns the list directly. add (RT); // access the root node preorderrecursion (RT. getlchild (), list); // traverses the left subtree preorderrecursion (RT. getrchild (), list); // traverses the right subtree}/*** in-order traversal ** @ return */Public iterator inorder () {list <bintreenode> List = new vertex list (); inorderrecursion (this. root, list); return list. iterator ();}/*** recursively define the central order traversal * @ Param RT * root node * @ Param list * linkedlist container */private void inorderr Ecursion (bintreenode RT, list) {If (null = RT) return; // recursive base, the empty tree returns inorderrecursion (RT. getlchild (), list); // traverses the left subtree list. add (RT); // access the root node inorderrecursion (RT. getrchild (), list); // traverse the right subtree}/*** then traverse * @ return */Public iterator postorder () {list <bintreenode> List = new writable list (); postorderrecursion (this. root, list); return list. iterator ();}/*** recursively defines post-order traversal * @ Param RT * root node * @ Param list * linked List container */private void postorderrecursion (bintreenode RT, list) {If (null = RT) return; // recursive base, the empty tree returns postorderrecursion (RT. getlchild (), list); // traverses the left subtree postorderrecursion (RT. getrchild (), list); // traverses the right subtree list. add (RT); // access the root node}/*** traverse by layer * @ return */Public iterator levelorder () {list <bintreenode> List = new rule list (); levelordertraverse (this. root, list); return list. iterator ();}/*** uses queues to perform layer-by-layer traversal of Binary Trees * @ Param RT * @ Param list */private void levelordertraverse (bintreenode RT, list) {If (null = RT) return; queue q = new arrayqueue (); q. push (RT); // The root node enters the queue while (! Q. isempty () {bintreenode P = (bintreenode) Q. dequeue (); // retrieve the first node P of the team and access the list. add (p); If (P. haslchild () Q. push (P. getlchild (); // Add the left and right children of P to the queue in sequence if (P. hasrchild () Q. push (P. getrchild () ;}}/*** search for Element E in the tree, return the data element to be searched at the node * @ Param E * @ return the found node */Public bintreenode find (Object E) {return searche (root, e );} /*** recursive search Element E * @ root of the param RT tree * @ Param e the data element to be searched * @ return returns the found node */private bintreenode searche (bintreenode RT, object E) {If (null = RT) return NULL; If (strategy. equal (RT. getdata (), E) return RT; bintreenode v = searche (RT. getlchild (), e); If (null = V) V = searche (RT. getrchild (), e); Return V;}/*** print binary tree * @ return */Public String printbintree () {stringbuilder sb = new stringbuilder (); printbintree (root, 0, Sb); return sb. tostring ();} /*** print Binary Tree ** @ Param btree root node * @ Param n node layers * @ Param Sb string used to save records */private void printbintree (bintreenode btree, int N, stringbuilder SB) {If (null = btree) return; printbintree (btree. getrchild (), n + 1, Sb); For (INT I = 0; I <n; I ++) Sb. append ("\ t"); If (n> = 0) Sb. append (btree. getdata () + "\ n"); printbintree (btree. getlchild (), n + 1, Sb);}/*** calculate the number of leaf nodes * @ return the number of leaf nodes */Public int sizeleaf () {searchleaf (this. root); Return leafsize;}/** Number of leaf nodes * @ Param RT */private void searchleaf (bintreenode RT) {If (null = RT) return; if (RT. isleaf () leafsize ++; else {searchleaf (RT. getlchild (); searchleaf (RT. getrchild ());}}}
Test
Package Datastructure. tree. btree; import Java. util. iterator; public class btreetest2 {// test function // Result: All functions can be implemented. The correct public static void main (string ARGs []) {// construct a binary tree bintreenode roots = new bintreenode (); bintreenode node = new bintreenode (); roots. setdata ('A'); roots. setlchild (New bintreenode ('B'); roots. setrchild (New bintreenode ('C'); node = roots. getlchild (); node. setlchild (New bintreenode ('D'); node. setrchild (New bintreenode ('E'); node = roots. getrchild (); node. setlchild (New bintreenode ('F'); binarytreeorder order = new binarytreeorder (roots); // ------ traverse -------- iterator <bintreenode> iter1 = order. preorder (); system. out. println ("Forward traversal:"); printiterator (iter1); iterator <bintreenode> iter2 = order. inorder (); system. out. println ("sequential traversal:"); printiterator (iter2); iterator <bintreenode> iter3 = order. postorder (); system. out. println ("post-order traversal:"); printiterator (iter3); iterator <bintreenode> iter4 = order. levelorder (); system. out. println ("layered traversal:"); printiterator (iter4); string STR = order. printbintree (); system. out. println ("Print Binary Tree: \ n" + Str); system. out. println ("Number of leaf nodes:" + order. sizeleaf (); bintreenode nodeone = order. find ('E'); system. out. println ("root node data element:" + nodeone. getdata ();} public static void printiterator (iterator <bintreenode> ITER) {While (ITER. hasnext () {system. out. print ("\ t" + ITER. next (). getdata ();} system. out. println ();}}
Result: The traversal in the forward order is as follows:
A BD
E c
F
In-order traversal:
D be
A f
C
Post-order traversal:
D EB
F c
A
Hierarchical traversal:
A BC
D e
F
Print Binary Tree:
C
F
A
E
B
D
Number of leaf nodes: 3
Data Element of the root node: E