2009-08-25 15 views
13

किसी को भी जावास्क्रिप्ट में एक साधारण बीटीआई कार्यान्वयन के किसी भी अच्छे उदाहरण के बारे में पता है? मेरे पास "चीजें" यादृच्छिक रूप से पहुंचने का एक समूह है, और प्रत्येक को कुशलता से सम्मिलित करना चाहता हूं।जावास्क्रिप्ट बाइनरी खोज पेड़ कार्यान्वयन

आखिरकार, प्रत्येक नया व्यक्ति डीओएम में डाला जाएगा जहां यह पेड़ में समाप्त होता है।

मैं इसे स्क्रैच से कोड कर सकता हूं लेकिन किसी भी पहियों को फिर से नहीं बदल सकता।

धन्यवाद

उत्तर

8

क्या इससे मदद मिलती है? - Computer science in JavaScript: Binary search tree, Part 1

+0

हाँ, उत्कृष्ट, धन्यवाद के लिए देख सकते हैं। मुझे नहीं पता कि वह Google खोज में क्यों नहीं आया था। – brad

12

अगर यह मायने रखती है, मैंने पाया कि एक शाब्दिक पेड़ के रूप में इस प्रकार का डेटा भंडारण के पहले से हल कर देता है के रूप में यह भंडारण और ब्याह/तत्वों को सम्मिलित करने वालों पर द्विआधारी खोज करने की तुलना में कम कुशल था। जाहिर है, जावास्क्रिप्ट ऑब्जेक्ट सृजन मुक्त नहीं है।

ol भी नहीं है 'एनकोड एक पेड़ में एक सरणी चाल:

[5, 3, 7, 1, null, 6, 9, null, null, null, null, null, null] 

 5 
    /\ 
    3 7 
//\ 
    1 6 9 

यानी बच्चों (एन [i]) = एन रूप में ही है [2i + 1], एन [2i + 2]। मुझे नहीं पता कि यह वास्तव में आपको जावास्क्रिप्ट में कोई जीत देता है या नहीं।

यदि आप बाइनरी पेड़ के कुछ विकल्प आज़माते हैं, तो क्या आप यहां अपने निष्कर्ष पोस्ट कर सकते हैं? :)

+1

क्या आप इस "चाल" की गहराई से स्पष्टीकरण प्रदान कर सकते हैं? अगर सरणी कहती है कि आपके पास [1,3,5,6,7,9] था तो वह सरणी कैसे प्राप्त हुई थी? प्रारंभिक सरणी के विपरीत उस सरणी के माध्यम से खोज करते समय क्या लाभ होता है? – AndrewHenderson

+0

@AndrewHenderson चाल एक प्रदर्शन अनुकूलन है (कम से कम कुछ भाषाओं और रनटाइम्स के लिए) जो मनमानी पॉइंटर्स का पीछा करने के लिए पूर्णांक अंकगणितीय और सरणी dereferences को प्रतिस्थापित करता है। पेड़ की जड़ हमेशा सूचकांक 0 होती है, इसके बच्चे हमेशा सूचकांक 1 और 2 होते हैं, उनके बच्चे हमेशा अनुक्रमांक 3, 4 और 5, 6 क्रमशः अनुक्रमित होते हैं। विकिपीडिया पर एक संक्षिप्त विवरण है: https://en.wikipedia.org/wiki/Binary_tree#Arrays –

2

आप buckets, जावास्क्रिप्ट लाइब्रेरी का प्रयास कर सकते हैं, इसमें आपकी हर चीज है।

7

https://gist.github.com/alexhawkins/f993569424789f3be5db

function BinarySearchTree() { 
    this.root = null; 
} 

BinarySearchTree.prototype.makeNode = function(value) { 
    var node = {}; 
    node.value = value; 
    node.left = null; 
    node.right = null; 
    return node; 
}; 

BinarySearchTree.prototype.add = function(value) { 
    var currentNode = this.makeNode(value); 
    if (!this.root) { 
     this.root = currentNode; 
    } else { 
     this.insert(currentNode); 
    } 
    return this; 
}; 

BinarySearchTree.prototype.insert = function(currentNode) { 
    var value = currentNode.value; 
    var traverse = function(node) { 
     //if value is equal to the value of the node, ignore 
     //and exit function since we don't want duplicates 
     if (value === node.value) { 
      return; 
     } else if (value > node.value) { 
      if (!node.right) { 
       node.right = currentNode; 
       return; 
      } else 
       traverse(node.right); 
     } else if (value < node.value) { 
      if (!node.left) { 
       node.left = currentNode; 
       return; 
      } else 
       traverse(node.left); 
     } 
    }; 
    traverse(this.root); 
}; 


BinarySearchTree.prototype.contains = function(value) { 
    var node = this.root; 
    var traverse = function(node) { 
     if (!node) return false; 
     if (value === node.value) { 
      return true; 
     } else if (value > node.value) { 
      return traverse(node.right); 
     } else if (value < node.value) { 
      return traverse(node.left); 
     } 
    }; 
    return traverse(node); 
}; 


/* BREADTH FIRST TREE TRAVERSAL */ 

/* Breadth First Search finds all the siblings at each level 
    in order from left to right or from right to left. */ 

BinarySearchTree.prototype.breadthFirstLTR = function() { 
    var node = this.root; 
    var queue = [node]; 
    var result = []; 
    while (node = queue.shift()) { 
     result.push(node.value); 
     node.left && queue.push(node.left); 
     node.right && queue.push(node.right); 
    } 
    return result; 
}; 


BinarySearchTree.prototype.breadthFirstRTL = function() { 
    var node = this.root; 
    var queue = [node]; 
    var result = []; 
    while (node = queue.shift()) { 
     result.push(node.value); 
     node.right && queue.push(node.right); 
     node.left && queue.push(node.left); 
    } 
    return result; 
}; 

/*DEPTH FIRST TRAVERSALS*/ 

/* preOrder is a type of depth-first traversal that tries 
    togo deeper in the tree before exploring siblings. It 
    returns the shallowest descendants first. 

    1) Display the data part of root element (or current element) 
    2) Traverse the left subtree by recursively calling the pre-order function. 
    3) Traverse the right subtree by recursively calling the pre-order function. */ 

BinarySearchTree.prototype.preOrder = function() { 
    var result = []; 
    var node = this.root; 
    var traverse = function(node) { 
     result.push(node.value); 
     node.left && traverse(node.left); 
     node.right && traverse(node.right); 
    }; 
    traverse(node); 
    return result; 
}; 

/* inOrder traversal is a type of depth-first traversal 
    that also tries to go deeper in the tree before exploring siblings. 
    however, it returns the deepest descendents first 

    1) Traverse the left subtree by recursively calling the pre-order function. 
    2) Display the data part of root element (or current element) 
    3) Traverse the right subtree by recursively calling the pre-order function. */ 

BinarySearchTree.prototype.inOrder = function() { 
    var result = []; 
    var node = this.root; 
    var traverse = function(node) { 
     node.left && traverse(node.left); 
     result.push(node.value); 
     node.right && traverse(node.right); 
    }; 
    traverse(node); 
    return result; 
}; 

/* postOrder traversal is a type of depth-first traversal 
    that also tries to go deeper in the tree before exploring siblings. 
    however, it returns the deepest descendents first 

    1) Traverse the left subtree by recursively calling the pre-order function. 
    2) Display the data part of root element (or current element) 
    3) Traverse the right subtree by recursively calling the pre-order function. */ 


BinarySearchTree.prototype.postOrder = function() { 
    var result = []; 
    var node = this.root; 
    var traverse = function(node) { 
     node.left && traverse(node.left); 
     node.right && traverse(node.right); 
     result.push(node.value); 
    }; 
    traverse(node); 
    return result; 
}; 

//find the left most node to find the min value of a binary tree; 
BinarySearchTree.prototype.findMin = function() { 
    var node = this.root; 
    var traverse = function(node) { 
     return !node.left ? node.value : traverse(node.left); 
    }; 
    return traverse(node); 
}; 

//find the right most node to find the max value of a binary tree; 
BinarySearchTree.prototype.findMax = function() { 
    var node = this.root; 
    var traverse = function(node) { 
     return !node.right ? node.value : traverse(node.right); 
    }; 
    return traverse(node); 
}; 


BinarySearchTree.prototype.getDepth = function() { 
    var node = this.root; 
    var maxDepth = 0; 
    var traverse = function(node, depth) { 
     if (!node) return null; 
     if (node) { 
      maxDepth = depth > maxDepth ? depth : maxDepth; 
      traverse(node.left, depth + 1); 
      traverse(node.right, depth + 1); 
     } 
    }; 
    traverse(node, 0); 
    return maxDepth; 
}; 


//Can you write me a function that returns all the averages of the nodes 
//at each level (or depth)?? with breadth-first traversal 


BinarySearchTree.prototype.nodeAverages = function() { 
    var node = this.root; 
    var result = {}; 
    var depthAverages = []; 

    var traverse = function(node, depth) { 
     if (!node) return null; 
     if (node) { 
      if (!result[depth]) 
       result[depth] = [node.value]; 
      else 
       result[depth].push(node.value); 
     } 
     //check to see if node is a leaf, depth stays the same if it is 
     //otherwise increment depth for possible right and left nodes 
     if (node.right || node.left) { 
      traverse(node.left, depth + 1); 
      traverse(node.right, depth + 1); 
     } 
    }; 
    traverse(node, 0); 

    //get averages and breadthFirst 
    for (var key in result) { 
     var len = result[key].length; 
     var depthAvg = 0; 
     for (var i = 0; i < len; i++) { 
      depthAvg += result[key][i]; 
     } 
     depthAverages.push(Number((depthAvg/len).toFixed(2))); 
    } 
    return depthAverages; 
}; 

//Convert a binary search tree to a linked-list in place. 
//In-order depth-first traversal. 
function LinkedList() { 
    this.head = null; 
} 

BinarySearchTree.prototype.convertToLinkedList = function() { 

    var result = []; 
    var node = this.root; 
    if (!node) return null; 

    var traverse = function(node) { 
     node.left && traverse(node.left); 
     result.push(node.value); 
     node.right && traverse(node.right); 
    }; 

    traverse(node); 

    var makeNode = function(value) { 
     var node = {}; 
     node.value = value; 
     node.next = null; 
     return node; 
    }; 

    var list = new LinkedList(); 
    list.head = makeNode(result[0]); 
    var current = list.head; 

    for (var i = 1; i < result.length; i++) { 
     var currentNode = makeNode(result[i]); 
     current.next = currentNode; 
     current = current.next; 
    } 
    return list; 
}; 

//TESTS 

var bst = new BinarySearchTree(); 
bst.add(40).add(25).add(78).add(10).add(32); 
console.log('BS1', bst); 

var bst2 = new BinarySearchTree(); 
bst2.add(10).add(20).add(30).add(5).add(8).add(3).add(9); 
console.log('BST2', bst2); 
console.log('BREADTHFIRST LTR', bst2.breadthFirstLTR()); 
console.log('BREADTHFIRST RTL', bst2.breadthFirstRTL()); 
console.log('PREORDER', bst2.preOrder()); 
console.log('INORDER', bst2.inOrder()); 
console.log('POSTORDER', bst2.postOrder()); 

/* 
BREADTHFIRST LTR [ 10, 5, 20, 3, 8, 30, 9 ] 
BREADTHFIRST RTL [ 10, 20, 5, 30, 8, 3, 9 ] 
PREORDER [ 10, 5, 3, 8, 9, 20, 30 ] 
INORDER [ 3, 5, 8, 9, 10, 20, 30 ] 
POSTORDER [ 3, 9, 8, 5, 30, 20, 10 ] 
*/ 

var bst3 = new BinarySearchTree(); 
bst3.add('j').add('f').add('k').add('z').add('a').add('h').add('d'); 
console.log(bst3); 
console.log('BREADTHFIRST LTR', bst3.breadthFirstLTR()); 
console.log('BREADTHFIRST RTL', bst3.breadthFirstRTL()); 
console.log('PREORDER', bst3.preOrder()); 
console.log('INORDER', bst3.inOrder()); 
console.log('POSTORDER', bst3.postOrder()); 

/* 
BREADTHFIRST LTR [ 'j', 'f', 'k', 'a', 'h', 'z', 'd' ] 
BREADTHFIRST RTL [ 'j', 'k', 'f', 'z', 'h', 'a', 'd' ] 
PREORDER [ 'j', 'f', 'a', 'd', 'h', 'k', 'z' ] 
INORDER [ 'a', 'd', 'f', 'h', 'j', 'k', 'z' ] 
POSTORDER [ 'd', 'a', 'h', 'f', 'z', 'k', 'j' ] 
*/ 


console.log(bst2.findMin()); // 3 
console.log(bst2.findMax()); // 30 
console.log(bst2.contains(15)); 
//bst2.add(55); 
//bst2.add(65); 
//bst3.add(75); 
console.log(bst2); 
console.log(bst2.getDepth()); // 3 
console.log(bst2.add(7).add(50).add(80).add(98)); 
console.log(bst2.getDepth()); // 5 
console.log(bst2.nodeAverages()); //[ 10, 12.5, 13.67, 22, 80, 98 ] 

console.log(bst2.convertToLinkedList()); 
//[ 3, 5, 7, 8, 9, 10, 20, 30, 50, 80, 98 ] 
//{ head: { value: 3, next: { value: 5, next: [Object] } } } 
0

द्विआधारी खोज के पेड़ आमतौर पर BST के रूप में जानता है पेड़ जो प्रकृति में हल कर रहे हैं एक विशेष प्रकार का है। बाइनरी सर्च पेड़ में प्रत्येक नोड अपने बाएं बच्चे से बड़ा होता है और उसके दाहिने बच्चे से छोटा होता है। यह सुविधा बाइनरी खोज पेड़ से नोड को खोजने, डालने और हटाने में आसान बनाती है।

एल्गो

// अगर रूट नोड रिक्त है या नहीं

// हाँ तो

// यदि नहीं की तुलना में पुनरावृति रूट करने के लिए नए नोड असाइन करते हैं की जाँच करें। इटरेट विधि वर्तमान में प्रसंस्करण नोड के बाएं और दाएं बच्चे से जोड़ने के लिए नोड मान की जांच करेगी।

// यदि जोड़ने के लिए नोड मूल्य बाईं बच्चे यद्यपि इस कदम से नोड की तुलना में कम है

// तो छोड़ दिया बच्चे की तुलना में इस बाएं चाइल्ड नोड

// किसी और कॉल पुनरावृति करने के लिए नए नोड आवंटित रिक्त है विधि

// यदि जोड़ने के लिए नोड मूल्य सही बच्चे को इस कदम से नोड मान से अधिक

// तो सही बच्चे की तुलना में यह सही बच्चे नोड के लिए नए नोड आवंटित खाली है

// किसी और कॉल पुनरावृति विधि

इस मदद करता है, तो आप पूरा लेख यहाँ Binary Search Tree Insert node Implementation in Javascript

संबंधित मुद्दे