avl-tree

Minimum number of node in AVL tree?

佐手、 提交于 2019-12-10 13:43:49
问题 I know the formula of finding minimum number of node in a AVL tree is S(h) = S(h-1) + S(h-2) + 1 However, I don't really get how to use this function, say if we have a AVL height of 6. The answer tells me that Minimum = 7 + 4 + 1 =12. But how do you get this number? I mean when you plug in 6 isn't it (6-1) + (6-2) + 1? Can anyone explain to me how to solve this? My teacher haven't talk about this yet but I really want to figure this out myself in order to be prepared for the test next week.

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

Rotating a binary search tree

有些话、适合烂在心里 提交于 2019-12-08 10:52:07
问题 I am implementing a binary search tree. I have created a method to rotate the tree if I find that the tree is out of balance. Somehow this method is not working correctly and my tree is emptied. Only the last two values I added to make the tree unbalanced are still left in the tree. Can you tell me what is wrong with my algorithm? I have not posted the entire class because it is very long and do not want to confuse anyone. public boolean balanceBST(){ if (root == null){ return false; } int

Recalculation of Balance Factor in AVL Tree

戏子无情 提交于 2019-12-08 07:52:48
问题 After performing a rotation to balance an AVL tree, immediately after an insertion, how can I change the balance factor of all the parent nodes (appropriately, by -1 or 1)? Each node of the AVL tree has the following structure: typedef struct _avlTree { nutbolt part; int balanceFactor; struct _avlTree *left,*right; } *avlTree; I have set the balance factor as per the definition given on Wikipedia. Do I need to have a pointer to the parent node in each node? 回答1: You either need a parent

Running time of adding N elements into an empty AVL tree

删除回忆录丶 提交于 2019-12-08 06:35:48
问题 The formula for "minimum nodes for an avl tree in height h" is recursive: n(0)=1, n(1)=2 n(h)= 1+n(h-1)+n(h-2) On the other hand, i found this in the internet for explanation of the complexity of adding N elements to a empty avl tree: Well, imagine the tree being built. One by one, the elements go through the tree nodes, and chose their abode by taking either a left or a right. The tree balances itself every time the heights are too skewed. Of course, the cost of balancing the tree is an O(1)

haskell - optimizing AVL tree

白昼怎懂夜的黑 提交于 2019-12-07 13:15:41
问题 as a part of learning haskell i decided to implement an AVL tree. as of now, i only implemented insertion. the implementation works, but it performs 3-4 times slower than a random java implementation i found for a random list of 9999999 random numbers. it performs almost as well when given an input list of [1..9999999] or [9999999..1] (descending or ascending list), so i think the problem may lie in the rl and lr rolls i would appreciate any hint about how to make it run faster. yes, i know

Get median from AVL tree?

☆樱花仙子☆ 提交于 2019-12-07 10:29:44
问题 If you have an AVL tree, what's the best way to get the median from it? The median would be defined as the element with index ceil(n/2) (index starts with 1) in the sorted list. So if the list was 1 3 5 7 8 the median is 5. If the list was 1 3 5 7 8 10 the median is 5. If you can augment the tree, I think it's best to let each node know the size (number of nodes) of the subtree, (i.e. 1 + left.size + right.size). Using this, the best way I can think of makes median searching O(lg n) time

Computational Complexity of TreeSet methods in Java

六月ゝ 毕业季﹏ 提交于 2019-12-07 02:30:48
问题 Is the computational complexity of TreeSet methods in Java, same as that of an AVLTree? Specifically, I want to know the computational complexity of the following methods: 1.add 2.remove 3.first 4.last 5. floor 6. higher Java Doc for method description: http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html For an AVL Tree, there are all O(logn)? Whats the complexity of the above TreeSet Methods? 回答1: Operations which work on a single element are all O(ln n) comparisons except first

haskell - optimizing AVL tree

本小妞迷上赌 提交于 2019-12-05 21:49:42
as a part of learning haskell i decided to implement an AVL tree. as of now, i only implemented insertion. the implementation works, but it performs 3-4 times slower than a random java implementation i found for a random list of 9999999 random numbers. it performs almost as well when given an input list of [1..9999999] or [9999999..1] (descending or ascending list), so i think the problem may lie in the rl and lr rolls i would appreciate any hint about how to make it run faster. yes, i know it looks kind of ugly. data Tree a = Empty | Branch { key :: a, balance :: Int, left :: Tree a, right ::

Get median from AVL tree?

为君一笑 提交于 2019-12-05 11:55:33
If you have an AVL tree, what's the best way to get the median from it? The median would be defined as the element with index ceil(n/2) (index starts with 1) in the sorted list. So if the list was 1 3 5 7 8 the median is 5. If the list was 1 3 5 7 8 10 the median is 5. If you can augment the tree, I think it's best to let each node know the size (number of nodes) of the subtree, (i.e. 1 + left.size + right.size). Using this, the best way I can think of makes median searching O(lg n) time because you can traverse by comparing indexes. Is there a better way? Augmenting the AVL tree to store