简体   繁体   中英

One algorithm for binary tree search, traversal, insertion, and deletion

I see Binary Tree implementations like this :

var insert = function(value, root) {
  if (!root) {
    // Create a new root.
    root = { val: value };
  }
  else {
    var current = root;
    while (current) {
      if (value < current.val) {
        if (!current.left) {
          // Insert left child.
          current.left = { val: value };
          break;
        }
        else {
          current = current.left;
        }
      }
      else if (value > current.val) {
        if (!current.right) {
          // Insert right child.
          current.right = { val: value };
          break;
        }
        else {
          current = current.right;
        }
      }
      else {
        // This value already exists. Ignore it.
        break;
      }
    }
  }

  return root;
}

var exists = function(value, root) {
  var result = false;

  var current = root;
  while (current) {
    if (value < current.val) {
      current = current.left;
    }
    else if (value > current.val) {
      current = current.right;
    }
    else {
      result = true;
      break;
    }
  }

  return result;
}

var traversePre = function(head, callback) {
  // Preorder traversal.
  if (head) {
    if (callback) {
      callback(head.val);
    }

    traversePre(head.left, callback);
    traversePre(head.right, callback);
  }
}

var traversePost = function(head, callback) {
  // Postorder traversal.
  if (head) {
    traversePost(head.left, callback);
    traversePost(head.right, callback);

    if (callback) {
      callback(head.val);
    }
  }
}

var traverseIn = function(head, callback) {
  // Inorder traversal.
  if (head) {
    traverseIn(head.left, callback);

    if (callback) {
      callback(head.val);
    }

    traverseIn(head.right, callback);
  }  
}

var traverseInIterative = function(head, callback) {
  // Inorder traversal (iterative style).
  var current = head;
  var history = [];

  // Move down to the left-most smallest value, saving all nodes on the way.
  while (current) {
    history.push(current);
    current = current.left;
  }

  current = history.pop();
  while (current) {
    if (callback) {
      callback(current.val);
    }

    // Move to the right, and then go down to the left-most smallest value again.
    current = current.right;
    while (current) {
      history.push(current);
      current = current.left;
    }

    current = history.pop();
  }
}

var root = insert(10);
insert(5, root);
insert(6, root);
insert(3, root);
insert(20, root);

Particularly, traverseInIterative looks pretty good to me. But I'm wondering if there's really a need to have insert and exists , and likewise to have search or delete . I get that (like in these implementations) that they are implemented differently, but wondering if you could implement a generic matching function that solves all of it in one swoop that would be at the same time as ideal as it gets, performance-wise.

One way to design a generic method to do all the operations would be -

genericMethod(value, root, command)

Here command parameter would receive a string specifying insert or delete or search . And based on the command parameter you can tweak the inner implementation to support all of the operations.

Now let's come to the matter of performance and design perspective. I don't think having a method like this would be ideal. Having a generic method like this would cause you more problem than you can think.

Now after reviewing your code - there are a number of things that can be improved which will give you a better experience in my opinion like -

  • For insertion/deletion - you need to check if the value already exists or not. So just call exists() method rather writing same codes in those method.

This type of generic behavior ensures that you are not writing same codes again and again also SRP(Single Responsibility Principle), so your code is perfectly compartmentalized and more easily readable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM