The binary linked list of a binary tree is previously implemented. When the binary linked list is used as the storage structure of the Binary Tree, because each node only has a pointer to its left and right son nodes, therefore, you can only find the Left and Right sons of any node. Generally, it cannot directly find the frontend and successor nodes of the node in a certain traversal order. If you add a pointer to the frontend and successor nodes of each node, the efficiency of the storage space will be reduced.
The binary linked list of N nodes contains N + 1 null pointer. Because a binary linked list containing N nodes contains pointers, each node has a pointer pointing from the parent node to the root node, so n-1 pointers are used in total, therefore, the binary linked list of N nodes contains N + 1 null pointer.
Therefore, we can use these null pointers to the frontend and successor nodes in a certain traversal order. This additional pointer is called a clue, and the binary linked list with a clue is called a clue linked list. The corresponding binary tree is called a threadedbinarytree ). Based on the different nature of the clue, the clue binary tree can be divided into three types: pre-order clue Binary Tree, middle-order clue Binary Tree, and post-order clue binary tree.
The following describes how to implement a binary tree with a forward direction.
Threadnode. h
# Include "stdafx. H "# include <iostream> using namespace STD; Template <typename type> class threadtree; Template <typename type> class threadnode {public: Friend class threadtree <type>; threadnode (): lchild (null), rchild (null), ltag (0), rtag (0) {} threadnode (type item, threadnode <type> * l = NULL, threadnode <type> * r = NULL): Data (item), rchild (R), lchild (L), ltag (0), rtag (0) {} void inorder (); Private: type data; // data t Hreadnode <type> * lchild; // left child threadnode <type> * rchild; // right child int ltag; // left flag (0 indicates left child, 1 indicates front child) int rtag; // right flag (0 points to the right child, 1 points to the next child)}; Template <typename type> void threadnode <type >:: inorder () {If (null! = This) {This-> lchild-> inorder (); cout <"->" <this-> data; this-> rchild-> inorder ();}}
Implementation of a middle-order clue Binary Tree: threadtree. h
# Include "threadnode. H "template <typename type> class threadtree {public: threadtree (): Root (null), pre (null), head (null) {} bool insert (const type item ); void inorder (); // You can traverse the void inthreading () in the left-side Navigation Pane. // you can create an index in the left-side Navigation Pane. Void inthreadvisit (); Private: threadnode <type> * root; // root node threadnode <type> * pre; // always record the threadnode <type> * head; void inthreadbuilt (threadnode <type> * t );}; template <typename type> bool threadtr EE <type >:: insert (const type item) {threadnode <type> * newnode = new threadnode <type> (item); If (root = NULL) {root = newnode; return 1;} threadnode <type> * pmove = root; while (1) {If (item> pmove-> data) {If (null = pmove-> rchild) {pmove-> rchild = newnode; return 1;} pmove = pmove-> rchild ;} else {If (null = pmove-> lchild) {pmove-> lchild = newnode; return 1 ;}pmove = pmove-> lchild ;}}} // create a clue template <typenam E type> void threadtree <type >:: inorder () {This-> root-> inorder (); cout <Endl ;} template <typename type> void threadtree <type >:: inthreading () {head = new threadnode <type>; head-> lchild = head; head-> rtag = 1; if (null! = Root) {head-> lchild = root; Pre = head; inthreadbuilt (Root); pre-> rchild = head; head-> rchild = pre ;}} template <typename type> void threadtree <type>: inthreadbuilt (threadnode <type> * t) {If (null! = T) {inthreadbuilt (t-> lchild); If (t-> lchild = NULL) {T-> lchild = pre; t-> ltag = 1 ;} if (pre-> rchild = NULL) {pre-> rchild = T; pre-> rtag = 1;} Pre = T; inthreadbuilt (t-> rchild );}} template <typename type> void threadtree <type>: inthreadvisit () {If (null = head) return; threadnode <type> * pmove = head-> lchild; while (pmove! = Head) {While (pmove-> ltag = 0) {pmove = pmove-> lchild;} cout <"->" <pmove-> data; while (pmove-> rchild! = Head) & pmove-> rtag = 1) {pmove = pmove-> rchild; cout <"->" <pmove-> data ;} pmove = pmove-> rchild; // enter the right subtree} cout <Endl ;}
Test:
// Threadtree. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include" threadtree. H "int _ tmain (INT argc, _ tchar * argv []) {threadtree <int> tree; int array [] = }; for (INT I = 0; I <8; I ++) {tree. insert (array [I]);} tree. inorder (); tree. inthreading (); tree. inthreadvisit (); Return 0 ;}
Result:
-> 12-> 14-> 16-> 18-> 20-> 54-> 63-> 65
-> 12-> 14-> 16-> 18-> 20-> 54-> 63-> 65
Press any key to continue...
Clue Binary Tree