Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › recursive binary tree c++
New Posts  All Forums:Forum Nav:

recursive binary tree c++

post #1 of 4
Thread Starter 
Hey guys, doing a pretty simple recursive binary tree, takes in numbers and i have it doing a print (inorder) function.
what my problem is is the program compiles runs, but the print doesnt seem to be working.

could anybody possibly help me find my issue?

thanks!
Code:

#include <iostream>
using namespace std;

struct node* tree = NULL;

struct node { 
    int data; 
    struct node* left; 
    struct node* right; 
};

struct node* newNode(int data) { 
  struct node* tree = new(struct node); 
  tree->data = data; 
  tree->left = NULL; 
  tree->right = NULL;

  return(tree); 
}
  
struct node* insert(struct node* tree, int data) { 
   
  if (tree == NULL) { 
    return(newNode(data)); 
  } 
  else {  
    if (data <= tree->data) 
        tree->left = insert(tree->left, data); 
    else 
        tree->right = insert(tree->right, data);
    return(tree);  
  } 
} 
 

void printTree(struct node* tree) { 
 
    if (tree == NULL) return;
        
  printTree(tree->left); 
  cout << tree->data; 
  printTree(tree->right); 
} 

int main() {
    
    int i;

    do{
        cout << "enter a number into the tree plz.. ";
        cin >> i;
        insert(tree, i);
    }
    while( i != -0);
    
    printTree(tree);

    return 0;
}



thanks again smile.gif
post #2 of 4
you never save the first number into the tree, so it's always null and nothing ever gets added. i made 1 change, and it worked fine for me.
Code:
do{
        cout << "enter a number into the tree plz.. ";
        cin >> i;
        tree = insert(tree, i);
    }
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #3 of 4
Quote:
Originally Posted by travesty View Post

you never save the first number into the tree, so it's always null and nothing ever gets added. i made 1 change, and it worked fine for me.
Code:
do{
        cout << "enter a number into the tree plz.. ";
        cin >> i;
        tree = insert(tree, i);
    }

^ This.
I learned to use a reference/pointer to the tree and modify the pointers in-place - that avoids all the assignments but it's a matter of taste:
void insert(struct node* &tree, int data)
WS
(18 items)
 
  
CPUMotherboardGraphicsRAM
FX-8150 Asus Crosshair V ECS GT430 Kingston KVR1333D3E9S/4GEC 
Hard DriveHard DriveOptical DriveCooling
Barracuda XT ST33000651AS Corsair Force GT Samsung TS-H653J  Noctua NH-D14 
CoolingCoolingCoolingCooling
Delta PFC1212DE-PWM Delta AFB1212HHE-PWM Delta AFC1212DE-PWM San ACE 9SG1212G101 
OSMonitorKeyboardPower
Debian Wheezy with 3.2 kernel and XFCE4 2x Acer S273HL Unicomp Customizer 104/105 Seasonic X750 Gold 
CaseMouse
Fractal Design Define XL Logitech 
  hide details  
Reply
WS
(18 items)
 
  
CPUMotherboardGraphicsRAM
FX-8150 Asus Crosshair V ECS GT430 Kingston KVR1333D3E9S/4GEC 
Hard DriveHard DriveOptical DriveCooling
Barracuda XT ST33000651AS Corsair Force GT Samsung TS-H653J  Noctua NH-D14 
CoolingCoolingCoolingCooling
Delta PFC1212DE-PWM Delta AFB1212HHE-PWM Delta AFC1212DE-PWM San ACE 9SG1212G101 
OSMonitorKeyboardPower
Debian Wheezy with 3.2 kernel and XFCE4 2x Acer S273HL Unicomp Customizer 104/105 Seasonic X750 Gold 
CaseMouse
Fractal Design Define XL Logitech 
  hide details  
Reply
post #4 of 4
Thread Starter 
omg how could i be so silly smile.gif thank you so much guys, really!
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › recursive binary tree c++