1#include <stdio.h>2#include <iostream>3 4 using namespacestd;5 6Template<typename t>classBTree;7 8 /*************************** Node class template *********************************/9Template<typename t>Ten classbtreenode{ OneFriendvoidBtree<t>::P rint (btreenode<t> *t); AFriendvoidBtree<t>::P Reorder (btreenode<t> *t); -FriendintBtree<t>::D eepth (btreenode<t> *t); -FriendvoidBtree<t>::insert (btreenode<t> *t,intn); the - Public: -Btreenode () {data = NULL; left = right =NULL;} -Btreenode (Constt& val) {data = Val; left = right =NULL;} +Btreenode (Constt& Val, btreenode<t> *l, btreenode<t> *r) {data = Val; left = l; right =R;} -~btreenode () {DeleteLeftDeleteRight ;} + A Private: at T data; -Btreenode<t> *Left ; -Btreenode<t> *Right ; - }; - - /*************************** Tree class template **********************************/ inTemplate<typename t> - classbtree{ to Public: + voidPrint (btreenode<t> *t); - voidPreorder (btreenode<t> *t); the intDeepth (btreenode<t> *t); * voidInsert (btreenode<t> *t,intn); $ Private:Panax NotoginsengBtreenode<t> *Root; - }; the +Template<typename t> A voidBtree<t>::P rint (btreenode<t> *t) the { +cout << T->data <<Endl; - } $ $ -Template<typename t> - voidBtree<t>::P Reorder (btreenode<t> *t) the { - if(t! =NULL)Wuyi { the Print (t); -Preorder (t->Left ); WuPreorder (t->Right ); - } About } $ -Template<typename t> - intBtree<t>::D eepth (btreenode<t> *t) - { A if(T = =NULL) + { the return 0; - } $ intleft =1; the intright =1; theLeft + = deepth (t->Left ); theRight + = Deepth (t->Right ); the returnLeft >= right?Left:right; - } in the //The right node is bigger than the left. theTemplate<typename t> About voidBtree<t>::insert (btreenode<t> *t,intN) the { the if(N <0) the { + return; - } the inti =II;BayiT->left =NewBtreenode<t> (ii++); the //i = II; theInsert (T->left, N-1); -T->right =NewBtreenode<t> (i +Ten); -Insert (T->right, N-1); the}
The friend function can access the private members of the encapsulated class, which undoubtedly destroys the encapsulation of the class. But it does improve the efficiency of many of the openings.
Used to write the list in C, and so on, all use the structure to define a point. In C + +, classes are used instead of structs: indeed, classes and structs are similar, except that classes can have private variables, and structs are public.
It is important to note that this is written when writing two-fork trees.
The code is above. The main is not written.
The first thing to write is a node class:
/*************************** Node class template *********************************/Template<typename t>classbtreenode{friendvoidBtree<t>::P rint (btreenode<t> *t); FriendvoidBtree<t>::P Reorder (btreenode<t> *t); FriendintBtree<t>::D eepth (btreenode<t> *t); FriendvoidBtree<t>::insert (btreenode<t> *t,intn); Public: Btreenode () {Data= NULL; left = right =NULL;} Btreenode (Constt& val) {data = Val; left = right =NULL;} Btreenode (Constt& Val, btreenode<t> *l, btreenode<t> *r) {data = Val; left = l; right =R;} ~btreenode () {DeleteLeftDeleteRight ;}Private: T data; Btreenode<T> *Left ; Btreenode<T> *Right ;};
In addition to the basic declarations, several friend functions are declared to be called after the Btree class, Btreenode simply represents the nodes, and the tree is connected to many nodes. So the following write Btree class, implement some functions of the tree. It is important to note that when you declare a friend, you cannot declare a function of a class or class template like a normal external function, and you need to add domain restrictions. Think about it too, otherwise how can the compiler be called after you want to invoke exactly which function? Because if there is a function of the same name within the class, there will be an accident. So the limit domain must be there!!!
/*************************** Tree class template **********************************/Template<typename t>classbtree{ Public: voidPrint (btreenode<t> *t); voidPreorder (btreenode<t> *t); intDeepth (btreenode<t> *t); voidInsert (btreenode<t> *t,intn);Private: Btreenode<T> *root;}; Template<typename t>voidBtree<t>::P rint (btreenode<t> *t) {cout<< T->data <<Endl;} Template<typename t>voidBtree<t>::P Reorder (btreenode<t> *t) { if(t! =NULL) {Print (t); Preorder (t-Left ); Preorder (t-Right ); }}template<typename t>intBtree<t>::D eepth (btreenode<t> *t) { if(T = =NULL) { return 0; } intleft =1; intright =1; Left+ = Deepth (t->Left ); Right+ = Deepth (t->Right ); returnLeft >= right?left:right;}//The right node is bigger than the left.Template<typename t>voidBtree<t>::insert (btreenode<t> *t,intN) { if(N <0) { return; } inti =II; T->left =NewBtreenode<t> (ii++); //i = II;Insert (T->left, N-1); T->right =NewBtreenode<t> (i +Ten); Insert (t->right, N-1);}
As you can see, the function is declared as normal in the class template, and functions are defined outside the class. But when you use these defined functions in the main function, you will get an error. That is because the class template is not declared when the friend is defined.
The first class template is a very important sentence before Btreenode:
class BTree;
It's perfect. In addition, remember: You should declare before use, otherwise there will be problems with both usage and definition.
I write the program has always been class what, class what, int main () what. In fact, first write the definition of head is a good habit!!!
Friend function calls between C + + class templates