Hey everyone, I need a little help with my binary tree program. The purpose of this program is just an exercise for myself, but for some reason it doesn't work. It's supposed to input words "One" through "Ten" into the binary tree(left side is alphabetically less than the top node, right is more) and I believe I've done that(but probably not). The problem is that with the read() function, it only displays three words, when they were supposed to be ten. Any help on what im doing wrong would be really really really appreciated. You would earn my respect of the highest level ^.^
Here is my current code: -
#include <iostream>
-
#include <string>
-
-
using namespace std;
-
-
struct Tnode{
-
string word;
-
int counter;
-
Tnode *right;
-
Tnode *left;
-
};
-
-
void insert( Tnode *leaf, char *words[] );
-
void read( Tnode *leaf );
-
-
int main(){
-
char *words[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};
-
Tnode *leaf;
-
leaf = new Tnode;
-
leaf->word = words[0];
-
insert( leaf, words );
-
read( leaf );
-
system( "pause" );
-
return 0;
-
}
-
-
void insert( Tnode *leaf, char *words[] ){
-
-
int i;
-
for(i=1;i<=9;i++){
-
//cout << "In for" << endl;
-
if(leaf->word.compare(words[i]) > 0){
-
//cout << "start Left" << endl;
-
-
/*if(leaf != NULL){
-
cout << "Left null" << endl;
-
insert( leaf->left, words );
-
}else{*/
-
-
cout << "putting " << words[i] << " into left node" << endl;
-
leaf->left = new Tnode;
-
leaf->left->word = words[i];
-
leaf->left->right = NULL;
-
leaf->left->left = NULL;
-
-
}else if(leaf->word.compare(words[i]) <= 0 ){
-
//cout << "start Right" << endl;
-
-
/*if(leaf != NULL){
-
cout << "Right null" << endl;
-
insert( leaf->right, words );
-
}else{*/
-
-
cout << "putting " << words[i] << " into right node" << endl;
-
leaf->right = new Tnode;
-
leaf->right->word = words[i];
-
leaf->right->right = NULL;
-
leaf->right->left = NULL;
-
-
}
-
}
-
}
-
-
void read( Tnode *leaf ){
-
if(leaf->left!=NULL)
-
read(leaf->left);
-
cout << leaf->word << "->";
-
if(leaf->right!=NULL)
-
read(leaf->right);
-
-
}
-
Thanks,
Austen
4 3095
Wow, looking at my code now, I can safely say I'm an idiot. Im not propelling through the tree at all. When I run into a problem, i'll post, but for now, just disregard the post above.
Sorry,
Austen
Sorry for triple posting, but I felt no need to start a new thread.
So now I have the same objective and everything, but a more sophisticated approach with some possible potential ;). I have no idea what to do right now though. I'm completely stuck. I know my problem is when I compare my word in the character array to the (nonexistent) word in the tree. I get an error during the program run because I'm trying to access data that doesn't exist. So here's what I want to do: - Find the alphabetical relevance of the word in the character array to the parent node's word.
- Insert the character array word appropriately
- Continue with the list
You'll notice that the last bullet was kinda vague. That's because I don't know how to go about doing it. I know this is probably a pretty easy concept, but I just can't grasp it.
Simply put: How do I go about continuing in my tree without trying to access nonexistent memory?
Heres my new code if relevant: -
#include <iostream>
-
#include <string>
-
-
using namespace std;
-
-
struct Tnode{
-
string word;
-
int counter;
-
Tnode *right;
-
Tnode *left;
-
};
-
-
void insert( Tnode *leaf, char *words[], int count );
-
void read( Tnode *leaf );
-
-
int main(){
-
char *words[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};
-
Tnode *leaf;
-
insert( leaf, words, 1 );
-
read( leaf );
-
system( "pause" );
-
return 0;
-
}
-
-
void insert( Tnode *leaf, char *words[], int count ){
-
-
cout<<"In function"<<endl;
-
-
if(leaf->word.compare(words[count]) > 0)
-
{
-
cout << "startLeft" << endl;
-
if(leaf->left!=NULL)
-
insert(leaf->left, words, count);
-
else
-
{
-
leaf->left=new Tnode;
-
leaf->left->word=words[count];
-
leaf->left->left=NULL;
-
leaf->left->right=NULL;
-
count++;
-
}
-
}
-
else if(leaf->word.compare(words[count]) <= 0)
-
{
-
cout << "startRight" << endl;
-
if(leaf->right!=NULL)
-
insert(leaf->right, words, count);
-
else
-
{
-
leaf->right=new Tnode;
-
leaf->right->word=words[count];
-
leaf->right->left=NULL;
-
leaf->right->right=NULL;
-
count++;
-
}
-
}
-
-
}
-
-
void read( Tnode *leaf ){
-
if(leaf->left!=NULL)
-
read(leaf->left);
-
cout << leaf->word << "->";
-
if(leaf->right!=NULL)
-
read(leaf->right);
-
-
}
-
-
ok I got it working with this code: -
#include <iostream>
-
#include <string>
-
-
using namespace std;
-
-
struct Tnode{
-
string word;
-
int counter;
-
Tnode *right;
-
Tnode *left;
-
};
-
-
void insert( Tnode *leaf, char *words[], int count );
-
void read( Tnode *leaf );
-
-
int main(){
-
char *words[] = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};//word array
-
Tnode *leaf;//binary tree
-
int counter = 0;//keeps track of current word
-
-
leaf = new Tnode;//sets up first few node for insertion
-
leaf->word = words[0];
-
leaf->right = NULL;
-
leaf->left = NULL;
-
-
for(counter=1;counter<10;counter++){//counts once for each word
-
insert( leaf, words, counter );
-
}
-
-
read( leaf );
-
system( "pause" );
-
return 0;
-
}
-
-
void insert( Tnode *leaf, char *words[], int count ){
-
-
//cout<<"In function"<<endl; //debugging feature
-
-
if(leaf->word.compare(words[count]) > 0)//compare alphabetic relevance between the two words
-
{
-
//cout << "startLeft" << endl; //debugging feature
-
if(leaf->left!=NULL){
-
//cout << "Left not NULL" << endl; //debugging feature
-
insert(leaf->left, words, count);
-
}else if(leaf->left == NULL)
-
{
-
//cout << "Left NULL" << endl; //debugging feature
-
leaf->left=new Tnode;
-
leaf->left->word=words[count];
-
leaf->left->left=NULL;
-
leaf->left->right=NULL;
-
-
}
-
}
-
else if(leaf->word.compare(words[count]) <= 0)//compare alphabetic relevance between the two words
-
{
-
//cout << "startRight" << endl; //debugging feature
-
if(leaf->right!=NULL){
-
//cout << "Right not NULL(" << leaf->right->word << ")" << endl; //debugging feature
-
insert(leaf->right, words, count);
-
}else if(leaf->right == NULL)
-
{
-
//cout << "Right NULL" << endl; //debugging feature
-
leaf->right=new Tnode;
-
leaf->right->word=words[count];
-
leaf->right->left=NULL;
-
leaf->right->right=NULL;
-
-
}
-
}
-
-
}
-
-
void read( Tnode *leaf ){
-
if(leaf->left!=NULL)
-
read(leaf->left);
-
cout << leaf->word << " ";
-
if(leaf->right!=NULL)
-
read(leaf->right);
-
return;
-
}
-
Wow, really sorry that you had to go through 4 posts with no help, but I'm glad you got it working on your own. Binary Trees confused me, too, when I first started out.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jerry Khoo |
last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am
> cs students and are now facing difficult problems in understanding
> what a binary tree is, how it works, and the algorithm...
|
by: Jerry Khoo |
last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am
cs students and are now facing difficult problems in understanding
what a binary tree is, how it works, and the algorithm to...
|
by: jova |
last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so
can someone please explain.
Thank You.
|
by: Tarique Jawed |
last post by:
Alright I needed some help regarding a removal of a binary search
tree. Yes its for a class, and yes I have tried working on it on my
own, so no patronizing please. I have most of the code working,...
|
by: pembed2003 |
last post by:
Hi,
I have a question about how to walk a binary tree. Suppose that I have
this binary tree:
8
/ \
5 16
/ \ / \
3 7 9 22
/ \ / \ / \
|
by: Foodbank |
last post by:
Hi all,
I'm trying to do a binary search and collect some stats from a text
file in order to compare the processing times of this program (binary
searching) versus an old program using linked...
|
by: Ken |
last post by:
I have a binary tree in VB NET and insertions seem to be slow.
The program receives data from one source and inserts it into the tree.
The program receives data from another source and...
|
by: piotrek |
last post by:
Hi
I would like to ask you a question.
Ian creating app. that download from server directory structure ( whole
tree ) and those data are placed in proper places into my treeview control.
I...
|
by: hn.ft.pris |
last post by:
I have the following code:
Tree.h defines a simple binary search tree node structure
########## FILE Tree.h ################
#ifndef TREE_H
#define TREE_H
//using namespace std;
template...
|
by: Vinodh |
last post by:
Started reading about Binary Trees and got the following questions in
mind. Please help.
Definition of a Binary Tree from "Data Structures using C and C++ by
Tanenbaum" goes like this,
"A...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |