Drzewo wyrażeń bez stosu

0

Witam!
Po kompilacji (nie ma błędów na etapie kompilacji, ale po wprowadzeniu danych do konsoli następuje crash) programu dostaję takim powiadomieniem:

"Zgłoszono nieobsługiwany wyjątek: naruszenie dostępu do odczytu.
**node** było 0x20.: wystąpił wyjątek "

Crash następuje, gdy do main'a wrzucę następujące wywołanie:

	t.printTree(w);

Nie wiem jednak czemu tak się dzieje więc przez skopaną implementację nie mogę sprawdzić czy algorytm , który z założenia tworzy drzewo wyrażeń z np.:

string input = "+ 1 2"

działa poprawnie z punktu widzenia logiki. Poniższy kod jest w połowie kodem testowym, który, na tym etapie, ma tylko sprawdzić algorytm tworzenia drzewa więc jestem świadom, że jest dużo błędów typu:

  • pola public zamiast private;
  • braki proceduralności;

ale w tej chwili, próbuję ustalić, co najgorszego z tymi wskaźnikami uczyniłem, że program nie działa (jestem na etapie nauki i pisania na ostatnią chwilę więc uprzedzam, że poniższy kod może zawierać nie jedną głupotę, której nie jestem świadomy)


Wywołanie w main'ie:

int main()
{
	string input;
	cout << "Podaj wyrazenie w ONP: ";
	getline(cin, input);
	input = removeSpaces(input);

	CTree t;
	CNode* w;
	t.buildTree(input);
	w = t.root;
	t.printTree(w);   // <-- to wywołanie crashuje program

    return 0;
}

CNode.h:

#pragma once
#include <iostream>
#include "CTree.h"


using namespace std;



class CNode
{
/*
friend void CTree::BuildTreeH(CNode* &ptr, string input);
friend void CTree::printTree(CNode* &node);



private:
	string data;
	CNode* right;
	CNode* left;
	CNode* parent;

	*/
public:
	string data;
	CNode* right;
	CNode* left;
	CNode* parent;

	CNode(string d);
	CNode();
	~CNode();

};

CNode.cpp:

#include "stdafx.h"
#include "CNode.h"


CNode::CNode(string d)
{
	data = d;
	right = NULL;
	left = NULL;
	parent = NULL;
}

CNode::CNode() {
	data = "";
	parent = left =right = NULL;
}


CNode::~CNode()
{
	delete right;
	delete left;
	delete parent;
}

 

CTree.h:

#pragma once
#include <iostream>


using namespace std;

class CNode;

class CTree
{
private:

public:
	void BuildTreeH(CNode* &ptr, string input);

	CNode* root;
	CTree();
	~CTree();
	bool isOperator(string c);
	void buildTree(string input);
	void printTree(CNode* &root);
};


CTree.cpp:

#include "stdafx.h"
#include "CTree.h"
#include <iostream>
#include "CNode.h"
#include <string>

using namespace std;

CTree::CTree()
{
	root = NULL;
}


CTree::~CTree()
{
}

//-----------Helping methods------------------

bool CTree::isOperator(string c) {
	if (c == "+" ||
		c == "-" ||
		c == "*" ||
		c == "/" ||
		c == "sin" ||
		c == "cos") return true;
	return false;
}

//-------------------------------------------

//-------------Main Methods------------------
void CTree::buildTree(string input) {
	BuildTreeH(root, input);
}


void CTree::BuildTreeH(CNode* &ptr, string input) {

	for (int i = 0; i < input.size(); i++) {
		string data = to_string(input.at(0));
		if (root == NULL) {
			root = new CNode(data);
			ptr = root;
		}
		else if (ptr->left == NULL) {
			if (isOperator(data)) {
				CNode* temp = new CNode(data);
				ptr->left = temp;
				temp->parent = ptr;
				ptr = temp;
			}
			else {
				CNode* temp = new CNode(data);
				ptr->left = temp;
				temp->parent = ptr;
			}
		}
		else if (ptr->right == NULL) {
			if (isOperator(data)) {
				CNode* temp = new CNode(data);
				ptr->right = temp;
				temp->parent = ptr;
				ptr = temp;
			}
			else {
				CNode* temp = new CNode(data);
				ptr->right = temp;
				temp->parent = ptr;
			}
		}
		else {   // right, left != NULL
			ptr = ptr->parent;
		}
	}
}


void CTree::printTree(CNode* &node) {
	printTree(node->left);
	printTree(node->right);
	cout << node->data << " ";
}

//-------------------------------------------


0

po dodaniu ifa do funkcji printTree, już się nie wywala. Wydaje mi się, że funkcja rekurencyjnie wywoływała się dla argumentu NULL

void CTree::printTree(CNode* &node) {
	if (node != NULL) {
		printTree(node->left);
		printTree(node->right);
		cout << node->data << " ";
	}
}

Niestety efekt tego jest taki, że po wprowadzeniu inputa "+ 1 2" wyprintowałem coś takiego: "43 43 43" więc chyba print już jest poprawny, ale w budowaniu drzewa coś popsułem.

1 użytkowników online, w tym zalogowanych: 0, gości: 1