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
1
vote
2 answers

How to balance binary tree in PHP without rotating parent?

I will try to make myself as clear as possible. Based from Adjacency List Model: http://articles.sitepoint.com/article/hierarchical-data-database I need a way to balance this tree 0 / \ 1 2 / / \ 3 4 5 \ \ …
OBL
  • 367
  • 1
  • 4
  • 13
1
vote
1 answer

How many keys can be contained at leaf level in B+ Tree

In my database class, my professor was describing about deleting keys from the B+ Tree. If you see the below image: I totally understood everything except one part where he told that the leaf level nodes can only contain 3 keys at most. As my per…
python
  • 4,403
  • 13
  • 56
  • 103
1
vote
0 answers

Balancing a BST recursively

I have a class of BST. I require a recursive function Rotate that takes the root of the BST as argument and returns a balanced BST. Attached is the code that balances only one node, but it is not working for root node. I want to make it recursive…
Nidaa
  • 11
  • 2
1
vote
1 answer

Java BinaryTree: How to balance a tree in the insert method?

I am trying to get a binary search tree to balance, and I know why it's not working, but I don't know how to fix it. I balance directly in the insert method. I put some slash to note where the balancing should happen. Like this the code is not…
1
vote
3 answers

Proving tree is balanced from subtrees's balancedness

I'm solving the following problem from "Cracking the Coding Interview": Implement a function to check if a binary tree is balanced. A balanced tree is a tree such that the heights of the two subtrees of any node never differ by more than one. The…
randomUser47534
  • 329
  • 1
  • 5
  • 18
1
vote
1 answer

Why the run time of similar solution is so different?

There is a problem in LeetCode. I use a straightforward recursive solution to solve it, but the run time is to long, which is 170ms. Then I found a similar solution, which also is recursive, and the run time of this is just about 10ms. Why? My…
therdes
  • 13
  • 2
1
vote
1 answer

Haskll, height balanced check

Is there a way to convert this, so that it checks for balance based on height? (Assuming this is what the problem is.) size :: Tree a -> Int size (Leaf n) = 1 size (Node x z) = size x + size z + 1 isBalanced :: Tree a -> Bool isBalanced (Leaf _)…
Zast
  • 492
  • 2
  • 7
  • 22
1
vote
1 answer

Can we say that any segment tree is balanced?

Complexity of the operation in the segment tree is equal to the O(logn) on the basis of this can we say that any segment tree is balanced?
user1886376
1
vote
4 answers

Understanding Balance Factors/Node Height for AVL rotations

So I am having a hard time understanding how to balance AVL trees. I understand the rotation stuff, but I can't figure out how to find the balance factor of node heights, for example: https://i.stack.imgur.com/UuK5f.png Can anyone explain to me how…
user1861967
  • 121
  • 5
  • 14
1
vote
1 answer

Amortized cost for a (fixed) balanced tree

Suppose I have a constant (once built doesn't change) balance tree with N nodes, every internal node having p children. Obviously the worse case scenario for accessing a node is logp(N). But what about the amortized cost for accessing r nodes? what…
user1377000
  • 1,433
  • 3
  • 17
  • 29
1
vote
2 answers

AVL Binary Tree - Balanace test

I'm trying to achieve a testing if a tree is AVL tree or not using prolog. I've made a height test that works for the tests I've done so far but my balancing test is still not going strong. This is my work so…
Anticipating
  • 141
  • 1
  • 3
  • 13
0
votes
1 answer

Balancing a BST with weights

I'm building a recursive Java method to balance a binary search tree (using ints, but designed generic) using weights in each node. For my purpose, the weight of a node is defined as the number of children + 1. 2 / \ 1 3 The weight of the…
0
votes
1 answer

AVL tree rotation occurs even when the tree is balanced

#include #include typedef struct node *treenode; struct node { int data; int height; treenode left; treenode right; }; int height(treenode t) { if(t == NULL) return -1; else return…
RUFINA
  • 1
  • 1
0
votes
0 answers

Does comparing string representation of dates in a binary tree implementation offer any advantages over comparing dates directly?

Does lexical comparation of Dates (or DateTimes) offer any advantages when constructing Binary Search Trees over direct comparation of Dates (or DateTimes) when constructing the tree using the date as a key? The newer dates will always follow after…
dodekja
  • 537
  • 11
  • 24
0
votes
1 answer

AVL tree Balancing, which of these 2 methods

I have implemented an AVL tree, but I have a problem. Suppose I have following tree, which of the both solutions for balancing are correct? Solution 1: Solution 2:
BerndHx
  • 1
  • 1