Javascript Data Structure Tree Binary Tree Clone
var BinaryTree = function (val) { this.val = val;/*from w w w. j a va2s . com*/ this.left = null; this.right = null; }; BinaryTree.prototype.insert = function (val) { if (val === this.val) { return; } else if (val <= this.val) { if (this.left === null) { this.left = new BinaryTree(val); } else { this.left.insert(val); } } else { if (this.right === null) { this.right = new BinaryTree(val); } else { this.right.insert(val); } } }; BinaryTree.prototype.clone = function () { var clonedTree = new BinaryTree(this.val); if (this.left) { clonedTree.left = this.left.clone(); } if (this.right) { clonedTree.right = this.right.clone(); } return clonedTree; }; var test1 = new BinaryTree(15) test1.insert(7); test1.insert(22); test1.insert(12); test1.insert(5); test1.insert(50); test1.insert(17); var cloned = test1.clone(); console.log('cloned:', cloned, '\noriginal:', test1);