mardi 5 mai 2015

Node Deletion in Objective-C Binary Search Tree - Pointers?

I see a lot of examples of node deletion with C/C++ for binary search trees. I'm trying to write this in objective C and I'm unsure if I want to use a single or double pointer approach or neither? Any links or advice would be great. Here's my code to create/insert node to the tree if that is helpful

-(Node*) Insert:(Node*)root withData:(int) data {
    //Empty root
    if(root == NULL) {
        root = [self GetNewNode:data];
    }
    // If data is less insert in left tree
    else if(data <= root.data) {
        root.left = [self Insert:root.left withData:data];
    }
    // If data is more insert in right tree
    else {
        root.right = [self Insert:root.right withData:data];
    }
    return root;
}

and

Node* root = NULL;  // Creating an empty node on the tree
Tree* tree = [[Tree alloc]init];

 // Setup the tree
root = [tree Insert:root withData:15];
root = [tree Insert:root withData:20];
root = [tree Insert:root withData:25];

Aucun commentaire:

Enregistrer un commentaire