binary-tree

How to tell whether a red-black tree can have X black nodes and Y red nodes or not

﹥>﹥吖頭↗ 提交于 2019-12-10 10:46:59
问题 I have an exam next week in algorithms, and was given questions to prepare for it. One of these questions has me stumped though. "Can we draw a red-black tree with 7 black nodes and 10 red nodes? why?" It sounds like it could be answered quickly, but I can't get my mind around it. The CRLS gives us the maximum height of a RB tree with n internal nodes: 2*lg(n+1). I think the problem could be solved using this lemma alone, but am not sure. Any tips? 回答1: Since this is exam preparation, I don't

generating a binary tree from given data in python

烈酒焚心 提交于 2019-12-10 10:38:34
问题 i wanted to know how to read values from a list into a binary tree. i have a triangle like this: 0 1 2 3 4 5 6 7 8 9 i have written a class node like this class node: def __init__(self,data,left=None,right=None): self.data=data self.left=left self.right=right basically what i want to do is something like this node(0,node(1),node(2)) i want to make a recursive function that can handle much bigger triangles. Can somehow tell me what i am supposed to do? edit: quite clearly binary tree is not

How to do tail recursion for a binary tree?

≯℡__Kan透↙ 提交于 2019-12-10 06:54:27
问题 If you have a binary tree, how can you iterate through (using in order) it using tail recursion? I know that tail recursion involves you to calculate the new value as you iterate, and then when you reach the base case, you just return the accumulation. But how do you do this for a tree, when you have to call the function call twice? 回答1: Assuming a depth-first left-to-right traversal, you cannot use tail recursion for the left-hand branch, but you can use it for the right-hand branch. Example

Is a red-black tree my ideal data structure?

守給你的承諾、 提交于 2019-12-10 02:15:59
问题 I have a collection of items (big rationals) that I'll be processing. In each case, processing will consist of removing the smallest item in the collection, doing some work, and then adding 0-2 new items (which will always be larger than the removed item). The collection will be initialised with one item, and work will continue until it is empty. I'm not sure what size the collection is likely to reach, but I'd expect in the range 1M-100M items. I will not need to locate any item other than

Reconstructing binary tree from inorder and preorder traversals

末鹿安然 提交于 2019-12-09 19:24:18
问题 I have written the following code for constructing a tree from its inorder and preorder traversals. It looks correct to me but the final tree it results in does not have the same inorder output as the one it was built from. Can anyone help me find the flaw in this function? public btree makeTree(int[] preorder, int[] inorder, int left,int right) { if(left > right) return null; if(preIndex >= preorder.length) return null; btree tree = new btree(preorder[preIndex]); preIndex++; int i=0; for(i

Looking for a java library that has implemented Binary Tree [closed]

*爱你&永不变心* 提交于 2019-12-09 08:11:00
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Is there a java library that has Binary Tree that I can use? I am not looking forward to test and implement my own. 回答1: The Java standard API only contains libraries that are universally useful and non-trivial to implement. A basic tree is trivial to implement: class BinaryTree { BinaryTree left; BinaryTree

set position for drawing binary tree

假装没事ソ 提交于 2019-12-09 07:13:24
问题 I want to drawing a binary tree with an graphical framework(Qt) like this: 9 / \ 1 10 / \ \ 0 5 11 / / \ -1 2 6 but I have a problem to set X and Y for every node, do you any idea to setting and fixation position ? (I have only height of every node and left-Child and right-Child) 回答1: Given the width canvasWidth and the height canvasHeight of the canvas you can calculate position of each node. First, let's assign two numbers to each node: the depth of the node and a serial index of the node

Why is inorder and preorder traversal useful for creating an algorithm to decide if T2 is a subtree of T1

本小妞迷上赌 提交于 2019-12-09 05:25:04
问题 I'm looking at an interview book and the question is: You have two very large binary trees: T1 , with millions of nodes, and T2 , with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1 . The authors mentions this as a possible solution: Note that the problem here specifies that T1 has millions of nodes—this means that we should be careful of how much space we use. Let’s say, for example, T1 has 10 million nodes—this means that the data alone is about 40 mb . We could

Count number of left nodes in BST

爱⌒轻易说出口 提交于 2019-12-09 03:08:48
问题 Given a BST, I am required to find the number of left nodes of the tree. Example: ` +---+ | 3 | +---+ / \ +---+ +---+ | 5 | | 2 | +---+ +---+ / / \ +---+ +---+ +---+ | 1 | | 4 | | 6 | +---+ +---+ +---+ / +---+ | 7 | +---+` The answer should be 4, as (5, 1 , 4, 7) are all left nodes of the tree. What I am thinking of doing is: public int countLeftNodes() { return countLeftNodes(overallRoot, 0); } private int countLeftNodes(IntTreeNode overallRoot, int count) { if (overallRoot != null) { count

Print all paths from root to leaf in a Binary tree

不想你离开。 提交于 2019-12-09 01:22:54
问题 Here is the code I wrote to print all paths of a Binary tree from root to leaf: public static void printRootToLeaf(Node1 root, List<Integer> list) { if(root == null) { return; } list.add(root.data); if(root.left == null && root.right == null) { System.out.println(list); return; } printRootToLeaf(root.left, list); printRootToLeaf(root.right, list); } I am calling this method in main like this: public static void main(String[] args) { Node1 root = new Node1(1); Node1 two = new Node1(2); Node1