Questions tagged [binary-trees]

a rooted tree in which each node has no more than two children

A binary tree is a tree type data structure where each node has at most two child nodes.

The image shows the rough model of binary tree.

Single parent or single level, complete binary tree

A binary tree is usually used in binary search. Further reading of binary search trees on Wikipedia. There are many types of binary trees on level of nodes and completion of child with a parent.

Example: A binary tree with complete nodes filled (i.e. every parent has two child nodes) is called a full binary tree. In respect to the image, a full binary tree will have the same structure as in image in all its nodes.

Full detailed reading of binary trees.

569 questions
141
votes
4 answers

BIT: What is the intuition behind a binary indexed tree and how was it thought about?

A binary indexed tree has very less or relatively no literature as compared to other data structures. The only place where it is taught is the topcoder tutorial. Although the tutorial is complete in all the explanations, I cannot understand the…
Nikunj Banka
  • 1,585
  • 3
  • 11
  • 9
129
votes
5 answers

What's the difference between a binary search tree and a binary heap?

These two seem very similar and have almost an identical structure. What's the difference? What are the time complexities for different operations of each?
Piper
  • 1,779
  • 2
  • 12
  • 13
39
votes
2 answers

Hash tables versus binary trees

When implementing a dictionary ('I want to look up customer data by their customer IDs'), the typical data structures used are hash tables and binary search trees. I know for instance that the C++ STL library implements dictionaries (they call them…
Alex ten Brink
  • 9,206
  • 3
  • 36
  • 63
31
votes
1 answer

Which combinations of pre-, post- and in-order sequentialisation are unique?

We know post-order, post L(x) => [x] post N(x,l,r) => (post l) ++ (post r) ++ [x] and pre-order pre L(x) => [x] pre N(x,l,r) => [x] ++ (pre l) ++ (pre r) and in-order traversal resp. sequentialisation. in L(x) => [x] in N(x,l,r) => (in…
Raphael
  • 73,212
  • 30
  • 182
  • 400
31
votes
2 answers

Counting binary trees

(I'm a student with some mathematical background and I'd like to know how to count the number of a specific kind of binary trees.) Looking at Wikipedia page for Binary Trees, I've noticed this assertion that the number of rooted binary trees of size…
Stéphane Gimenez
  • 1,490
  • 1
  • 14
  • 29
30
votes
2 answers

Not all Red-Black trees are balanced?

Intuitively, "balanced trees" should be trees where left and right sub-trees at each node must have "approximately the same" number of nodes. Of course, when we talk about red-black trees*(see definition at the end) being balanced, we actually mean…
Aryabhata
  • 6,291
  • 2
  • 36
  • 47
28
votes
1 answer

Two definitions of balanced binary trees

I have seen two definitions of balanced binary trees, which look different to me. A binary tree is balanced if for each node it holds that the number of inner nodes in the left subtree and the number of inner nodes in the right subtree differ by at…
forrestGump
  • 787
  • 1
  • 10
  • 14
26
votes
1 answer

Why does the splay tree rotation algorithm take into account both the parent and grandparent node?

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
  • 383
  • 3
  • 6
22
votes
1 answer

AVL trees are not weight-balanced?

In a previous question there was a definition of weight balanced trees and a question regarding red-black trees. This question is to ask the same question, but for AVL trees. The question is, given the definition of $\mu$-balanced trees as in the…
21
votes
2 answers

Proving a binary heap has $\lceil n/2 \rceil$ leaves

I'm trying to prove that a binary heap with $n$ nodes has exactly $\left\lceil \frac{n}{2} \right\rceil$ leaves, given that the heap is built in the following way: Each new node is inserted via percolate up. This means that each new node must be…
varatis
  • 463
  • 1
  • 4
  • 8
20
votes
2 answers

Creating a Self Ordering Binary Tree

I have an assignment where I need to make use a binary search tree and alter it to self order itself such that items that are accessed the most (have a higher priority) are at the top of the tree, the root being the most accessed node. The professor…
20
votes
5 answers

Efficient compression of unlabeled trees

Consider unlabeled, rooted binary trees. We can compress such trees: whenever there are pointers to subtrees $T$ and $T'$ with $T = T'$ (interpreting $=$ as structural equality), we store (w.l.o.g.) $T$ and replace all pointers to $T'$ with pointers…
Raphael
  • 73,212
  • 30
  • 182
  • 400
16
votes
2 answers

Is there a faster solution for the Google Code Jam Great Wall Problem

Consider the following Google Code Jam round 1C question: The Great Wall of China starts out as an infinite line, where the height at all locations is $0$. Some number of tribes $N$, $N \le 1000$, will attack the wall the wall according to the…
torquestomp
  • 378
  • 1
  • 6
16
votes
2 answers

Colour a binary tree to be a red-black tree

A common interview question is to give an algorithm to determine if a given binary tree is height balanced (AVL tree definition). I was wondering if we can do something similar with Red-Black trees. Given an arbitrary uncoloured binary tree (with…
Aryabhata
  • 6,291
  • 2
  • 36
  • 47
14
votes
2 answers

Proving a binary tree has at most $\lceil n/2 \rceil$ leaves

I'm trying to prove that a binary tree with $n$ nodes has at most $\left\lceil \frac{n}{2} \right\rceil$ leaves. How would I go about doing this with induction? For people who were following in the original question about heaps, it has been moved…
1
2 3
37 38