binary-search-tree

How many ways can you insert a series of values into a BST to form a specific tree?

橙三吉。 提交于 2019-12-03 02:04:59
问题 This earlier question asked how many ways there were to insert the values 1 - 7 into a binary search tree that would result in the following tree: 4 / \ 2 6 / \ / \ 1 3 5 7 (The answer is 80, by the way). Suppose more generally that you're given an arbitrary BST holding some set of values and want to know how many possible ways there are to insert those values into a BST that would end up producing the resulting tree. Is there an efficient algorithm for determining this? Thanks! 回答1: We can

Real world examples of tree structures

心已入冬 提交于 2019-12-03 01:00:11
问题 I'm looking for some examples of tree structures that are used in commercial/free software projects, modern or old. I can see examples on wikipedia, but I am looking for more concrete examples and how they're used. For example primary keys in databases are (from what I've read) stored in BST structure or a variation of the BST (feel free to correct me on this) My question isn't limited Binary Search Trees (BSTs), it can include any variation such as red-black, AVL and so on. 回答1: Is it okay

Is it always possible to turn one BST into another using tree rotations?

血红的双手。 提交于 2019-12-02 21:12:38
Given a set of values, it's possible for there to be many different possible binary search trees that can be formed from those values. For example, for the values 1, 2, and 3, there are five BSTs we can make from those values: 1 1 2 3 3 \ \ / \ / / 2 3 1 3 1 2 \ / \ / 3 2 2 1 Many data structures that are based on balanced binary search trees use tree rotations as a primitive for reshaping a BST without breaking the required binary search tree invariants. Tree rotations can be used to pull a node up above its parent, as shown here: rotate u right v / \ -----> / \ v C A u / \ <----- / \ A B

For a given binary tree find maximum binary search sub-tree

烂漫一生 提交于 2019-12-02 20:53:40
For a given binary tree, find the largest subtree which is also binary search tree? Example: Input: 10 / \ 50 150 / \ / \ 25 75 200 20 / \ / \ / \ / \ 15 35 65 30 120 135 155 250 Output: 50 / \ 25 75 / \ / 15 35 65 This answer previously contained an O(n log n) algorithm based on link/cut trees. Here is a simpler O(n) solution. The core is a procedure that accepts a node, the unique maximum BSST rooted at its left child, the unique maximum BSST rooted at its right child, and pointers to the left-most and right-most elements of these BSSTs. It destroys its inputs (avoidable with persistent data

Print Binary search tree from biggest number to smallest using Java

时光怂恿深爱的人放手 提交于 2019-12-02 20:17:22
问题 I need a way, recursive / non recursive to print BST from biggest to smallest number, Example : for this tree came threw the answer of how to print BST I would like to get : 25,20,16,15,10,9,8,6,4,3,2,1 I know the way to print it opposite way : (in order) public void displaySmallToBig(Node root){ // inorder if(root!=null){ displaySmallToBig(root.left); System.out.print(" " + root.data); displaySmallToBig(root.right); } } Will print : 1 2 3 4 4 6 8 9 10 15 16 20 25 Thanks in advance 2 all the

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

佐手、 提交于 2019-12-02 18:31:15
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 way. The solution below is appropriate for working things out by hand and gaining an intuition, please

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

南楼画角 提交于 2019-12-02 17:42:50
How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space. 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 wondering why this tree was ever created without having been created in sorted order that doesn't do us any

How can I specify the range of a random number?

空扰寡人 提交于 2019-12-02 17:19:52
问题 I have binary search tree code that inserts numbers randomly. I can modify size each time, but I want to modify the range of numbers, for example: I want the random number be just one digit or just 2 digits. How can I do that? public static void main( String[ ] args ) { BinarySearchTree bst = new BinarySearchTree( ); Random random = new Random( System.currentTimeMillis() ); int[] randoms = new int[1000]; Random randGen = new Random(); for(int i = 0; i < randoms.length; i++) { bst.insert(

Binary Tree in Objective-C

丶灬走出姿态 提交于 2019-12-02 16:52:13
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 if (root == NULL) { root = node; } else { if (node.data > root.data) { // to the right [self

Why is the root always null in this method insert method for binary tree

浪子不回头ぞ 提交于 2019-12-02 15:20:07
问题 Iam trying to implement a recursive insert node method for BST class public void insertNode(Node r, Node n) { if(r == null) { System.out.println("r=n"+ n.data); r = n; } else { System.out.println("r=! null finding place in tree for "+ n.data); if(n.data <= r.data) { if(r.left == null) r.left = n; else insertNode(r.left, n); } else { if(r.right == null) r.right = n; else insertNode(r.right, n); } } } Im trying to invoke this method like so: int[] arrTree = {34, 2, 56, 12, 44, 39, 56, 1}; BT t