binary-search-tree

Sorted list to complete BST array representation

ε祈祈猫儿з 提交于 2019-12-10 06:03:53
问题 I'm wondering whether there is a mapping between a sorted array (e.g., [1, 2, 3, 4, 5, 6]) and the representation that one obtains when one constructs a complete binary search tree from this sorted array, and expresses said binary search tree as an array (e.g., [4, 2, 6, 1, 3, 5], see graphic below)? 4 2 6 1 3 5 Here's some more context: It is well known that one can take a sorted array and construct a complete binary search tree from it (there is a unique representation). A recursive

Balancing a binary search tree

故事扮演 提交于 2019-12-10 04:08:17
问题 Ok, I am trying to get a binary search tree to balance, and I know why it's not working, but I don't know how to fix it. This is what I have for my balancing methods. public void balance(){ if(isEmpty()){ System.out.println("Empty Tree"); return; } if(!isEmpty()){ values = new Object[count()]; index = 0; createAscendingArray(root); clear(); balanceRecursive(0, index); values = null; } } private void createAscendingArray(TreeNode<E> current){ if(current == null) return; if(current.getLeftNode(

Best Data Structure/Algorithm for insert/delete/rank/select queries

本秂侑毒 提交于 2019-12-09 07:04:48
问题 So far, I know that self-balancing BST like AVL tree and Red Black Tree can do these operations in O(log n) times. However, to use these structures, we must implement AVL tree or RB tree ourselves. I have heard that there is an algorithm/ implementation of these four operations without using self-balancing BST. With our own defined structure, we need to write so many lines. However, I have heard that there is a possibility of supporting these four operators in less than 100 lines code :\ Do

The correct way to build a Binary Search Tree in OCaml

女生的网名这么多〃 提交于 2019-12-08 21:05:36
Ok, I have written a binary search tree in OCaml. type 'a bstree = |Node of 'a * 'a bstree * 'a bstree |Leaf let rec insert x = function |Leaf -> Node (x, Leaf, Leaf) |Node (y, left, right) as node -> if x < y then Node (y, insert x left, right) else if x > y then Node (y, left, insert x right) else node The above code was said to be good in The right way to use a data structure in OCaml However, I found a problem. This insert will only work when building a bst from a list in one go, such as let rec set_of_list = function [] > empty | x :: l > insert x (set_of_list l);; So if we build a bst

Perfect Balanced Binary Search Tree

最后都变了- 提交于 2019-12-08 19:19:11
问题 I have an theoretical question about Balanced BST . I would like to build Perfect Balanced Tree that has 2^k - 1 nodes, from a regular unbalanced BST . The easiest solution I can think of is to use a sorted Array/Linked list and recursively divide the array to sub-arrays, and build Perfect Balanced BST from it. However, in a case of extremely large Tree sizes, I will need to create an Array/List in the same size so this method will consume a large amount of memory. Another option is to use

Printing to a file in C

こ雲淡風輕ζ 提交于 2019-12-08 19:16:21
问题 How do I print to an empty .txt file I already have created? I already print the results to the console, and now I want to print to a file named "Output.txt" . I've tried a couple of things that haven't worked, but I think it was easier to create a duplicate printDictionary() specifically for printing to a file called printDictionaryToFile() . I'm a little lost on how to do it though. Can anyone correct me on where I went wrong? I already added an extra FILE type called *out for my output to

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

妖精的绣舞 提交于 2019-12-08 16:48:00
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 values of another BST into "this" BST: traverses the other BST and inserts every value void LinkedBST<T>:

Find whether a tree is a binary search tree in Haskell

扶醉桌前 提交于 2019-12-08 16:19:36
问题 type BSTree a = BinaryTree a data BinaryTree a = Null | Node (BinaryTree a) a (BinaryTree a) deriving Show flattenTree :: BinaryTree a -> [a] flattenTree tree = case tree of Null -> [] Node left val right -> (flattenTree left) ++ [val] ++ (flattenTree right) isBSTree :: (Ord a) => BinaryTree a -> Bool isBSTree btree = case btree of Null -> False tree -> (flattenTree tree) == sort (flattenTree tree) What I want to do is to write a function to determine whether the given tree is a binary search

Why isn't std::set just called std::binary_tree? [closed]

拜拜、爱过 提交于 2019-12-08 14:23:55
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 8 months ago . std::set in C++ is not a real set in terms of data structures. std::unordered_set is a real set, but std::set is a binary search tree , more specifically a red-black tree. Why, then, is it called std::set? Is there some specific functionality that sets a std::set apart from a binary tree? Thanks.

questions about binary search tree

南楼画角 提交于 2019-12-08 13:52:27
问题 Show that every n-node binary search tree is not equally likely (assuming items are inserted in random order), and that balanced trees are more probable than straight-line trees. How is it prove mathematical? 回答1: Number of possible tree configurations : see With ' N ' no of nodes, how many different Binary and Binary Search Trees possible? Number of ways to get a single line, most imbalanced, deepest tree with n nodes : 2^(n-1) Explanation: 2 ways to pick up first node (greatest or smallest)