General C++ Tree

Hey C++ guru's...

I would really appreciate some help...

I'm building a general tree (a tree with one root and N children), I've written the code and compiled it quote/un-quote successfully... ...however, I can't seem to get my delete function working properly -- what I want to do is pass in a transactionID per "that" and delete "it" and all of "its" children...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
///////////////////////// GENERALTREE.h /////////////////////////
#ifndef GENERALTREE_h
#define GENERALTREE_h

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

class GenTreeNode{
	public:
		int transactionID, totalNumChildren;
		GenTreeNode *childrenPtr;

		std::string toString(){
			std::ostringstream oss;
			oss<<transactionID<<' '<<frequency;

			return oss.str();
		}

		void print(){
			cout<<toString()<<endl;
		}

		void printNode() {
			print();
			for(int i=0; i<totalNumChildren; i++){
				childrenPtr[i].printNode();
			}
		}

		GenTreeNode(): transactionID(0),totalNumChildren(0){
			childrenPtr = NULL;
		}

		GenTreeNode(int id, int cNo){
			transactionID = id;
			totalNumChildren = cNo;
			childrenPtr = new GenTreeNode[totalNumChildren];
		}

		void setTreeNode(int id, int cNo){
			transactionID = id;
			totalNumChildren = cNo;
			childrenPtr = new GenTreeNode[totalNumChildren];
		}

		~GenTreeNode(){
			if(childrenPtr != NULL){
				delete[] childrenPtr;
			}
		}

		void setChildren(int *tranID, int *cNos){
			for(int i=0; i<totalNumChildren; i++){
				childrenPtr[i].setTreeNode(tranID[i], cNos[i]);
			}
		}

		void delChildren(GenTreeNode *ChildPtr){
			if(totalNumChildren){
				delete[] childrenPtr;
			}
		}
};

class GeneralTree{
	public:
		//initialize root
		GenTreeNode root;

		GeneralTree(){}

		virtual ~GeneralTree(){}

		void setTreeChildren(int *tranIDs, int *cNos){
			root.setTreeNode(*tranIDs, *cNos);
			root.setChildren(tranIDs, cNos);
		}

		void printTree() {
			root.printNode();
		}
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
///////////////////////// MAIN.cpp /////////////////////////
#include <iostream>
#include <fstream>
#include "GeneralTree.h"

using namespace std;

int main(){
	GeneralTree cTree;

	int tIDs[] = { 100,200,300,400,500,600,700,800,900 };
	int numOfChilds[] = { 9,4,2,2,4,6,8,6,4 };

	cTree.setTreeChildren(tIDs, numOfChilds);
	cTree.printTree();

	return 0;
}


************************************

Here's the find function that I was working on...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
GenTreeNode findNode(int tranId){
	// check if transactionID equals to the tranId - if yes return this node
	if(transactionID == tranId){
		return node;
	}
			
	if (childrenPtr != NULL){
		// get node from recursive calling find 
		node = findNode(tranId); 
			
		// if the node was found we are thru
		if(node != NULL) 
			return node;
	}
}


Any suggestions would be GREATLY appreciated...
Last edited on
Have a tree node point to its first child and then its oldest sibling younger than it.
Topic archived. No new replies allowed.