Class for AVL trees (C++) -
so wanted make avl tree in c++. used class make nodes , class rest of fuctions (instert, delete etc). when want make more 1 tree decided needed make more roots (one every tree). problem when declare new root inside main. when declare outside seems work fine.
avlnode:
class avl_node { public: int data; avl_node *left; avl_node *right; }*root; avltree::insert implementation:
avl_node* avltree::insert(avl_node* root, int item) { if (root == null) { root = new avl_node; root->data = item; root->left = null; root->right = null; return root; } if (item < root->data) { root->left = insert(root->left, item); } else if (item > root->data) { root->right = insert(root->right, item); } else if (item == root->data) { cout << "this exists" << endl; } root = balance(root); return root; } main function:
int main() { avl_node* roott; int item; avltree avl; avltree kk; root = avl.insert(root, item); roott = kk.insert(roott, item); kk.display(roott, 1); return 0; }
// in main root = avl.insert(root, item); // in avltree::insert avl_node* avltree::insert(avl_node* root, int item) { if (root == null) { // <- here at here, using ::root not initialized. @ point, ::root points random address, not want.
write
}*root = null; and
avl_node* roott = null; to initialize pointers.
need initialize int item in main, presumably 0.
int item = 0;
Comments
Post a Comment