| | |
Tree help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
So in a binary tree the insertion looks something like this:
But how would you insert for an n-ary tree?
C++ Syntax (Toggle Plain Text)
void btree::insert(int num) { if(root!=NULL) { insert(num, root); } else { root=new Node; root->data=num; root->left=NULL; root->right=NULL; } } void btree::insert(int num, Node *leaf) { if(num< leaf->data) { if(leaf->left!=NULL) { insert(num, leaf->left); } else { leaf->left=new Node; leaf->left->data=num; leaf->left->left=NULL; leaf->left->right=NULL; } } else if(num>=leaf->data) { if(leaf->right!=NULL) insert(num, leaf->right); else { leaf->right=new Node; leaf->right->data=num; leaf->right->left=NULL; leaf->right->right=NULL; } } }
But how would you insert for an n-ary tree?
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
So If i had this how would I keep track of the root children?
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<vector> using namespace std; class Tree { public: Tree() { root = NULL; } bool isEmpty() const { return root==NULL; } void insert(int); void remove(int); private: struct node { node *parent; vector <node*> children; int item; }; node* root; }; void Tree::insert(int num) { if(isEmpty()) { root=new node; root->item = num; } else { //? } } int main() { return 0; }
![]() |
Similar Threads
- Left Column Menu Tree (JavaScript / DHTML / AJAX)
- Binary search tree removal (C++)
- outputing a basic tree ? (C)
- Insertion in a binary search tree (C++)
- Help with strings and file directory tree (C)
- coding a complete binary tree with Dev-C++ (C++)
- evaluating expression using tree (C)
- Win Explorer tree flickers (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: URGENT! It is for my assignment. I am so stuck. Please check my c++ work.
- Next Thread: Flie Handling
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






