binary-search-tree

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

淺唱寂寞╮ 提交于 2019-12-03 09:37:29
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 you guys have any ideas on how this should be done? Other than BST, is there any other possible options?

Flatten binary search to in order singly linked list [C]

怎甘沉沦 提交于 2019-12-03 08:32:31
I am trying to flatten a binary search tree to a singly linked list. Binary search tree: 6 / \ 4 8 / \ \ 1 5 11 / 10 Flattened Singly Linked List: 1 -> 4 -> 5 -> 6 -> 8 -> 10 -> 11 I can't seem to figure this out for some reason. I have a struct for the tree nodes: typedef stuct node { int key; struct node *left; struct node *right; } Node; I have a function to create and allocate memory to the tree node: Node* newNode (int key) { Node *new = malloc (sizeof(Node)); new->left = NULL; new->right = NULL; new->key = key; return new; } I have a struct for the list nodes: typedef struct list { int

How many permutations of a given array result in BST's of height 2?

▼魔方 西西 提交于 2019-12-03 07:43:53
A BST is generated (by successive insertion of nodes) from each permutation of keys from the set {1,2,3,4,5,6,7}. How many permutations determine trees of height two? I been stuck on this simple question for quite some time. Any hints anyone. By the way the answer is 80. zw324 Consider how the tree would be height 2? -It needs to have 4 as root, 2 as the left child, 6 right child, etc. How come 4 is the root? -It needs to be the first inserted. So we have one number now, 6 still can move around in the permutation. And? -After the first insert there are still 6 places left, 3 for the left and 3

Find number of permutations of a given sequence of integers which yield the same binary search tree

那年仲夏 提交于 2019-12-03 06:26:56
Given an array of integers arr = [5, 6, 1] . When we construct a BST with this input in the same order, we will have "5" as root, "6" as the right child and "1" as left child. Now if our input is changed to [5,1,6], our BST structure will still be identical. So given an array of integers, how to find the number of different permutations of the input array that results in the identical BST as the BST formed on the original array order? Your question is equivalent to the question of counting the number of topological orderings for the given BST. For example, for the BST 10 / \ 5 20 \7 | \ 15 30

Why lookup in a Binary Search Tree is O(log(n))?

别来无恙 提交于 2019-12-03 05:49:38
问题 I can see how, when looking up a value in a BST we leave half the tree everytime we compare a node with the value we are looking for. However I fail to see why the time complexity is O(log(n)) . So, my question is: If we have a tree of N elements, why the time complexity of looking up the tree and check if a particular value exists is O(log(n)), how do we get that? 回答1: Your question seems to be well answered here but to summarise in relation to your specific question it might be better to

Finding the minimum and maximum height in a AVL tree, given a number of nodes?

て烟熏妆下的殇ゞ 提交于 2019-12-03 05:15:52
问题 Is there a formula to calculate what the maximum and minimum height for an AVL tree, given a certain number of nodes? For example: Textbook question: What is the maximum/minimum height for an AVL tree of 3 nodes, 5 nodes, and 7 nodes? Textbook answer: The maximum/minimum height for an AVL tree of 3 nodes is 2/2, for 5 nodes is 3/3, for 7 nodes is 4/3 I don't know if they figured it out by some magic formula, or if they draw out the AVL tree for each of the given heights and determined it that

How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space

a 夏天 提交于 2019-12-03 04:29:07
问题 How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space. 回答1: You don't give much to go on, but if the requirement is what I think it is, you have a binary tree already created and sitting in memory, but not sorted (the way you want it to be sorted, anyway). I'm assuming that the tree nodes look like struct tree_node { struct tree_node * left; struct tree_node * right; data_t data; }; I'm also assuming that you can read C While we could just sit around

how to rebuild BST using {pre,in,post}order traversals results

不想你离开。 提交于 2019-12-03 03:54:43
We know the pre-order, in-order and post-order traversals. What algorithm will reconstruct the BST? Because it is BST, in-order can be sorted from pre-order or post-order <1>. Actually, either pre-order or post-order is needed only.... <1> if you know what the comparison function is From pre-order and in-order , to construct a binary tree BT createBT(int* preOrder, int* inOrder, int len) { int i; BT tree; if(len <= 0) return NULL; tree = new BTNode; t->data = *preOrder; for(i = 0; i < len; i++) if(*(inOrder + i) == *preOrder) break; tree->left = createBT(preOrder + 1, inOrder, i); tree->right

Binary Tree in Objective-C

℡╲_俬逩灬. 提交于 2019-12-03 03:22:00
问题 I am learning algorithms and data structures and to train I am trying to design and implement a binary tree using objective-c. So far I have the following Classes: main - for testing Node - node of tree BinaryTree - for all methods related to the tree One of the first methods in BinaryTree class I implemented is insertNode:forRoot: . - (void)insertNodeByRef:(Node **)node forRoot:(Node **)root{ if (head == NULL) { head = *node; } // Case 2 root is null so can assign the value of the node to it

Deletion procedure for a Binary Search Tree

纵饮孤独 提交于 2019-12-03 03:11:38
Consider the deletion procedure on a BST, when the node to delete has two children. Let's say i always replace it with the node holding the minimum key in its right subtree. The question is: is this procedure commutative? That is, deleting x and then y has the same result than deleting first y and then x? I think the answer is no, but i can't find a counterexample, nor figure out any valid reasoning. EDIT: Maybe i've got to be clearer. Consider the transplant(node x, node y) procedure: it replace x with y (and its subtree). So, if i want to delete a node (say x) which has two children i