binary-tree

What is the average asymptotic depth of a simple unbalanced search tree?

三世轮回 提交于 2019-12-07 17:38:38
问题 For a balanced search tree, it is O(log(N)) for all cases. For unbalanced search trees, the worst case is O(N), e.g., insert 1, 2, 3, 4,.. and best case complexity is when it is balanced, e.g., insert 6, 4, 8, 3, 5 7. How do we define the average case complexity for unbalanced search tree? 回答1: The average height of Binary Trees is Theta(sqrt(n)). This has been shown (or referenced, not very sure) in the following paper: http://www.dtc.umn.edu/~odlyzko/doc/arch/extreme.heights.pdf. But

Simple numerical expression solver

别说谁变了你拦得住时间么 提交于 2019-12-07 15:11:24
问题 First of all, sorry for my bad English. For my last project on my Algorithms and Data Structures class, I need to create a simple numerical expression solver in C++. It needs to solve simple expressions like 3*12+(4-6) . I managed to split the expression and separate the operators from the numbers, but I can't go any further. The trick is putting the operators on a binary tree, but I haven't managed to do that. The program needs to use only the default C++ libs. Maybe there is some basic

How to split a rope tree?

点点圈 提交于 2019-12-07 14:11:46
问题 I came across a rope tree as an alternative data structure for a string. http://en.wikipedia.org/wiki/Rope_(data_structure) To concat is easy, but I am stuck on the split operation. The wikipedia article states: For example, to split the 22-character rope pictured in Figure 2.3 into two equal component ropes of length 11, query the 12th character to locate the node K at the bottom level. Remove the link between K and the right child of G. Go to the parent G and subtract the weight of K from

Recursion that branches running out of memory

做~自己de王妃 提交于 2019-12-07 08:34:01
问题 I have a programming assignment that goes like this: You are given three numbers a, b, and c. (1 ≤ а, b, c ≤ 10^18) Each time you have two choises, either add b to a (a+=b), or add a to b (b+=a). Write a program that will print out YES or NO depending on whether you can get to c by adding a and b to each other. I've tried solving this problem using recursion that branches to two branches every time where one branch stores a+b, b and the other branch stores a, b+a. In every recursive call, the

How to draw binary tree view using WPF?

五迷三道 提交于 2019-12-07 07:43:14
问题 I want to draw it like this Image : I can draw a binary tree on console. I want to draw it using WPF. Here is my code which I write for console. class Program { static void Main(string[] args) { List<BinaryTreeData> myBinaryData = new List<BinaryTreeData>(); myBinaryData.Add(new BinaryTreeData{ownID=1}); myBinaryData.Add(new BinaryTreeData { parentID=1, ownID = 2 }); myBinaryData.Add(new BinaryTreeData { parentID=1,ownID = 3 }); foreach (var item in myBinaryData) { Console.WriteLine("{0}-----

how to find the height of a node in binary tree recursively

一笑奈何 提交于 2019-12-07 06:02:51
问题 path = 0 # the lenght of the path while self.right != None or self.left != None: while self.right != None: self = self.right path = path +1 while self.left != None: self = self.left path = path +1 return path this is my sample code for find the Height, is defined as the length of the longest path by number of nodes from self to a leaf. The height of a leaf node is 1. it doesn't work. 回答1: What you're doing isn't recursive, it's iterative. Recursive would be something like: def height(node):

Implementing Binary Tree in Ruby

徘徊边缘 提交于 2019-12-07 04:37:43
问题 I've been trying to implement BinaryTree class in Ruby, but I'm getting the stack level too deep error, although I don't seem to be using any recursion in that particular piece of code: 1. class BinaryTree 2. include Enumerable 3. 4. attr_accessor :value 5. 6. def initialize( value = nil ) 7. @value = value 8. @left = BinaryTree.new # stack level too deep here 9. @right = BinaryTree.new # and here 10. end 11. 12. def empty? 13. ( self.value == nil ) ? true : false 14. end 15. 16. def <<(

What python code generates all possible groupings (trees) for binary operators

社会主义新天地 提交于 2019-12-07 01:40:27
问题 As explained in several SO questions, and more abstractly at mathworld, the sequence of Catalan numbers happens to correspond to the number of parenthetical groupings that can be generated for any given number of operators. But I haven't found an algorithm to generate all these groupings. This binary bracketing algorithm corresponds to the Tamari Lattice and can be described in a number of different ways. The most obvious practical use for this algorithm is to generate all possible

Printing Successor and Predecessor in a BST

[亡魂溺海] 提交于 2019-12-06 17:30:30
My problem is as follows: I have a binary search tree with keys: a1<a2<...<an , the problem is to print all the (a_i, a_i+1) pairs in the tree (where i={1,2,3,...}) using a recursive algorithm in O(n) time without any global variable and using O(1) extra space. An example: Let the keys be: 1,2, ..., 5 Pairs that will be printed: (1,2) (2,3) (3, 4) (4, 5) So you can't do inorder traversal in the tree and find the successor/predecessor for each node. Because that would take O(nh) time and if the tree is balanced, it will be O(nlgn) for the whole tree. Although you are right that finding an in

Remove all nodes in a binary three which don’t lie in any path with sum>= k

◇◆丶佛笑我妖孽 提交于 2019-12-06 17:05:45
问题 Not able to understand answer given HERE Can someone please help to understand. My Algo: Recursively find sum of each path. If sum >=k, put all the nodes in the path in a hashset At the end traverse the tree, remove nodes which are not there in hashset. I am pretty sure, there is a lot of scope of improvement here. 回答1: You have tree and you are recursively parsing it like this : go_node(Node node){ go_node(node.left); go_node(node.right); } At your example, you want to delete any subtree