473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Need help (inorder-preorder-postorder)

mnf7
3
My program doesn't work properly. Please help me



Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. using namespace std;
  7.  
  8. class node{
  9.     public:
  10.         node *child_left;
  11.         node *child_right;
  12.         char value [11];
  13. };
  14. node *reshe = NULL;    
  15. //#######################################################
  16. void insert (node *c)
  17. {
  18.     node *a;
  19.     node *b;
  20.     b = NULL;
  21.     a = reshe;
  22.  
  23.     while ( a!=NULL)
  24. {
  25.     b = a;
  26.     if(strcmp(c->value,a->value) < 0)
  27. {
  28.     a= a-> child_left; 
  29. }
  30.     else
  31. {
  32.     a= a-> child_right; 
  33. }
  34. }
  35.     if( b==NULL )
  36. {
  37.     reshe=c;
  38. }
  39.     else if(strcmp( c->value,b->value) < 0)
  40. {
  41.     b -> child_left = c;
  42. }
  43.     else
  44. {
  45.     b-> child_right = c;
  46. }
  47. }
  48. //#######################################################
  49. void preorder(node *r){
  50.     if( r!=NULL)
  51.     {
  52.         cout<<"   "<< r->value;
  53.         preorder(r->child_left);
  54.         preorder(r->child_right);
  55.     } 
  56. }
  57. //#######################################################
  58. void inorder(node* r){
  59.     if( r!=NULL)
  60.     {
  61.         inorder(r->child_left);
  62.         cout << r->value << " " ;
  63.         inorder(r->child_right);
  64.     }
  65. }
  66. //########################################################
  67. void postorder(node* r){
  68.     if( r!=NULL)
  69.     {
  70.         inorder(r->child_left);
  71.         inorder(r->child_right);
  72.         cout << r->value << " " ;
  73.  
  74.     }
  75. }
  76. //#######################################################
  77. void main()
  78. {
  79.     int choice=0 ;
  80.     while (choice!=3){
  81.     system("cls");
  82.     cout<<"\n\n\t 1. Insert a Expression.";
  83.     cout<<"\n\n\t 2. peymayesh.";
  84.     cout<<"\n\n\t 3. Exit.";
  85.     cout<<"\n\n\t Enter your choice : ";
  86.     cin>>choice;
  87.  
  88.     switch (choice)
  89. {
  90.     case 1:{ system("cls");
  91.             node *c=new node;
  92.             cout<<"\n\n\t Enter your Expression Trees: ";
  93.             cin >> c->value;
  94.             c->child_right=NULL; 
  95.             c->child_left=NULL; 
  96.             insert(c); 
  97.            }
  98.             break;
  99.     case 2:{ system("cls");
  100.             cout << "\n\n\t  peymayesh Preorder : ";
  101.             preorder(reshe);
  102.             cout << "\n\n\t  peymayesh Inorder : ";
  103.             inorder(reshe);
  104.             cout << "\n\n\t  peymayesh Postorder : ";
  105.             postorder(reshe);
  106.             getchar();
  107.             getchar();
  108.            }
  109.             break;
  110.     case 3: break;
  111. }
  112. }
  113. }
Jun 24 '14 #1
1 1048
weaknessforcats
9,208 Expert Mod 8TB
There are too many bugs in the code to address here. I suggest you step through the code using your debugger.

The obvious bugs are:
1) in the insert function the argument is a node* c. However, c is not used in the search while loop. What is used is a node* a that has never been initialized.

2)In the inorder, preorder, and postorder functions a recursive call is made if the argument node* is not NULL. However, if it is NULL, the functions just quit without doing anything.
Jun 24 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Sofia | last post by:
My name is Sofia and I have for many years been running a personals site, together with my partner, on a non-profit basis. The site is currently not running due to us emigrating, but during its...
6
by: Robert Maas, see http://tinyurl.com/uh3t | last post by:
System login message says PHP is available, so I tried this: http://www.rawbw.com/~rem/HelloPlus/h.php It doesn't work at all. Browser just shows the source. What am I doing wrong?
0
by: Gregory Nans | last post by:
hello, i need some help to 'tree-ify' a string... for example i have strings such as : s = """A(here 's , B(A ) silly test) C(to show D(what kind) of stuff i need))""" and i need to...
7
by: Mike Kamermans | last post by:
I hope someone can help me, because what I'm going through at the moment trying to edit XML documents is enough to make me want to never edit XML again. I'm looking for an XML editor that has a...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
15
by: drdoubt | last post by:
using namespace std In my C++ program, even after applying , I need to use the std namespace with the scope resolution operator, like, std::cout, std::vector. This I found a little bit...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
3
by: Bob.Henkel | last post by:
I write this to tell you why we won't use postgresql even though we wish we could at a large company. Don't get me wrong I love postgresql in many ways and for many reasons , but fact is fact. If...
11
by: Alan Mailer | last post by:
A project I'm working on is going to use VB6 as a front end. The back end is going to be pre-existing MS Access 2002 database tables which already have records in them *but do not have any...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.