| | |
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 char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






