Questions tagged [tree-balancing]

In the context of data structures, tree balancing refers to reorganizing the nodes in a binary search tree to ensure that the height of the tree is not too large.

79 questions
2
votes
0 answers

C++ AVL Tree balancing Issues

I've built an AVL Tree out of an old Binary Tree in C++ for practice and it is not working correctly. I think it may not be updating the height of the nodes correctly but I can't figure out the root of the issue ( I'm 90% sure it has to do with the…
BitShift
  • 39
  • 3
2
votes
1 answer

How to make a binary tree balance

Here is a simple binary tree in c, but it seems not balance, how to make it balance? Code: /** * binary_tree impl */ #include #include typedef struct _tnode _tnode; typedef struct _bin_tree _bin_tree; struct _tnode { …
Eric
  • 22,183
  • 20
  • 145
  • 196
2
votes
2 answers

BST of minimal height

I'm trying to solve the following problem: "Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a BST with minimal height." The given answer takes the root node to be the middle of the array. While…
randomUser47534
  • 329
  • 1
  • 5
  • 18
2
votes
2 answers

Keeping avl tree balanced without rotations

B Tree is self balancing tree like AVL tree. HERE we can see how left and right rotations are used to keep AVL tree balanced. And HERE is a link which explains insertion in B tree. This insertion technique does not involve any rotations, if I am…
Andy897
  • 6,915
  • 11
  • 51
  • 86
2
votes
0 answers

In-place balancing of Binary Search Tree stored in array

Lets assume I have a BST that is not balanced. The BST is stored in array, where root is at index 0 and left and right children of node on index k are on indices 2k and 2k + 1 respectively. For example a tree: 5 4 …
wondra
  • 3,271
  • 3
  • 29
  • 48
2
votes
2 answers

C++ tree AVL balance

I've run in to an issue with the balance part of my tree. I have checkBal called after a recursive insertion. If I try to add 5, 2 and 4 it checks the balance of 2 and continues back up to 5 then goes into rotateLeft part of the right rotate which…
krizzo
  • 1,823
  • 5
  • 30
  • 52
2
votes
1 answer

Splay tree rotation algorithm: Why use zig-zig and zig-zag instead of simpler rotations?

I don't quite understand why the rotation in the splay tree data structure is taking into account not only the parent of the rating node, but also the grandparent (zig-zag and zig-zig operation). Why would the following not work: as we insert, for…
Bober02
  • 15,034
  • 31
  • 92
  • 178
1
vote
2 answers

Doubt Regarding Function to check whether a tree is balanced or not?

I read in a book named "Coding Interview Cracked", that to check whether a BST is balanced or not, just find out the difference between the maximum and minimum height but I not sure whether it is 100% correct. Though I am unable to find a counter…
ua741
  • 1,446
  • 15
  • 28
1
vote
3 answers

Balancing String based Binary Search Tree (For Spellchecking)

Update: I can't get "Balancing" to work, because I cannot get "doAVLBalance" to recognize the member functions "isBalanced()", "isRightHeavy()", "isLeftHeavy". And I don't know why! I tried Sash's example(3rd answer) exactly but I get "deceleration…
1
vote
1 answer

How to fix segmentation fault in AVL deletion operation when rebalancing?

I am implementing an AVL tree and my search and insertion functions work properly, but I get a segmentation fault with my remove function. I have implemented a BST tree correctly before, so I know the issue is with the rebalancing of the tree rather…
1
vote
2 answers

Is there any use for a binary search tree that is not self-balanced?

All of the types of binary search trees I can find are self-balancing. Are there any that aren't, that are actually useful?
mattalxndr
  • 9,143
  • 8
  • 56
  • 87
1
vote
2 answers

Batch operations with balancing trees

I use a self-balancing binary search tree (currently it is an AVL tree but can be substituted with another one). I noticed that there are distinct periods when only certain operation is performed: large deletion or insertion batches are executed…
Andrey Godyaev
  • 856
  • 1
  • 8
  • 18
1
vote
0 answers

Is AVL/Red-black Trees balancing rule extensible to any unbalanced tree situation?

I was wondering whether the AVL-tree or Red-black-tree balancing method (which happens at each node insertion by simple rotation) could be extended to any given binary tree at any stage of the binary tree construction. I found that following AVL or…
1
vote
3 answers

Balancing an AVL tree

I'm having trouble with a balancing-AVL tree question, since my solution appears to conflict with the back-of-the-textbook solution. I've looked at online visualizations of AVL trees, and they suggest mine is correct. Is my textbook wrong? This is…
Konrad
  • 163
  • 2
  • 16
1
vote
1 answer

Balance BST tree manually

I've done balancing of the tree(bst>avl) requested by hand and I wonder that it was really easy, so I am not sure whether I've done it correctly. a / \ b e3 / \ e1 e2 initial state is: 'a' is parent of 'b'(left) and…
rotator
  • 11
  • 1