ternary tree giving error - c++

This is simple ternary tree structure . I have written code correctly but while running it says after some time:
Sorry ternary.exe has stopped working.
Can you tell me the cause of this error.
#include<iostream>
#include<string>
using namespace std;
struct tnode{
int data[2];
tnode *ptr[3];
};
void swap(int *a,int *b){
int t;
t=*a;
*a=*b;
*b=t;
}
//for initializing tnode variables as null or null character
void newtree(tnode *&T){
T->data[0]='\0';
T->data[1]='\0';
T->ptr[0]=NULL;
T->ptr[1]=NULL;
T->ptr[2]=NULL;
}
void fillto(tnode *&T,int a){
if(T->data[0]=='\0'){
T->data[0]=a;
}
else if(T->data[0]!='\0'&&T->data[1]=='\0'){
T->data[1]=a;
if(T->data[0]>T->data[1])
swap(T->data[0],T->data[1]);
}
else{
if(a<T->data[0]){
if(T->ptr[0]==NULL){
T->ptr[0]=new(tnode);
newtree(T->ptr[0]);
}
fillto(T->ptr[0],a);
}
else if(a>T->data[1]){
if(T->ptr[2]==NULL){
T->ptr[2]=new(tnode);
newtree(T->ptr[2]);
}
fillto(T->ptr[2],a);
}
else{
if(T->ptr[1]==NULL){
newtree(T->ptr[1]);
T->ptr[1]=new(tnode);
}
fillto(T->ptr[1],a);
}
}
}
tnode *datatnode(string s){
int l=0;
tnode *T;
tnode *E;
T=new(tnode);
char c[0];
newtree(T);
E=T;
while(l<=s.length()){
c[0]=s[l];
cout<<atoi(c)<<endl;
fillto(T,atoi(c));
l++;
}
return E;
}
int main(){
string s="5398124";
tnode *T;
T=new(tnode);
T=datatnode(s);
cout<<T->data[0];
return 0;
}

You should remove '=' sign as below
tnode *datatnode(string s){
int l=0;
tnode *T;
tnode *E;
T=new(tnode);
char c;
newtree(T);
E=T;
int a = s.length();
while(l<a){
c=s[l];
cout<<atoi(&c)<<endl;
fillto(T,atoi(&c));
l++;
}
return E;
}

Its difficult to say from your code (as mentally you have to run it in your head). Better to debug it out. Call some debug at key points in your code and try to locate the exact line of code.... this could produce a lot of debug depending how big your data-set is.
At a guess I would say that you probably hit a bad address or somthing like this, that is usually why programs die un-expectedly and immediatly! So I would suggest being very secure on your pointer checking. For example:
void fillto(tnode *&T,int a){
if (T != NULL){
if(T->data[0]=='\0')
{
T->data[0]=a;
}
:
:
}
else
{
printf("Warning: NULL pointer!\n");
}
}
Basically any time you use a pointer that is passed in to a function you should check it is not null. This is generally good code practise and may help you to find your bugs :)
Also int initialisation can just be:
int i = 0;
instead of
int i = '\0';

The fundamental flaw that causes the error is in the 'void fillto(tnode *&T,int a)' function:
...
if(T->ptr[1]==NULL){
newtree(T->ptr[1]);
...
}
As the function newtree does not check if the pointer is null, you end up dereferencing a NULL pointer in newtree

Related

Updating a pointer value with an integer during a recursive call in postorder traversal of a BST

int MovieTree::countMovieNodes()
{
int count = 0;
int* c = &count;
countMovieNodes(root,c);
return *c;
}
void MovieTree::countMovieNodes(MovieNode *node, int *c)
{
int count;
if(node == NULL)
{
return;
}
else
{
count ++;
countMovieNodes(node->leftChild, c);
countMovieNodes(node->rightChild, c);
}
}
My code is returning 0, so clearly I am misunderstanding the methodology to updating the pointer values. How do I fix this? I don't think my logic for post order traversal of the BST is the issue.
If you want to keep your current format, creating a new count is still making of copy of it, just incerment the pointer directly:
int MovieTree::countMovieNodes()
{
int count = 0;
int* c = &count;
countMovieNodes(root,c);
return *c;
}
void MovieTree::countMovieNodes(MovieNode *node, int *c)
{
if(node == NULL)
{
return;
}
else
{
++*c;
countMovieNodes(node->leftChild, c);
countMovieNodes(node->rightChild, c);
}
}
Your code doesn't actually use the c parameter (it just passes it on to recursive calls, which also don't use c). Your code also creates a local count variable in each call, which is only incremented (not written to or read from).
What you should do instead is
delete int count;
change count ++; to (*c)++; (or *c += 1; if you don't like parens) to increment the count variable in the top-level countMovieNodes() call)

Segmentation Fault 11 whenever I run this. Would like assistance/feedback

So I'm making a really rudimentary implementation of a circular list. I haven't made the remove function yet. Whenever I run the cpp, I get a seg fault 11. Any feedback would be much appreciated. Thank you.
#include <iostream>
using namespace std;
struct node{
node* next=NULL;
bool tail= false;
int contents;
};
node* start;//start is a pointer that exists at the start of the list before the first element
class CircList{
node *seek;
public:
CircList (){ //creates a list of one node that points to itself
node *b= new node;
b->contents=0;
b->next = b;
start->next=b;
b->tail=true;
}
bool empty(){
if(start->next==NULL){
return true;
}
return false;
}
int size(CircList a){
if(start->next==NULL){
cout<<"size is 0 \n";
return true;
}
seek=start->next;
for(int i=0; i++;){
if(seek->tail==true){
cout<<"size is "<<i;
}
seek=seek->next;
}
return 0;
}
void insert(int pos, int val){
if(start->next ==NULL){//if inseting when the list is empty
node *b= new node;
b->next = b;
b->tail=true;
return;
}
node *b= new node;
b->contents= val;
seek=start->next;
for(int i=0;i<=pos; i++){
if(seek->tail==true){//if inserting at the end
seek->tail=false;
b->tail=true;
seek->next=b;
b->next=start->next;
}
if(pos==i){//if inserting between two nodes
b->next = seek->next;
seek->next = b;
}
seek=seek->next;
}
}
void remove(int a){
seek=start->next;
for(int i=0;i<=a-1; i++){
if(i<a){
seek=seek->next;
}
if(i==a-1){
}
}
}
void display(){
cout<<start->next->contents; //will also be completed in the near future
seek=start->next;
for(int i=0; ;i++){
if(seek->tail==false){
cout<<seek->contents<<"\n";
}
if(seek->tail==true){
cout<<seek->contents<<"\n";
return;
}
}
}
};
That was the .h file. The following is the cpp. I just plugged in numbers to test. I want to get the program running so that I can test how it behaves.
#include <iostream>
#include "CircList.h"
using namespace std;
int main(){
CircList a;
a.insert (5,5);
a.insert (5,5);
a.insert (1,4);
a.insert (20,65);
a.insert (3,7);
a.size(a);
a.display();
}
I kept treating start as a node instead of a pointer. By making start = Null and replacing all the "start->next"'s with "start", I got it to compile and run. But now it's only infinitely inserting nodes with a value of 0 in the contents.
Edit: I fixed it. By changing that weird for loop in the display function to a while loop, it doesn't do infinite inserts of the node in the constructor, anymore. It seems to work decently enough now.
This here causes a seg fault
start->next=b;
because start is NULL at the start of the program so you are de-referencing a null pointer.
instead set start to the first node in your constructor
start = b;
Your global variable start is an uninitialized pointer, yet you dereference it all over the place.

binary search tree c++ searching operation always giving 0;

in this BST,the searchbst function is searching perfectly but this function is returning 0 always. It is not giving 5 or 8 as i have programmed it to do so what is the error in code because of which this problem is there
#include<iostream>
using namespace std;
struct bstnode{
bstnode *lchild;
int data;
bstnode *rchild;
};
void creatbst(bstnode *&T,int k){
if(T=='\0'){
T=new(bstnode);
T->data=k;
T->lchild='\0';
T->rchild='\0';
}
else if(k<T->data){
creatbst(T->lchild,k);
}
else if(k>T->data){
creatbst(T->rchild,k);
}
}
int searchbst(bstnode *T,int k){
if(T=='\0')
return 5;
else{
if(k<T->data)
searchbst(T->lchild,k);
else if(k>T->data)
searchbst(T->rchild,k);
else
return 8;
}
}
int main(){
bstnode *T;
T='\0';
creatbst(T,36);
creatbst(T,20);
creatbst(T,75);
creatbst(T,42);
creatbst(T,8);
creatbst(T,31);
creatbst(T,25);
creatbst(T,3);
creatbst(T,80);
cout<<endl<<"searching for ";
cout<<searchbst(T,3);
cout<<endl<<"searching for ";
cout<<searchbst(T,1);
return 0;
}
You're not using the return value of the recursive calls.
Replace:
if(k<T->data)
searchbst(T->lchild,k);
else if(k>T->data)
searchbst(T->rchild,k);
else
return 8;
with:
if(k < T->data)
return searchbst(T->lchild, k);
else if(k > T->data)
return searchbst(T->rchild, k);
else
return 8;
Your code most likely has undefined behavior: If you don't follow the conditions leading to the return 5; or the return 8; statements, you call searchbst(), ignore its result, and eventually you fall of the function. You probably meant to return the result of searchbst():
return searchbst(T->rchild, k);
BTW, you have a funny way of writing the null pointer constant: although '\0' works, the conventional way is to use 0 or nullptr (the latter is the C++11 way).

Trie Implementation in C++

I am trying to implement the trie as shown on the TopCoder page. I am modifying it a bit to store the phone numbers of the users. I am getting segmentation fault. Can some one please point out the error.
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int words;
int prefix;
long phone;
struct node* children[26];
};
struct node* initialize(struct node* root) {
root = new (struct node);
for(int i=0;i<26;i++){
root->children[i] = NULL;
}
root->word = 0;
root->prefix = 0;
return root;
}
int getIndex(char l) {
if(l>='A' && l<='Z'){
return l-'A';
}else if(l>='a' && l<='z'){
return l-'a';
}
}
void add(struct node* root, char * name, int data) {
if(*(name)== '\0') {
root->words = root->words+1;
root->phone = data;
} else {
root->prefix = root->prefix + 1;
char ch = *name;
int index = getIndex(ch);
if(root->children[ch]==NULL) {
struct node* temp = NULL;
root->children[ch] = initialize(temp);
}
add(root->children[ch],name++, data);
}
}
int main(){
struct node* root = NULL;
root = initialize(root);
add(root,(char *)"test",1111111111);
add(root,(char *)"teser",2222222222);
cout<<root->prefix<<endl;
return 0;
}
Added a new function after making suggested changes:
void getPhone(struct node* root, char* name){
while(*(name) != '\0' || root!=NULL) {
char ch = *name;
int index = getIndex(ch);
root = root->children[ch];
++name;
}
if(*(name) == '\0'){
cout<<root->phone<<endl;
}
}
Change this:
add(root->children[ch], name++, data);
// ---------------------^^^^^^
To this:
add(root->children[ch], ++name, data);
// ---------------------^^^^^^
The remainder of the issues in this code I leave to you, but that is the cause of your run up call-stack.
EDIT OP ask for further analysis, and while I normally don't do so, this was a fairly simple application on which to expand.
This is done in several places:
int index = getIndex(ch);
root = root->children[ch];
... etc. continue using ch instead of index
It begs the question: "Why did we just ask for an index that we promptly ignore and use the char anyway?" This is done in add() and getPhone(). You should use index after computing it for all peeks inside children[] arrays.
Also, the initialize() function needs to be either revamped or outright thrown out in favor of a constructor-based solution, where that code truly belongs. Finally, if this trie is supposed to be tracking usage counts of words generated and prefixes each level is participating in, I'm not clear why you need both words and prefix counters, but in either case to update the counters your recursive decent in add() should bump them up on the back-recurse.

Segmentation Fault in Loop's Condition

The following code is to sort a linked list after creating it. The sorting algorithm used is somewhat similar to Bubble Sort. I am checking the two consecutive nodes and swapping them if necessary. I used the debugger which told me that the fault is raised while condition checking for the loops which are used while sorting.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
using namespace std;
struct link_list
{
char value[20];
struct link_list *next;
};
int main()
{
struct link_list *head=NULL;
int i,j;
char input[20];
char ch;
struct link_list *loop_var,*temp2,*prev_node,*temp4=NULL;
temp3=NULL;
do
{
cout<<"\nEnter the string you want to insert";
cin>>input;
cout<<"\nDo you want to continue entering?";
cin>>ch;
if (head==NULL)
{
head=new link_list;
strcpy(head->value,input);
head->next=NULL;
continue;
}
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next);
temp2=new link_list;
loop_var->next=temp2;
strcpy(temp2->value,input);
temp2->next=NULL;
}while(ch=='y' || ch=='Y');
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
{
cout<<loop_var->value<<"\n";
}
cout<<loop_var->value<<"\n";
char arr[20];
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
{
cout<<"\nLoop1";
for (temp4=head;temp4->next!=NULL;temp4=temp4->next)
{
cout<<"\nLoop2";
temp2=temp4;
if (strcmp(temp2->value,temp2->next->value)>0)
{
cout<<"\nSwap Enter";
if (temp2==head && temp2->next->next==NULL)
{
cout<<"\nSpecial1";
temp2->next->next=temp;
temp2->next=NULL;
}
else if (temp2==head)
{
cout<<"\nSpecial2";
head=temp2->next;
temp2->next=head->next;
head->next=temp2;
}
else if (temp2->next->next==NULL)
{
cout<<"\nSpecial3";
prev_node->next=temp2->next;
prev_node->next->next=temp2;
temp2->next=NULL;
}
else
{
cout<<"\nNormal1";
prev_node->next=temp2->next;
temp2->next=prev_node->next->next;
prev_node->next->next=temp2;
cout<<"\nNormal2";
}
}
prev_node=temp4;
cout<<"\nLoop2PreExit";
fflush(stdin);
cout<<"\nLoop2Exit";
}
cout<<"\nLoop1Exit";
}
for (loop_var=head;loop_var->next!=NULL;loop_var=loop_var->next)
{
cout<<loop_var->value<<"\n";
}
cout<<loop_var->value;
getch();
}
temp2->next->next=temp;
"temp" is not defined anywhere... if your compiler filled in that hole for you, then this is what is causing your loop's condition to segfault.
Also, naming every other variable "temp#" is an easy way to have mistakes like this.