|  | 
 
| #include <iostream> #include <fstream>
 using namespace std;
 ifstream in("input.txt");
 ofstream out("output.txt");
 
 template<class T>
 class BinaryTree;
 
 template<class T>
 class BadInput
 {
 friend class BinaryTree<T>;
 public:
 BadInput()
 {
 cout<<"BadInput"<<endl;
 }
 };
 
 template<class T>
 class BinaryTreeNode
 {
 friend class BinaryTree<T>;
 public:
 BinaryTreeNode()
 {
 LeftChild = RightChild = 0;
 }
 
 BinaryTreeNode(const T&e)
 {
 this->data = e;
 this->LeftChild = 0;
 this->RightChild = 0;
 }
 To
 BinaryTreeNode(const T&e, BinaryTreeNode* l, BinaryTreeNode * r)
 {
 this->data = e;
 this->LeftChild = l;
 this->RightChild = r;
 }
 To
 void SetData(const T&e)
 {
 this->data = e;
 }
 T GetData()
 {
 return this->data;
 }
 private:
 T data;
 BinaryTreeNode<T> *LeftChild;
 BinaryTreeNode<T> *RightChild;
 };
 
 
 template<class T>
 class BinaryTree
 {
 public:
 BinaryTree()
 {
 this->root = 0;
 }
 
 ~BinaryTree()
 {
 }
 
 void MakeTree(const T&element, BinaryTree<T>&left, BinaryTree<T>&right);
 
 void PreOrder( void (*Visit)(BinaryTreeNode<T> *u))
 {
 this->PreOrder(Visit,root);
 }
 
 void PreOutPut(void)
 {
 PreOrder(Output, root);
 cout<<endl;
 }
 
 private:
 BinaryTreeNode<T>* root;
 To
 void PreOrder(void (*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T>* t);
 To
 static void Output(BinaryTreeNode<T>* t)
 {
 out<< t->data << '';
 }
 };
 
 
 template <class T>
 void BinaryTree<T>::MakeTree(const T&element, BinaryTree<T>&left, BinaryTree<T>&right)
 {
 root = new BinaryTreeNode<T>(element, left.root, right.root);
 left.root = 0;
 right.root = 0;
 }
 
 template <class T>
 void BinaryTree<T>::PreOrder(void (*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T>* t)
 {
 if(t)
 {
 Visit(t);
 PreOrder(Visit, t->LeftChild);
 PreOrder(Visit, t->RightChild);
 }
 }
 
 
 void main()
 {
 if (in.fail())
 {
 cout<<"the input.txt is not exist!";
 exit(1);
 }
 
 int iLine;
 in>>iLine;
 To
 int iRealLine = iLine + 1;
 BinaryTreeNode<int> (*piNode)[3] = new BinaryTreeNode<int>[iRealLine][3];
 for(int i = 0; i <iRealLine; i++)
 {
 int a, b, c;
 in>>a >>b >>c;
 piNode[i][0].SetData(a);
 piNode[i][1].SetData(b);
 piNode[i][2].SetData(c);
 }
 
 BinaryTree<int>* piTree = new BinaryTree<int>[iRealLine];
 for(int j = iRealLine-1; j >=0; j--)
 {
 int a = piNode[j][0].GetData();
 int b = piNode[j][1].GetData();
 int c = piNode[j][2].GetData();
 To
 piTree[a].MakeTree(a, piTree[b], piTree[c]);
 }
 
 piTree->PreOutPut();
 }
 
 Data (input.txt)
 
 9
 1 2 3
 2 4 5
 3 6 7
 4 8 9
 5 0 0
 6 0 0
 7 0 0
 8 0 0
 9 0 0
 
 Question: Can't output results, how to change?
 | 
 |