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.

76 questions
65
votes
9 answers

The best way to calculate the height in a binary search tree? (balancing an AVL-tree)

I'm looking for the best way to calculate a nodes balance in an AVL-tree. I thought I had it working, but after some heavy inserting/updating I can see that it's not working correct (at all). This is kind of a two-part question, the first part…
thr
  • 18,022
  • 19
  • 89
  • 129
19
votes
4 answers

What Self Balancing Tree is simplest in Functional Programming?

I'm designing a self balancing tree in Haskell. As an exercise and because it is nice to have in your back hand. Previously in C and Python I preferred Treaps and Splay Trees due to their simple balancing rules. I always disliked R/B Trees, since…
Thomas Ahle
  • 28,005
  • 19
  • 77
  • 105
10
votes
1 answer

how to find all possible solutions to a formula, like 100*7-8*3+7? (8 Out of 10 Cats Does Countdown solver)

so as fun i decided to write a simple program that can solve the 8 Out of 10 Cats Does Countdown number puzzle, link is form Countdown, but same rules. so my program simply goes through a all possible combinations of AxBxCxDxExF, where letters are…
Shawn
  • 413
  • 8
  • 35
6
votes
3 answers

Are there versions of the C++ STL's associative data structures optimized for numerous partial copies?

I have a large tree that grows as my algorithm progresses. Each node contains set, which I suppose is implemented as balanced binary search tree. Each node's set shall remain fixed after that node's creation, before its use in creating that node's…
Jeff Burdges
  • 4,036
  • 21
  • 41
5
votes
3 answers

What's the complexity of map/set :: insert if one has provided a correct iterator hint?

Is it O(1) or O(logN) but with a smaller coefficient? If this is unspecified, I'd at least like to know the answer based on a reasonable supposition that the map/set is implemented using a red-black or AVL tree. The general algorithm to insert an…
Armen Tsirunyan
  • 120,726
  • 52
  • 304
  • 418
4
votes
4 answers

Left balanced binary trees

I am reading a book on data structures and it says that a left balanced binary tree is a tree in which the leaves only occupy the leftmost positions in the last level. This seemed a little vague to me. Does this mean that leaves are only on the…
rubixibuc
  • 6,013
  • 18
  • 50
  • 86
4
votes
1 answer

Balancing arithmetic expression tree with +, - operators

Given a binary arithmetic expression tree consisting of only addition and subtraction operators, and numbers, how to balance the tree as much as possible? The task is to balance the tree without evaluating the expression, that is the number of nodes…
4
votes
2 answers

AVL Tree Balancing

I am working on an assignment that asks me to implement an AVL tree. I'm pretty sure I have the rotation methods correct, but I'm having trouble figuring out when to use them. For example, the explanation in the book says that I should climb up the…
Will M
  • 2,095
  • 4
  • 21
  • 29
3
votes
1 answer

Balancing a binary search tree recursively

I have a BinarySearchTree with objects in it of Instance bankaccount which is a class I created, so basically it's just a binarysearchtree and I wrote a method that will take the tree and balance it, for some reason it prints out exactly the tree…
Ed_
  • 347
  • 1
  • 8
3
votes
1 answer

Proper way to Re-Balance a 2-3 Tree after deleting the root node

The goal is to remove 22 from the root node and re-balance the tree. First I remove 22, and replace it by its in-order successor 28. Secondly I rebalance the resulting tree, by moving the empty node to the left. The resulting tree is below. Is…
eman_44
  • 80
  • 7
3
votes
1 answer

Grammar balancing issue

Is it possible to force Boost.Spirit Qi to behave in such way, that generated grammar would be adjustable in compliance with some runtime-calculable conditions/rules/rates? For example, the input consists of language constructs, that cause the…
Tomilov Anatoliy
  • 13,614
  • 8
  • 46
  • 134
3
votes
1 answer

Removing multiple items from balancing binary tree at once

I'm using a red-black binary tree with linked leafs on a project (Java's TreeMap), to quickly find and iterate through the items. The problem is that I can easily get 35000 items or so on the tree, and several times I have to remove "all items above…
3
votes
3 answers

Balancing KD Tree

So when balancing a KD tree you're supposed to find the median and then put all the elements that are less on the left subtree and those greater on the right. But what happens if you have multiple elements with the same value as the median? Do they…
user1855952
  • 1,321
  • 5
  • 24
  • 51
2
votes
1 answer

Prolog - Balanced tree or not

I want to write a program that tells me if a tree is balanced or not. In this case balanced means same height or a height difference of 1. This is what I've written so far but it doesn't work for the height difference of 1.…
user940599
  • 71
  • 2
  • 7
2
votes
1 answer

Modify a BinarySearchTree to be balanced (AVL) : Java

I need to modify a Binary Search Tree that I created to assure that it is balanced. I only need to modify the add and remove methods, according to my instructions. Here's what I currently have: package proj; public class BinarySearchTree
Chris V.
  • 1,083
  • 4
  • 19
  • 48
1
2 3 4 5 6