Skip to content

Binary Tree

Recursive Definition

Base Case: an empty tree is a binary tree; Definition: A tree having a root node, and two binary subtrees (respectively left subtree and right subtree, which can be empty) is a binary tree.

Central node

A BT's node x is said to Central when the count of its subtree's leaves is equal to the sum of the nodes' keys belonging to the path(root,x).

Full, Complete, and Perfect binary trees

Depending on how nodes are arranged in a binary tree, it can be full, complete and perfect:

  • Full binary tree: each node has exactly 0 or 2 children (but never 1).
  • Complete binary tree: when all levels, except the last one, are full of nodes.
  • Perfect binary tree: when all the levels (including the last one) are full of nodes.

Height

The height of binary tree is

O(log(n))h(T)O(n)

K-ary Tree

It is the generalization of a Binary Tree.

A k-ary tree is a tree where the children of a node are labeled with distinct positive integers iN+i[1,k].

  • No labels larger than k are present.
  • A binary tree is a K-ary tree with k = 2.

Complete K-ary Tree

It is a K-ary tree where

  1. All the leaves have the same depth
  2. All the internal nodes u have deg(u)=k=count(children).

Let us call f(T) the count of leaves, and i(T) the count of internal nodes of a tree with height h=h(T)

It can be demonstrated that for a Complete K-ary Tree:

TreeCount of leaves f(T)Count of internal nodes i(T)Height h(T)
Complete K-Ary Tree with height hf(T)=khi(T)=kh1/k1h(T)=logk(n)

Demonstration f(h)=kh

By definition, a complete k-ary tree has exactly k children per internal node

LevelChildren
01=k0
1k=k1
2kk=k2
kk
hkh

demcompkt-f

The root has exactly k children, and each one of the will have exactly k children, unless we reach the leaves.

Demonstration by induction: f(h)=kh for a complete k-ary tree

Base case: h=0k0=1, trivially true since we have the root in a non-empty complete k-ary tree

Inductive Hypothesis: We assume that f(h)=kh is true for a complete k-ary tree of height h

Inductive Step: By inductive hypothesis a tree with h+1

demcompkt-i

Since the tree is a complete k-ary tree,

  • At step h, f(h)=kh
  • At step h+1, f(h+1)=khk=kh+1 as each internal node at level h has exactly k children.

Demonstration i(h)=kh1/k1

Given the previous result we can think of the same tree with one more level, which means this last level only has leaves.

Tn=i(h)+f(h)=i=0h1ki+kh

To compute count of internal nodes we can use the geometric series

i=0h1ki<kii=0h1ki=kh1+11k1=kh1k1, k1

Demonstrate h(T)=logk(n)

We know that f(h)=kh for a complete k-ary tree

Then if the we have n=f(h) leaves, we find n=khh=logk(n)

Extra: f(T)=(n+1)/2 for a non-empty 2-ary complete tree with n nodes

Demonstration that, for a non-empty 2-ary complete tree with n nodes, the count of leaves is exactly n+12. In this case we only consider the second case: n>0. We start by using induction on the hypothesis f(Tn)=n+12

Induction

  1. Base Case: n=1f(Tn)=(1+1)/2=1
    1. Only the root is present, trivially true
  2. Inductive Hypothesis:
    1. We suppose that f(Tn)=n+12 is true at step n=h1f(Tn)=n(h1)+12
    2. We assume that k1k<n , the property is true.
  3. Inductive step: we need to demonstrate for any n>0
    1. We exclude n=0 by definition

For a non-empty complete binary tree f(Tk)=k+12 and the total count of nodes, can be written as

nodes(Tn)=nodes(Tleft)+nodes(Tright)+root=n12+n12+1

Since the two subtrees are complete and have less nodes than Tn, we can apply the inductive hypothesis

  • Trivially, f(Tleft)=((n1)/2)+12<n
  • Trivially, f(Tright)=((n1)/2)+12<n
  • Trivially f(Tleft)=f(Tright) and f(Tleft)+f(Tright)<n
f(Tn)=f(Tleft)+f(Tright)=2((n1)/2)+12=n1+22=n+12

Balanced Binary Tree

A tree is a balanced tree if its height is: h(T)=O(log(n))

Complete Binary Tree is balanced

A Complete Binary Tree is a Balanced Binary Tree (but the opposite is not always true)

Performances

It is important to keep binary tree's height as close to O(log(n)) when performing operations on it as it will improve the performances.

Binary Search Tree (BST)

Binary Search Trees or BST for short are a particular application of binary trees.

BST has at most two nodes (like all binary trees). However, the values are so that the left children value must be less than the parent, and the right children must be higher.

Duplicates: Some BST doesn’t allow duplicates while others add the same values as a right child. Other implementations might keep a count on a case of duplicity (we are going to do this one later).**