| | |
Binary Tree
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 10
Reputation:
Solved Threads: 1
i want to implement a binary tree code but i encountered a problem. after i insert some numbers to tree, i couldn't print my numbers except first one or i couldn't insert the numbers except first one. As a result my binary tree doesn't work, it only shows the first(root) value. where is the problem? inserting or printing numbers?
here is my header file
Thank you...
here is my header file
cpp Syntax (Toggle Plain Text)
struct node{ int index; node *rnode; node *lnode; }; class MyTree { public: MyTree(void); void traverseInOrder(); bool isEmpty()const; bool search(int item); bool insert(int newItem); private: bool searchByNode(int item, node *ptr); void inOrder(node *ptr); node *root; }; //here is implementation #include "MyTree.h" #include <iostream> using namespace std; MyTree::MyTree(void) { root=0; } bool MyTree::isEmpty()const{ if(root==0) return true; else return false; } void MyTree::traverseInOrder(){ inOrder(root); } void MyTree::inOrder(node *ptr){ if(ptr!=0) { inOrder(ptr->lnode); cout<<ptr->index<<endl; inOrder(ptr->rnode); } } bool MyTree::insert(int newItem){ node *newNode= new node(); newNode->index=newItem; newNode->lnode=0; newNode->rnode=0; node *current, *trailCurrent; if(isEmpty()){ root=newNode; return true; } else{ current=root; while(current!=0) { trailCurrent=current; if(current->index==newItem) cout<<"there is already such item"<<endl; else{ if( newItem > current->index ) current=current->rnode; else current=current->lnode; } } if(trailCurrent->index < newItem) newNode=trailCurrent->rnode; else newNode=trailCurrent->lnode; return true; } return false; } bool MyTree::search(int item){ return searchByNode(item,root); } bool MyTree::searchByNode(int item, node *ptr){ if(item==ptr->index) return true; else{ if(item>ptr->index) searchByNode(item,ptr->rnode); else searchByNode(item,ptr->lnode); } return false; } //main MyTree test; test.insert(9); test.insert(13); test.insert(10); cout<<"traverse inorder:"<<endl; test.traverseInOrder();
Thank you...
Last edited by WolfPack; Jan 25th, 2008 at 9:12 am. Reason: To get Indentation and syntax highlighting use [CODE=CPP][/CODE] Tags
•
•
Join Date: Nov 2006
Posts: 47
Reputation:
Solved Threads: 0
cpp Syntax (Toggle Plain Text)
class MyTree { private://initialize things on top so that it will be easy for the compiler to initialize them // dear we dont make functions in private,they all r in public bool searchByNode(int item, node *ptr); void inOrder(node *ptr); node *root; public: MyTree(void);// if it is a default constructor den no argument void traverseInOrder(); bool isEmpty()const; bool search(int item); // it will only be int not int item because we dont pass any variable in da prototype bool insert(int newItem); //same here only int in paranthesis likewise (int) };
ur code will look like:::
cpp Syntax (Toggle Plain Text)
class MyTree { private: node *root; public: MyTree(); void traverseInOrder(); bool isEmpty()const; bool search(int ); bool insert(int ); bool searchByNode(int , node *ptr); void inOrder(node *ptr); };
Last edited by WolfPack; Jan 25th, 2008 at 9:17 am. Reason: use [CODE=CPP][/CODE] Tags when you post code
![]() |
Similar Threads
- complete binary tree using an array help (C++)
- binary tree traversal .. (C++)
- Binary Tree Traversal (C)
- Question about binary tree & heaps (Computer Science)
- binary tree class (C++)
- C++ complete binary tree using an array. Unexpected end file (C++)
- coding a complete binary tree with Dev-C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: Help needed
- Next Thread: Help need in Knight's Tour
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





