binary-search-tree

Binary search tree C# delete node function

情到浓时终转凉″ 提交于 2019-12-12 04:26:11
问题 My delete function doesn't work when I try to delete node with only right child. It works when node has left child only or has both left and right. I would like to know if this is a valid approach to this problem. I know how to write this in C++ but I need it to work in C# also. //Private method, with args: root node, node to be deleted private Node DeleteN(Node root, Node deleteNode) { if (root == null) { return root; } if (deleteNode.data < root.data) { root.left = DeleteN(root.left,

Is there a way to convert from a general tree to binary SEARCH tree?

霸气de小男生 提交于 2019-12-12 03:58:02
问题 I know how to convert from a general tree to a binary tree just fine, a a / | \ / b c d -> b \ c \ d I was just asked how to convert from a general tree to a binary search tree though. My thoughts are that the person who asked me either didn't mean binary search tree (I asked him, he said he did), or he's misunderstanding something from his class notes. In any case, has anyone heard of doing this? General tree to binary search tree? The answer I gave him was first convert to a binary tree

converting sorted linked list to balanced binaryTree not returning correctly?

馋奶兔 提交于 2019-12-12 03:45:42
问题 Here are key methods I wrote for converting linkedList to Balanced BinarySearch Tree . I get BST but it is not balanced. why is it so? public static Node headNode; public static IntTreeNode convertLinkedListToBST(Node node){ int len = getCount(node); headNode = node; return convertLinkedListToBSThelper(node, 0, len-1); } //http://www.programcreek.com/2013/01/leetcode-convert-sorted-list-to-binary-search-tree-java/ public static IntTreeNode convertLinkedListToBSThelper(Node node, int start,

Optimal binary search trees for successor lookup?

断了今生、忘了曾经 提交于 2019-12-12 03:10:08
问题 There are many algorithms for finding optimal binary search trees given a set of keys and the associated probabilities of those keys being chosen. The binary search tree produced this way will have the lowest expected times to look up those elements. However, this binary search tree might not be optimal with regards to other measures. For example, if you attempt to look up a key that is not contained in the tree, the lookup time might be very large, as the tree might be imbalanced in order to

Having trouble understanding tree traversal recursive functions

十年热恋 提交于 2019-12-12 01:23:34
问题 I am having some trouble understanding the recursive functions involved in preorder, inorder, and postorder tree traversal. I have some knowledge of recursion (but admittedly its not my strong suit). All of the seem to call themselves twice first making a call with the left child of the root and then with the right child. But how exactly is this possible? Wouldn't the call to the preOrder function with the left child return the flow of control back to the top, and the next call would never be

BST code is not working with large number

左心房为你撑大大i 提交于 2019-12-12 00:28:31
问题 I have a very frustrated situation with my BST code: vector<int> order; BinarySearchTree tree; for (int i=0; i<1000; ++i) { int j = rand()%1000; order.push_back(j); } for (int i = 0; i < 1000; ++i) { tree.insert(order[i]); } while(!tree.isEmpty()) { cout << tree.min() << endl; tree.remove(tree.min()); } the code is working perfectly fine with small number of i, say 10, or 100. However, it is stop working when i is 1000. the insert function is as follows void BinarySearchTree::insert(int d) {

List All Function still trying to retrieve a node I deleted from a binary search tree

心不动则不痛 提交于 2019-12-11 22:07:57
问题 I have these functions to remove a node from my binary search tree: bool collection::removeFromTree(const char name[]) { for (treeNode * curr = root; curr;) { int8_t result = strcmp(name, curr->item->getName()); if (result == 0) { deleteNode(curr); return true; } else if (result < 0) curr = curr->left; else if (result > 0) curr = curr->right; } return false; } void collection::deleteNode(treeNode *& goneNode) { //if it's a leaf if (!goneNode->left && !goneNode->right) { delete goneNode; /

removing a node from a binary search tree using recursion

泪湿孤枕 提交于 2019-12-11 20:55:57
问题 I am working with the following code: import random from time import time class BinaryNode: def __init__(self, value = None): """Create binary node""" self.value = value self.left = None self.right = None def add(self, val): """Adds a new node to the tree containing this value""" if val <= self.value: if self.left: self.left.add(val) else: self.left = BinaryNode(val) else: if self.right: self.right.add(val) else: self.right = BinaryNode(val) def delete(self): """ Remove value of self from

Reading data back into a binary search tree

百般思念 提交于 2019-12-11 19:46:37
问题 I have a program that adds a node containing (int studentNumber, String firstName, String lastName, String major, double gpa) and to "save" it to a file, I have this method: public void saveRecord (Node focusNode) { JOptionPane.showMessageDialog(null,"saving file..."); try { ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file)); if (focusNode != null){ System.out.println(focusNode); output.writeObject((focusNode)); preOrderTraverseTree(focusNode.leftChild);

Max depth of Binary search tree

Deadly 提交于 2019-12-11 19:04:45
问题 I want to find the max depth of binary search tree. I found a code. int maxDepth(struct node* node) { if (node==NULL) { return(0); } else { // compute the depth of each subtree int lDepth = maxDepth(node->left); int rDepth = maxDepth(node->right); // use the larger one if (lDepth > rDepth) return(lDepth+1); else return(rDepth+1); } } I want to know that how come node->left would return 1 ? Is it by default ? The code is easy but I do not know from where is the answer coming, can anyone