| |

VerySource

 Forgot password?
 Register
Search
View: 880|Reply: 4

Binary tree establishment problem (seeking help urgently)

[Copy link]

3

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-10-3 23:30:01
| Show all posts |Read mode
#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?
Reply

Use magic Report

0

Threads

19

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-10-3 23:45:01
| Show all posts
Single step debugging.
Reply

Use magic Report

0

Threads

36

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-10-4 01:00:01
| Show all posts
piTree[a].MakeTree(a, piTree[b], piTree[c]);
After the root of piTree is empty
Reply

Use magic Report

3

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-10-4 03:15:01
| Show all posts
Still unclear, how to change it? ?
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-10-4 06:30:01
| Show all posts
Do a few assert yourself first. Determine what the problem is. Then single-step debugging. This physical work can only be done by the host himself
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list