help with PRINT TREE function in optimal binary search tree program

here is my code, without the print tree functions i can get the table and root table fine, I don't know why i'm getting this error though, any ideas?

breaks around line 122, thanks!




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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//Optimal BST
#define input 18
#include<iostream>
#include <iomanip>
#include <fstream>
using namespace std;

typedef int Temp[input][input];

char arrayChar[input] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'O', 'Q', 'R'};
int P[input] = {995, 22, 23, 562, 33, 8, 60, 118, 30, 723, 807, 626, 15, 89, 21, 128, 626, 621};
int Root[input][input];
int S[input][input];

int i, j, d, k;
const int n = input - 1;
int minroot, minvalue, psum;


int INF = 1000000;

ofstream outFile;

int OptimalBinarySearchTree(int p[])
{

	for(i = 1; i <= (n+1); i++)
		{
			S[i][i-1] = 0;
		}
    
	for(i = 0; i <= n; i++)
        {    
			S[i][i] = P[i];    
			Root[i][i] = i;    
		}
 
    for(d = 0; d <= (n); d++)
		{
			for(i = 0; i <= (n-d); i++)
				{
					j = i + d;
					minroot = 0; minvalue = INF; psum = 0;
				
					for(k = i; k <= j; k++)
						{
							psum += p[k];
							int value = S[i][k-1] + S[k+1][j];
				
							if( value < minvalue )
								{ 
									minroot = k + 1; 
									minvalue = S[i][k-1] + S[k+1][j]; 
								}
						}
					
					Root[i][j] = minroot;    S[i][j] = psum + minvalue;
				}
		}
 
    return S[1][n], Root[1][n];
}

void Print(int v[input][input])
{
	cout << setw (4) << "-";
	cout << "  ";
	outFile << setw (4) << "-";
	outFile << "  ";
	
	for(int i = 0 ; i <= input - 1 ; i++ )
		{
			cout << setw (5) << arrayChar[i];
			outFile << setw (5) << arrayChar[i];
		}
	
	cout << endl << endl;
	outFile << endl << endl;
	
	for(int i=0;i<=input-1;i++)
		{
			cout << setw (4) << arrayChar[i] << " | ";
			outFile << setw (4) << arrayChar[i] << " | ";
		
			for(int j=0;j<=input-1;j++)
			{
				cout << setw (4) << v[i][j] << " ";
				outFile << setw (4) << v[i][j] << " ";
			}
		
			cout << endl;
			outFile << endl;
		}
}
//PSUEDO CODE FOR PRINTING FUNCTION
//void PrintTree(Temp Root[input][input], int i, int j, int space_needed)
//{
//    if(i <= j)
//    {
//        /* print <space_needed> and then roots[i][j];*/
//
//      /*   PrintTree(Root[][], i, Root[i][j] - 1, ++space_needed);*/
//
//        /* PrintTree(Root[][], Root[i][j] + 1, j, ++space_needed);*/
//    }
//}





void PrintTree(Temp Root, int i, int j, int space)
{
	if(i <= j)
	{
		for(int val = 0; val < space ; val++)
		{
			outFile << '\t';
		}
		outFile << Root[input][input] << endl;
		space++;
		PrintTree(Root, i, Root[i][j]+1, space);//BREAKS HERE!!!!!!!!!!!
		PrintTree(Root, Root[i][j]-1, j, space);
		
	}
	else
	{
		for(int val = 0; val < space; val++)
		{
			outFile << '\t';
		}
		outFile << '-' << endl;
	}
	return;
}

int main()
{
	Temp Root;
	int rootvalue=0;

	OptimalBinarySearchTree(P);

	outFile.open ("tlb0020_project2.txt");
	outFile.clear();
	
	cout << endl << "Main Table" << endl;
	outFile << endl << "Main Table" << endl;
	Print(S);

	cout << endl << "--------------------------------" << endl;
	cout << endl << "Root Table" << endl;
	outFile << endl << "Root Table" << endl;


	for(j=1;j<=input-1;j++)
		{
			for(i=0;i<=input-1;i++)
				{
					Root[0][0] = 1;
					if(Root[i][j] != 0)
						{
							int root = 0;
							rootvalue = Root[i][j];
							Root[i][j] = rootvalue;							

						}
				}
		}

	Print(Root);

	cout << endl;
	cout << endl;

	/*PrintTree(Root, 1, n, 0);*/

	/*PrintTree(Root,i,j);*/

	PrintTree(Root, 1, n, 0);



	outFile.close();

	system("pause");

	return 0;

}
Last edited on
Arrays are indexed starting at 0. So legal index values of S are from S[0][0] to S[17][17]. The last pass through the loop at line 27 will set i=n+1, which is 18, so line 29 will set S[18][17], which is out of bounds.

Once you write outside the bounds of the array, the behavior of the program is undefined.

Dave
so does line 27 need to be changed to i<=n?
also, can you check the print tree function. When i comment out everything that has to do with print tree my program runs fine.


Mainly i just need a working print tree function
Last edited on
Topic archived. No new replies allowed.