473,715 Members | 6,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

level order traversal and a que issues

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#include <string.h>
using namespace std;

#define TRUE 1
#define FALSE 0

struct info_node
{
char code[8] ;
int weight ;
int level ;
struct info_node* father ;
struct info_node* left ;
struct info_node* right ;
struct info_node* next ;

} ;

struct prqueue
{
struct info_node * prqtop ;
} ;

struct lo_queue
{
struct info_node* front ;
struct info_node* rear ;
} ;

void create(struct prqueue* pq);
int empty(struct prqueue* pq);
void insert(struct prqueue* pq, struct info_node* new_node);
struct info_node* remove(struct prqueue* pq);
int oneleft(prqueue * pque);
void visit(info_node * node);
void qInsert( lo_queue* pdq, info_node* node);
int qEmpty(lo_queue * pdq);
void qCreate(struct lo_queue* pdq);
struct info_node* remove(struct prqueue* pq);
void levelOrderTrave rsal(lo_queue* pdq,info_node* root, int level);
void levelorder(lo_q ueue* pdq,info_node* root);
void main(void)
{
FILE* infile;
struct prqueue pque ;
struct lo_queue que ;
struct prqueue* pmain_queue ;
struct info_node* pleaf_location[7] ;
struct info_node* tree_root ;
struct info_node* new_node;
struct info_node* move;
struct info_node* test;
int count=0,level=0 ;
int numAllow = 2*level;

create(&pque);
qCreate(&que);
new_node = NULL;
test = NULL;
infile = fopen("c:/prq_data.txt", "r");
struct info_node* p1 ; // can be used in the loop which
removes 2
nodes from
struct info_node* p2 ; // your priority queue
new_node = ( struct info_node* ) malloc( sizeof ( struct
info_node )
);
test = ( struct info_node* ) malloc( sizeof ( struct info_node
) );
//fscanf(infile," %s %d ",new_node->code,&new_no de->weight);
while(!feof(inf ile))
{
new_node = ( struct info_node* ) malloc( sizeof (
struct info_node )
);
fscanf(infile," %s%d
",new_node->code,&new_no de->weight);
pleaf_location[count++] = new_node;
insert(&pque,ne w_node);
}
move = pque.prqtop;
// remove 2
while(!oneleft( &pque))
{
new_node = ( struct info_node* ) malloc( sizeof (
struct info_node )
);// create new node
p1 = ( struct info_node* ) malloc( sizeof ( struct
info_node ) ); //
create node holder 1
p2 = ( struct info_node* ) malloc( sizeof ( struct
info_node ) );//
create node holder 2
p1 = remove(&pque);// remove first node out of pque
p2 = remove(&pque); // remove second node out of pque
strcpy(new_node->code, p1->code);// copy
new_node->code, from the
first node
strcat(new_node->code, p2->code); // copy
new_node->code, from the
second node
cout << "newNode Code" << new_node->code << endl;
new_node->weight = p1->weight + p2->weight;// add the weight
together
new_node->father = new_node; // initialize the father
cout << "left p1->code = " << p1->code << endl;
cout << "right p2->code = " << p2->code << endl;
new_node->left = p1;// itialize the left
new_node->right = p2; //intialize the right
insert(&pque,ne w_node);//insert it into the que
}

// the last node will be the root
move = pque.prqtop;
tree_root = remove(&pque);
//tree_root = remove(&pque);
//visit(tree_root );
levelorder(&que , tree_root);

printf("End of program: ");
getch();

} // end of function main()

void create(struct prqueue* pq)
{
pq->prqtop = NULL;
}

void qCreate(lo_queu e *pdq)
{
pdq->front = NULL;
pdq->rear = NULL;
}

int empty(struct prqueue* pq)
{
if(pq->prqtop == NULL)
{
return(1);
}
else
{
return(0);
}
}

void insert(struct prqueue* pq, struct info_node* new_node)
{
struct info_node* move;
struct info_node* pre_pntr;

move = pq->prqtop;
pre_pntr = pq->prqtop;
if(empty(pq))//check for empty
{
new_node->next = NULL;
pq->prqtop = new_node;
}
else
{
while(move != NULL && new_node->weight move->weight)
{
pre_pntr = move;
move = move->next;
}
if(move == pq->prqtop)
{
new_node->next = move;
pq->prqtop = new_node;
}
else if(move == NULL)
{
new_node->next = NULL;
pre_pntr->next = new_node;
}
else
{
new_node->next = move;
pre_pntr->next = new_node;
}
}
}

struct info_node* remove(struct prqueue* pq)
{
struct info_node* remove;
remove = NULL;
if(empty(pq))
{
printf("ERROR EMPTY!!!");
}
else if(pq->prqtop->next == NULL)
{
remove = pq->prqtop;
pq->prqtop = NULL;
}
else
{
remove = pq->prqtop;
pq->prqtop = pq->prqtop->next;
}
return(remove);
//printf("%s %d\n",remove->code,remove->weight);
}

int oneleft(prqueue *pque)
{
info_node* node;
node = remove(pque);
if(empty(pque))
{
insert(pque,nod e);
return(1);
}
else
{
insert(pque,nod e);
return(0);
}
}

void visit(info_node * node)
{
if(node != NULL)
printf("%s\n", node->code) ;
}// end of function visit()

info_node* qRemove(lo_queu e* pdq)
{
info_node* auxinfo;
if(qEmpty(pdq))
{
cout << "priority que empty" << endl;
}
else if(pdq->front->next == NULL)
{
auxinfo = pdq->front;
pdq->front = NULL;
pdq->rear = NULL;
}
else
{
auxinfo = pdq->front;
pdq->front = pdq->front->next;
}
return(auxinfo) ;

}

void qInsert(lo_queu e* pdq, info_node* node )
{
if(qEmpty(pdq))
{
pdq->front = node;
pdq->rear = node;
}
else
{
pdq->rear->next = node;
pdq->rear = node;
}
}

int qEmpty(lo_queue * pdq)
{
if(pdq->front == NULL && pdq->rear == NULL)
{
return(1);
}
else
{
return(0);
}
}

void levelorder(lo_q ueue* pdq,info_node* root)
{

int testCounter = 0;
if(root == NULL)
cout << " ";

else{

cout << " ";

// Insert the first node

qInsert(pdq,roo t);

// The level we're at in the tree

int level = 0;
//cout << "level" << level << endl;

// The number of items we can take out (Binary Tree = 2 *
level)

int numAllow = 2 * level;

while(!qEmpty(p dq)){
while(numAllow < 2*level)
{
info_node* temp = qRemove(pdq);
cout << temp->code << " ";

if(temp->left != NULL)
{

qInsert(pdq, temp->left);
} // end if()

if(temp->right != NULL)
{
qInsert(pdq, temp->right);
} // end if()

// We've output one of our allowed
elements

numAllow++;
//cout << "numallow = " << numAllow << endl;

} // end while()
//cout << "loop" << endl;
cout << "\n ";
level++;

} // while()

cout << " ";

} // end else()
} // end breadthFirstStr ing()

//my problem is with my level order function which keeps giving me this
error Unhandled exception //at 0x00402008 in meteor.exe: 0xC0000005:
Access violation writing location 0xbaadf029. I dont //see where i am
stepping out of my boundry, the input file is
//A 8
//B 1
//C 3
//D 5
//E 12
//F 4
//G 2
//any help would be great

Dec 2 '06 #1
7 2832
On 2006-12-02 19:49, GrispernMix wrote:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#include <string.h>
using namespace std;

#define TRUE 1
#define FALSE 0
[....]

First of all, this does not look like C++ to me, it looks like C with a
few usages of C++-features. If it was C++ you should include <string>
and not <string.h(whi ch you includes twice) and use bool instead of
defining TRUE and FALSE, and even in C there is <stdbool.hwhi ch you
could use.

Now, it seems like you are having trouble with a queue, and unless you
have a very pressing need to make one yourself (in which case I can't
help you) I would recomend to take a look at std::queue (you get it by
including <queue>).

If you must use your implementation all the help I can give you is
saying that your problem most likely comes from a stray pointer you are
trying to use, running your app in a debuger might give you some help.

--
Erik Wikström
Dec 2 '06 #2
On 2 Dec 2006 10:49:30 -0800 in comp.lang.c++, "GrispernMi x" <Gr*********@gm ail.comwrote,
>//any help would be great
Dude, sorry, but your code sucks.
Maybe I'll have more time later, but for now, numallow++ is wrong.
Reenable cout << numallow.
And initialize ALL your variables, uninitialized variables are death.

Dec 2 '06 #3

GrispernMix wrote:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#include <string.h>
using namespace std;

#define TRUE 1
#define FALSE 0
<snip>

I'll second that - the code sucks. This is the perfect example on how
not to code.
If you fail to initialize pointers (a coder's ultimate nemesis) be
prepared to suffer the consequences.
Just in case you didn't get it -initialize ALL your variables. And by
ALL we mean every single one.
hint -use ctors + init list.
Prefer references over pointers.
pointer == bugs. Uninitialized pointers is a disease.
void main() is illegal (even in C).

Dec 3 '06 #4
On Sat, 02 Dec 2006 21:11:49 GMT in comp.lang.c++, David Harmon <so****@netcom. comwrote,
>Maybe I'll have more time later,
OK, now is later. You still here?
Here is a chopped and reformed version of your code.
There are plenty of things I don't like about it, but I think the
changes I made should bust the debugging roadblock you hit.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#include <string.h>
using namespace std;

#include <assert.h>

#define TRUE 1
#define FALSE 0

struct info_node
{
char code[8];
int weight;
int level;
info_node * father;
info_node * left;
info_node * right;
info_node * next;
};

info_node *malloc_node()
{
info_node * new_node = (info_node *)malloc(sizeof (info_node));
memset(new_node , 0, sizeof(info_nod e));
return new_node;
}
struct prqueue
{
info_node * prqtop;
};

struct lo_queue
{
info_node * front;
info_node * rear;
};

/* Please remove unnecessary forward declarations.
void create(prqueue * pq);
int empty(prqueue * pq);
void insert(prqueue * pq, info_node * new_node);
info_node * remove(prqueue * pq);
int oneleft(prqueue * pque);
void visit(info_node * node);
void qInsert( lo_queue * pdq, info_node * node);
int qEmpty(lo_queue * pdq);
void qCreate(lo_queu e * pdq);
info_node * remove(prqueue * pq);
void levelOrderTrave rsal(lo_queue * pdq, info_node * root, int level);
void levelorder(lo_q ueue * pdq, info_node * root);
*/
void create(prqueue * pq)
{
assert(pq);
pq->prqtop = NULL;
}

void qCreate(lo_queu e * pdq)
{
assert(pdq);
pdq->front = NULL;
pdq->rear = NULL;
}

int empty(prqueue * pq)
{
assert(pq);
if (pq->prqtop == NULL) {
return 1;
} else {
return 0;
}
}

void insert(prqueue * pq, info_node * new_node)
{
assert(new_node );
assert(pq);
info_node * move = pq->prqtop;
info_node * pre_pntr = pq->prqtop;

if (empty(pq)) { //check for empty
new_node->next = NULL;
pq->prqtop = new_node;
} else {
while (move != NULL && new_node->weight move->weight) {
pre_pntr = move;
move = move->next;
}
if (move == pq->prqtop) {
new_node->next = move;
pq->prqtop = new_node;
} else if (move == NULL) {
new_node->next = NULL;
pre_pntr->next = new_node;
} else {
new_node->next = move;
pre_pntr->next = new_node;
}
}
}

info_node * remove(prqueue * pq)
{
info_node * remove = NULL;
assert(pq);

if (empty(pq)) {
printf("ERROR EMPTY!!!");
} else if (pq->prqtop->next == NULL) {
remove = pq->prqtop;
pq->prqtop = NULL;
} else {
remove = pq->prqtop;
pq->prqtop = pq->prqtop->next;
}
assert(remove);
return remove;
//printf("%s %d\n",remove->code,remove->weight);
}

int oneleft(prqueue * pque)
{
assert(pque);
info_node * node = remove(pque);
if (empty(pque)) {
insert(pque, node);
cout << "one left!" << endl;
return 1;
} else {
insert(pque, node);
return 0;
}
}

void visit(info_node * node)
{
if (node != NULL)
printf("Visit %s\n", node->code);
} // end of function visit()
int qEmpty(lo_queue * pdq)
{
assert(pdq);
if (pdq->front == NULL && pdq->rear == NULL) {
return 1;
} else {
return 0;
}
}
info_node * qRemove(lo_queu e * pdq)
{
info_node * auxinfo = 0;
assert(pdq);
if (qEmpty(pdq)) {
cout << "priority que empty" << endl;
} else if (pdq->front->next == NULL) {
auxinfo = pdq->front;
pdq->front = NULL;
pdq->rear = NULL;
} else {
auxinfo = pdq->front;
pdq->front = pdq->front->next;
}
return auxinfo;
}

void qInsert(lo_queu e * pdq, info_node * node )
{
assert(pdq);
assert(node);
if (qEmpty(pdq)) {
pdq->front = node;
pdq->rear = node;
} else {
pdq->rear->next = node;
pdq->rear = node;
}
}

void levelorder(lo_q ueue * pdq, info_node * root)
{
int testCounter = 0;
if (root == NULL) {
cout << "NULL\n";
return;
}

cout << " ";

// Insert the first node

assert(pdq);
qInsert(pdq, root);

// The level we're at in the tree

int level = 0;

// The number of items we can take out (Binary Tree = 2 * level)

int numAllow = 2 * level;

while (!qEmpty(pdq)) {
cout << "level " << level << endl;

while (!qEmpty(pdq) && numAllow < 2*level) {

info_node * temp = qRemove(pdq);
cout << "(" << temp->code << ") ";

if (temp->left != NULL) {
cout << " L(" << temp->left->code << ") ";
qInsert(pdq, temp->left);
} // end if()
if (temp->right != NULL) {
cout << "R(" << temp->right->code << ") ";
qInsert(pdq, temp->right);
} // end if()
memset(temp, 0, sizeof(info_nod e)); // <<<=== Lookee here!

// We've output one of our allowed elements
numAllow++;
cout << "numallow = " << numAllow << endl;
} // end while()
cout << "\n ";
level++;
}
cout << " ";
}
int main()
{
FILE * infile;
prqueue pque = { 0 };
lo_queue que = { 0 };
//info_node * pleaf_location[7] = { 0 };
int count = 0, level = 0;
int numAllow = 2*level;

create(&pque);
qCreate(&que);

infile = fopen("prq_data .txt", "r");
if (!infile)
perror("prq_dat a.txt");

//fscanf(infile," %s %d ",new_node->code,&new_no de->weight);
while (!feof(infile)) {
info_node * new_node = malloc_node();
int res = fscanf(infile, "%s%d", new_node->code, &new_node->
weight);
if (res != 2)
perror("infile" );

//pleaf_location[count++] = new_node;
insert(&pque, new_node);
}

// remove 2

while (!oneleft(&pque )) {
info_node * p1 = remove(&pque); // remove first node out of pque
info_node * p2 = remove(&pque); // remove second node out of pque
info_node * new_node = malloc_node(); // create new node
strcpy(new_node->code, p1->code); // copy new_node->code, from the first node
strcat(new_node->code, p2->code); // copy new_node->code, from the second node
cout << "newNode Code" << new_node->code << endl;
new_node->weight = p1->weight + p2->weight; // add the weight together
new_node->father = new_node; // initialize the father
cout << "left p1->code = " << p1->code << endl;
cout << "right p2->code = " << p2->code << endl;
new_node->left = p1; // itialize the left
new_node->right = p2; //intialize the right
insert(&pque, new_node); //insert it into the que
}

// the last node will be the root
info_node * tree_root = remove(&pque);
//tree_root = remove(&pque);
//visit(tree_root );
levelorder(&que , tree_root);

printf("End of program: ");
getch();
} // end of function main()
Dec 3 '06 #5
OK, now is later. You still here?
Here is a chopped and reformed version of your code.
Ya I am still here, I really appreciate the effort, first i have been
using void main(void) in order to get the program to run and stop
while debugging, You say its illegal, yet it is accepting it, though i
have come to realize that it accepts alot of things that are even close
to being right, which definetly puts me into a quandry as i am a
student. And anything else you dont like about my code, please tell me
and don't hold back. Well in the mean time, there are alot of stuff in
your code that i dont know so i am going to go over it, thanks again.

Dec 3 '06 #6
memset(temp, 0, sizeof(info_nod e)); // <<<=== Lookee here!
why do you need memset here?
int res = fscanf(infile, "%s%d", new_node->code, &new_node->
weight);
if (res != 2)
perror("infile" );
why would res = 2?

Dec 3 '06 #7
On 3 Dec 2006 09:18:18 -0800 in comp.lang.c++, "GrispernMi x"
<Gr*********@gm ail.comwrote,
> memset(temp, 0, sizeof(info_nod e)); // <<<=== Lookee here!
why do you need memset here?
It is a cheap hack, a bad example, and I shouldn't have done it.
It is just a way of setting the whole thing to zeros, but the real way
is of course assigning all of the members = 0.

If I understand the code, that node temp-should be removed from the
list and no longer used (should be deallocated with free(), but you
never free() anything). But it IS still affecting the program somehow,
and setting all of its pointers to zero is avoiding the crash that was
troubling you. I only hope that with that improvement, you could figure
out where the real bug lies.

Please change that line to:
temp->father = temp->left = temp->right = temp->next = 0;
strcpy(temp->code, "Boo!");
> int res = fscanf(infile, "%s%d", new_node->code, &new_node->
weight);
if (res != 2)
perror("infile" );
why would res = 2?
The result of fscanf() is the number of data items read. If it is not
the number expected, it usually means a problem. So I stuck that error
check in while I had no idea what was going wrong, just to eliminate
that possibility. The lack of error checking while reading input is one
example of "things I did not like." It may seem unnecessary in a
program that is all about other things, but the simplest typo error can
cause fscanf() to fail, your program to get wrong input without you
knowing it, and your sanity to evaporate.

Lastly, the return type from main() should always be int. It is the
only return type that has ever been allowed in standard C or C++.
void main() is warning sign for book authors or teachers who are not
paying attention or who don't care much about correctness.

Dec 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2361
by: Tim Tyler | last post by:
Today's: "Directory Traversal Vulnerability": - http://secunia.com/advisories/10955/ More evidence tht PHP was hacked together rapidly without a great deal of thought being given to security. -- __________ |im |yler http://timtyler.org/ tim@tt1lock.org Remove lock to reply.
1
8310
by: mike | last post by:
best regards: What is the difference between DOM Level 1 and DOM Level 2. Thank you. May god be with you.
2
2405
by: Al Reid | last post by:
I'm using VB 2005. I have a production application where I load a ListView with information from several sources based on user interaction. At some point I need to iterate through the Items collection and print the documents that have been requested. The contents of the ListView are never sorted or manipulated in any way. Occasionally the documents print out of order. Is it possible that: For Each li as ListViewItem in...
6
4066
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
6
20015
by: APEJMAN | last post by:
would you please help me? I wrote 3 separate line of code for printing my binary tree, and now I am trying to print the level-order traversal of the tree, where the nodes at each level of the tree are printed on a separate line. my codes are below,( for printing inorder, preorder and post order) I have no Idea how I can print them in , level-order traversal I think I should use a Queue, but how? do you have any code that can help me? would...
4
3984
by: APEJMAN | last post by:
would you please help me with this question? I know that a binary tree can be recovered from its pre-order traversal. That is, a tree built from the pre-order traversal should always be the same as the original tree. Is it true that the pre-order traversal tells the order in which the values were inserted?
8
4182
by: APEJMAN | last post by:
hI Would you please help me with this question? Using Big O Notation, what is the running time of the level-order traversal ( the code below) in terms of the number of nodes N and the tree height h? Based on your knowledge of the possible values of h, what are the average, best, and worst running times in terms of just N? would you please explain to me? void Tree<T> :: level_order_traversal(std::ostream& out, TreeNode<T>* rootNode) { ...
4
1615
by: star33 | last post by:
Hi, i made a display()function that uses level-order traversal of the tree to display nodes level-by-level.I was told that I needed to put a queue class in my project. the hint was to put the queue in the root, do regular traversal but when we meet the marker, get() the marker from the queue and break the line. Thanks, I need to get the something like this in my output ....40 ..30..50 20.35.45.55 but I am getting 5 errors, don't know why? ...
6
4423
by: gskbond | last post by:
Following is my logic to traverse level by level an mway search tree.... (Yeah I finally used templates to implement a queue... :) ) void mWayTree:: TraverseLevelOrder(Node *root,int lvl) { int i; int level; Dq<Node *> Q; Node *tmp;
0
8823
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.