Non-recursive add function in a binary tree using c++ -
Non-recursive add function in a binary tree using c++ -
i writing add together function add together nodes binary tree non recursively. have run problem of beingness able produce 1 level deep binary tree. debugged , know problem can't figure out how prepare it. maybe fresh pair of eyes see dont... problem temp node beingness reset root value every new function phone call , hence, adding nodes linearly. anyways, here function:
void binarytreenonrec::add(int num){ treenode *temp, *parent; if (root==null) { root = new treenode; root->data=num; root->left=0; root->right=0; temp=root; printf("value root \n"); } else { temp=root; while (temp!=null) { if (num <= temp->data){ parent = temp; temp=temp->left; //root =temp; printf("value left \n"); } else { parent =temp; temp=temp->right; //root=temp; printf("value right \n"); } } parent = new treenode; parent->data=num; parent->left=null; parent->right=null; temp=parent; } } thanks kind of help.
you not adding new node tree, running downwards tree , filling parent new node, never adding tree, alter below code:
parent = new treenode; parent->data=num; parent->left=null; parent->right=null; temp=parent; to:
treenode *newnode = new treenode; newnode->data = num; newnode->left = null; newnode->right = null; //now insert new node below parent if(num <= parent->data) parent->left = newnode; else parent->right = newnode; c++ binary-tree non-recursive
Comments
Post a Comment