Sponsored Content
Full Discussion: Bus error in tree insertion
Top Forums Programming Bus error in tree insertion Post 302494413 by vineetjoshi on Monday 7th of February 2011 08:21:12 AM
Old 02-07-2011
Bus error in tree insertion

Hi, I am new to C++ and unix. I am trying to write a programm for inserting elements into a binary tree. To get the code flow I used few Couts and m facing buss error while insertion.
Below is the code snippet. explainations needed. thanks Smilie
Code:
#include <iostream.h>
struct mytree
{
  int element;
  mytree * left;
  mytree * right;
}*root;
class tree
{
  public:
  int size(mytree * new_t);
  mytree* insert(int a, mytree * new_t );
};
int tree:: size(mytree * new_tr)
{
  if(new_tr==NULL)
  {
    return (0);
  }
  else
  {
    return ((size(new_tr -> left)) + (size(new_tr -> right)) + 1);
  }
}

///////////////////////////////////////////
mytree* tree::insert(int a, mytree * new_t )
{
  if(new_t == NULL)
  {
    new_t->element = a;
    new_t->right = NULL;
    new_t -> left =NULL;
    cout << "very first insert";
  }
  else
  {
    if (a <= new_t -> element)
    {
      if (new_t -> left != NULL)
      {
        cout <<" first small insert";
        insert(a, new_t->left);
      }
      else
      {
        cout << "first final small insert";
        mytree * node = new mytree;
        new_t -> left = node;
        new_t -> left -> element = a;
        new_t -> left -> left = NULL;
        new_t -> left -> right = NULL;
      }
    }
    if (a > new_t -> element)
    {
      if (new_t -> right != NULL)
      {
        cout << "first greator insert";
        insert(a, new_t -> right);
      }
      else
      {
        mytree * node = new mytree;
        new_t -> right = node;
        new_t -> right -> element = a;
        new_t -> right -> left = NULL;
        new_t -> right -> right = NULL;
        cout << "first greator final insert";
      }
    }
  }
}
int main()
{
  tree * a = new tree;
  int no_add,this_element, sizet;
  char choice;
  this_element=0;
  mytree * tree1 = new mytree;

  cout <<"enter no of elements to be added ";
  cin >> no_add;
  for(int i=0; i < no_add; i++)
  {
    cout << "what to add at element "<< i<< " ? "<<endl ;
    cin >> this_element;
    tree1 = a->insert(this_element, tree1);
  }
  cout << "need to know size ? y or n "<< endl;
  cin >> choice;
  if (choice == 'y')
  {
    sizet = a-> size(tree1);
    cout << " size of tree is " << endl ;
    cout << sizet;
  }
  else
  {
    cout << " program ends here";
  }
  return 0;
}

Is a no return from insert causing problem ? If yes why does it compile

Last edited by Scott; 02-07-2011 at 11:47 AM.. Reason: Added code tags, indentation
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bus Error

This may belong in the C Programming forum, but here goes anyway... What would cause a bus error? I searched google for a cause, but came up with some conflicting reports... Could it be caused by disk space? A lot of the pages I found mentioned linking with the incorrect versions of the... (4 Replies)
Discussion started by: LivinFree
4 Replies

2. UNIX for Dummies Questions & Answers

bus error on solaris

Hi there I am running soalris 9 on a sun fire 480r and all of a sudden (today) whenever the users run the command `top` we get the following message `bus error` does anybody have any information on what this is all about and whether there is a routine i can perform to gather more... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

3. UNIX for Dummies Questions & Answers

Bus error(coredump

aix 5.3 ML1 system was functioing well, when suddenly telnet session hung and then I got the following message "Bus error(coredump)" would appreciate if anyone would assist as to what to do next. (3 Replies)
Discussion started by: Student37
3 Replies

4. UNIX for Advanced & Expert Users

Bus Error(coredump)

Hi, I am using HP-UX. While i try to run the Sqlplus command using the shel script in encounter the following error: <shell script name>: some number Bus Error(coredump) What may be the reason behind this. I read few previous threads in this forum where i found some similar case. There it was... (2 Replies)
Discussion started by: Jayesh
2 Replies

5. Programming

BUS error

Hi! I've got a program which runs fine under Linux, but I have compiled it to run under SunOS 5.8 in a Sparc computer, and now it sometimes fails with "bus error". Ussing gdb I surfed to the error line, which is *pointer = some_vector; where some_vector is a 16 byte struct (4 integers)... (1 Reply)
Discussion started by: shesatmine
1 Replies

6. HP-UX

Need help on Bus error(coredump)

Hi all, I am quite weak in C but I need to get some work done. Hope someone can help me out. I keep getting this error when i try to run my C application in my HP-UX 10.20 machine. Some code snippet: Month(DBTime) =====This is a function which will return variable "CutOffTime" to be use... (5 Replies)
Discussion started by: Vision©
5 Replies

7. HP-UX

Bus Error

I am getting bus error when i include "#!/bin/ksh". If i remove interpreter then script is working. Can anyone explain this and how can i avoid this error? Operating System is HP-UX B.11.23 U 9000/800 1091834454 (2 Replies)
Discussion started by: anbu23
2 Replies

8. Programming

Bus error

Hi everyone, I have a GUI project and when I run it and left in idle state for a long time(there is nothing done, just opened GUI, no more actions),I get bus error after trying to do anything with it. I've tried to build it in debug mode and use gdb, but I don't get any error in debug mode.It... (3 Replies)
Discussion started by: sisi
3 Replies

9. Programming

Bus Error: 10...Help please!

Hi all, I am writing a phonebook program to store names and number using a list. Here is the code for the function which allows the user to enter the name and number (where the error occurs). //THIS FUNCTION ADDS A NEW ENTRY TO THE phonebook_list void insert(void){ //variables int... (5 Replies)
Discussion started by: kdejan
5 Replies

10. Programming

Insertion Sort Error in C++

TD P { margin-bottom: 0in; }P { margin-bottom: 0.08in; }TT.cjk { font-family: "WenQuanYi Zen Hei Sharp",monospace; }A:link { } I am trying to run this program for Insertion Sort, But don't know that why I am getting following Error #include<iostream> int main(){ int i,j,key,n;... (4 Replies)
Discussion started by: liveproject101
4 Replies
tsearch(3C)															       tsearch(3C)

NAME
tsearch(), tfind(), tdelete(), twalk() - manage binary search trees SYNOPSIS
DESCRIPTION
and are routines for manipulating binary search trees. They are generalized from Knuth (6.2.2) Algorithms T and D. All comparisons are done with a user-supplied routine, compar. This routine is called with two arguments, the pointers to the elements being compared. It returns an integer less than, equal to, or greater than 0, according to whether the first argument is to be considered less than, equal to or greater than the second argument. The comparison function need not compare every byte, so arbitrary data may be contained in the ele- ments in addition to the values being compared. is used to build and access the tree. key is a pointer to an entry to be accessed or stored. If there is an entry in the tree equal to the value pointed to by key, a pointer to the previous key associated with this found entry is returned. Otherwise, key is inserted, and a pointer to it returned. Note that since the value returned is a pointer to key and key itself is a pointer, the value returned is a pointer to a pointer. Only pointers are copied, so the calling routine must store the data. rootp points to a variable that points to the root of the tree. A NULL value for the variable pointed to by rootp denotes an empty tree; in this case, the variable is set to point to the entry which will be at the root of the new tree. Like searches for an entry in the tree, returning a pointer to it if found. However, if it is not found, returns a NULL pointer. The arguments for are the same as for deletes a node from a binary search tree. Arguments are the same as for The variable pointed to by rootp is changed if the deleted node was the root of the tree. returns a pointer to the parent of the deleted node, or a NULL pointer if the node is not found. traverses a binary search tree. root is the root of the tree to be traversed. (Any node in a tree may be used as the root for a walk below that node.) action is the name of a routine to be invoked at each node. This routine is, in turn, called with three arguments: o First argument is the address of the node being visited. o Second argument is a value from an enumeration data type (defined in the header file), depending on whether this is the first, second or third time that the node has been visited (during a depth-first, left-to-right traversal of the tree), or whether the node is a leaf. o Third argument is the level of the node in the tree, with the root being level zero. EXAMPLES
The following code reads strings, and stores structures containing a pointer to each string and a count of its length. It then walks the tree, printing out the stored strings and their lengths in alphabetical order. RETURN VALUE
A NULL pointer is returned by if there is not enough space available to create a new node. A NULL pointer is returned by and if rootp is NULL on entry. If the datum is found, both and return a pointer to it. If not, returns NULL, and returns a pointer to the inserted item. WARNINGS
The root argument to is one level of indirection less than the rootp arguments to and Two nomenclatures are used to refer to the order in which tree nodes are visited. uses preorder, postorder and endorder to respectively refer to visiting a node before any of its children, after its left child and before its right and after both its children. The alternate nomenclature uses preorder, inorder, and postorder to refer to the same visits, which could result in some confusion over the meaning of postorder. If the calling function alters the pointer to the root, results are unpredictable. SEE ALSO
bsearch(3C), hsearch(3C), lsearch(3C), thread_safety(5). STANDARDS CONFORMANCE
tsearch(3C)
All times are GMT -4. The time now is 01:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy