PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Problem with inserting Nodes at Binary trees
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problem with inserting Nodes at Binary trees

Réponse
 
LinkBack Outils de la discussion
Vieux 24/12/2007, 20h02   #1
Blondeamon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problem with inserting Nodes at Binary trees

Ok i'll try to be brief. As part of one of my essays i thought of
creating source code for implementation of a binary tree and its basic
functions.

My code is both from books,lecture notes and googling but for some
reason on the printing of the nodes using inOrder traversal only 2 or 3
nodes are being printed.

I will put my code here although i am sure that only few might actually
comprehend what it is about because this subject needs good knowledge of
all the tricks about inserting/deleting nodes from theory of Binary Trees.


Exercise: Create code for a binary tree.The program reads the size of
the tree N and then inserts N nodes with values between 1 and 30.000
into the tree.
You cant enter a node if it is not already on the tree,so you must
search if it exists before entering it.

Finally ,after having inserted all N nodes print them using InOrder
traversal.


Tips: My code works for the first 2 nodes...but doesnt print anything
else.And i will bust my head open if i wont figure out why.

I appreciate any guys , this is a tough question and this is my
first time on this forum.
(seems very warm so far btw)








#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

//the struct which represents the node
struct tnode {
struct tnode *left, *right;
int key;
};


void InOrder( tnode *x )
{
if (x == 0) return;
InOrder(x->left);
cout << x->key << " ";
InOrder(x->right);
}


int main ()
{
srand((long)2004126);
tnode *root = 0;
int size, key2;
int flag;

cout << "\nPlease enter the size of the tree. N: ";
cin >> size;

for (int i = 0; i < size; i++) {
key2 = 1 + rand() % 30000; //creating random node key
between 1-30.000
tnode *p = root;
tnode *q = 0 ;
flag = 0;

//searching existing tree for node with that value

while((p != 0)&&(flag==0)) {
q = p;
if (p->key == key2) {
flag = 1; //node with that key already
exists -cant enter
} else if (p->key < key2) {
p = p->left; //case 1: key smaller than root
} else {
p = p->right; //case 2: key bigger than root
}
}


//creating new node with key2 as content
if (flag == 0) { //flag is 0 when that value didnt
already exist on the tree
tnode *z = new tnode;
z->key = key2;
z->left = 0;
z->right = 0; //creation of nw node that will
take key2 as its value
//and will have 2 NULL kids (left and right)

if (q == 0) {
root = z;
//case of empty tree
//case handling about where the new node will be put
} else if (key2 > q->key) {
q->right = z;
} else {
q->left = z;
}
}
}

//cout << "\nroot is : "<< root->key; //not needed
,just for checking

//the value of root
InOrder(root);
cout << endl;
cin >> size;
return 0;
}
  Réponse avec citation
Vieux 24/12/2007, 20h52   #2
Nikos Chantziaras
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with inserting Nodes at Binary trees

Blondeamon wrote:
> My code is both from books,lecture notes and googling


It's a bad idea to copy&paste code; write your own


> I will put my code here although i am sure that only few might actually
> comprehend what it is about because this subject needs good knowledge of
> all the tricks about inserting/deleting nodes from theory of Binary Trees.


Well, just because you have problems with it doesn't mean you need a
Nobel prize in IT to grasp it It looks like a straightforward and
trivial tree to me with no "tricks" or optimizations involved.


> Tips: My code works for the first 2 nodes...but doesnt print anything
> else.And i will bust my head open if i wont figure out why.
>
> I appreciate any guys , this is a tough question and this is my
> first time on this forum.
> (seems very warm so far btw)
>
> #include <iostream>
> #include <cstdlib>
> #include <ctime>
>
> using namespace std;
>
> //the struct which represents the node
> struct tnode {
> struct tnode *left, *right;
> int key;
> };
>
>
> void InOrder( tnode *x )
> {
> if (x == 0) return;
> InOrder(x->left);
> cout << x->key << " ";
> InOrder(x->right);
> }
>
>
> int main ()
> {
> srand((long)2004126);
> tnode *root = 0;
> int size, key2;
> int flag;
>
> cout << "\nPlease enter the size of the tree. N: ";
> cin >> size;
>
> for (int i = 0; i < size; i++) {
> key2 = 1 + rand() % 30000; //creating random node key
> between 1-30.000
> tnode *p = root;
> tnode *q = 0 ;
> flag = 0;
>
> //searching existing tree for node with that value
>
> while((p != 0)&&(flag==0)) {
> q = p;
> if (p->key == key2) {
> flag = 1; //node with that key already
> exists -cant enter
> } else if (p->key < key2) {
> p = p->left; //case 1: key smaller than root


Wrong. It's *bigger*, not smaller. That is you must either use:

p->key > key2

or:

key2 < p->key

but *not* what you're currently doing:

p->key < key2
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h18.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09344 seconds with 10 queries