binary-search-tree

Need direction about Binary Search Tree implementation in Objective-C

被刻印的时光 ゝ 提交于 2020-01-06 06:25:19
问题 I have a partial implementation of a binary tree that doesn't work properly. I believe I am missing fundamental knowledge about struct memory management in objective-c but not sure what it is(besides malloc). When I try to create a new tree node based on a struct I get Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) which led me to believe I didn't create a memory location for this struct pointer. What is the proper way of doing this in Objective-C? (Code in below) Thank you for taking the

Priority Search Tree confusion

前提是你 提交于 2020-01-06 02:49:15
问题 The only reasonable slide set I found is this, which in page 15 says, for building: Sort all points by their x coordinate value and store them in the leaf nodes of a balanced binary tree (i.e., a range tree) Starting at the root, each node contains the point in its subtree with the maximum value for its y coordinate that has not been stored at a shallower depth in the tree; if no such node exists, then node is empty I implemented a Range Tree just before, so based on the example I used there,

Binary Search Tree of Strings (before balancing)

一世执手 提交于 2020-01-05 12:11:47
问题 I'm reading up on Binary Search Trees and I have a question to answer similar to Ordered Binary Tree of Strings Are the trees I've drawn below correct for before and after balancing? The data being entered are the strings, in order of, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus Before balancing: Mercury / \ Earth Venus \ / Mars Saturn / \ Jupiter Uranus After balancing: Mercury / \ Jupiter Uranus / \ / \ Earth Mars Saturn Venus Is it also correct that the depth of the first tree is

count number of binary search tree of height h for all possible roots over n elements

[亡魂溺海] 提交于 2020-01-05 04:02:59
问题 I have n elements like 1,2,3,4....n. I wanted to count the number of possible binary search tree with height H for all possible roots Count number of Binary Search Tree with all possible height: int countTrees(int N) { if (N <=1) { return(1); } else { int sum = 0; int left, right, root; for (root=1; root<=N; root++) { left = countTrees(root - 1); right = countTrees(N - root); // number of possible trees with this root == left*right sum += left*right; } return(sum); } } In the above program, I

Data structure for inverting a subarray in log(n)

主宰稳场 提交于 2020-01-05 03:34:43
问题 Build a Data structure that has functions: set(arr,n) - initialize the structure with array arr of length n . Time O(n) fetch(i) - fetch arr[i] . Time O(log(n)) invert(k,j) - (when 0 <= k <= j <= n ) inverts the sub-array [k,j] . meaning [4,7,2,8,5,4] with invert(2,5) becomes [4,7,4,5,8,2] . Time O(log(n)) How about saving the indices in binary search tree and using a flag saying the index is inverted? But if I do more than 1 invert, it mess it up. 回答1: Here is how we can approach designing

Time complexity of finding k successors in BST

二次信任 提交于 2020-01-03 16:50:49
问题 Given a binary search tree (BST) of height h , it would take O(k+h) time to apply the BST InOrder Successor algorithm k times in succession, starting in any node, applying each next call on the node that was returned by the previous call. Pseudo code: get_kth_successor(node): for times = 1 to k: node = successor(node) return node How can I prove this time complexity? In particular, I am trying to establish a relation between k and the number of nodes visited, but can't find any pattern here.

Fair deletion of nodes in Binary Search Tree

时光毁灭记忆、已成空白 提交于 2020-01-03 16:45:32
问题 The idea of deleting a node in BST is: If the node has no child, delete it and update the parent's pointer to this node as null If the node has one child, replace the node with its children by updating the node's parent's pointer to its child If the node has two children, find the predecessor of the node and replace it with its predecessor, also update the predecessor's parent's pointer by pointing it to its only child (which only can be a left child) the last case can also be done with use

Fair deletion of nodes in Binary Search Tree

人走茶凉 提交于 2020-01-03 16:44:01
问题 The idea of deleting a node in BST is: If the node has no child, delete it and update the parent's pointer to this node as null If the node has one child, replace the node with its children by updating the node's parent's pointer to its child If the node has two children, find the predecessor of the node and replace it with its predecessor, also update the predecessor's parent's pointer by pointing it to its only child (which only can be a left child) the last case can also be done with use

Writing a generic traverse function that allows the flexibility of dealing with multiple functions with differing parameters

淺唱寂寞╮ 提交于 2020-01-03 03:25:06
问题 I want to employ std::function to help me run a generic traverse function that traverses a BST and calls the parametrized function. My difficulty is that the parametrized function varies in its own parameters. So, for example, I was to generalize the following three functions (all different in their parameters). //populates an array with the values in the BST by traversing the BST void LinkedBST<T>::populate(T const * const data, size_t & I, Node *x) { data[i++] = x->val; } //insert unique

How to return nothing from a function that returning value?

大憨熊 提交于 2020-01-02 08:23:41
问题 I have a binary search tree, and I want to delete a node. I need to get its parent, so I wrote a function: private BSTreeNode<T> getParent(BSTreeNode<T> root, BSTreeNode<T> node) { if(root == null) return null; if(node.element().lessThan(root.element())) { if(root.getLeft() != null && root.getLeft().element().equal(node.element())) return root; else getParent(root.getLeft(), node); } else { if(root.getRight() != null && root.getRight().element().equal(node.element())) return root; else