Everything About Trees (2024)

A tree is a type of graph data structure composed of nodes and edges. Its main properties are

  • It is acyclic (doesn't contain any cycles);
  • There exists a path from the root to any node;
  • Has N - 1 edges, where N is the number of nodes in the tree; and
  • Each node has exactly one parent node with the exception of the root node

Here are some examples of trees:

Everything About Trees (1)

Everything About Trees (2)

Everything About Trees (3)

Everything About Trees (4)

Everything About Trees (5)

Everything About Trees (6)

Everything About Trees (7)

PreviousNext

We have used terms above that you may be unfamiliar with. Here are there definitions and an example to accompany them:

Here's the summary:

  • Internal node: every node in a tree that has at least one child node.
  • Leaf node: every node in a tree that has no child nodes.
  • Ancestor: all the nodes that are between the path from the root to the current node are the ancestors of the current node. An ancestor node of the current node is either the parent of the current node or the parent of another ancestor of the node.
  • Descendent: all the nodes that are reachable from the current node when moving down the tree are the descendants of the current node. A descendant of the current node is either a child of the node or a child of another descendant of the node.
  • Level: the number of ancestors from the node to the root nodes.

Binary Tree

An n-ary tree is a tree in which each node has no more than n children. A binary tree is a type of n-nary tree with n = 2, so every node in a binary tree has 0 to 2 children.

Everything About Trees (18)

Binary Tree implementation in Python, Java, C++, Javascript and Go

1class Node:2 def __init__(self, val):3 self.val = val4 self.left = None5 self.right = None6
1class Node<T> {2 public T val;3 public Node<T> left;4 public Node<T> right;56 public Node(T val) {7 this(val, null, null);8 }910 public Node(T val, Node<T> left, Node<T> right) {11 this.val = val;12 this.left = left;13 this.right = right;14 }15}16
1template <typename T>2struct Node {3 T val;4 Node<T>* left;5 Node<T>* right;67 explicit Node(T val, Node<T>* left = nullptr, Node<T>* right = nullptr)8 : val{val}, left{left}, right{right} {}910 ~Node() {11 delete left;12 delete right;13 }14};15
1class Node {2 constructor(val, left = null, right = null) {3 this.val = val;4 this.left = left;5 this.right = right;6 }7}8
1class Node<T> 2{3 public T val;4 public Node<T> left;5 public Node<T> right;67 public Node(T val) 8 {9 this.val = val;10 }1112 public Node(T val, Node<T> left, Node<T> right) 13 {14 this.val = val;15 this.left = left;16 this.right = right;17 }18}19
1type Node struct {2 val int3 left *Node4 right *Node5}6

Full, Complete and Perfect Binary Trees

Everything About Trees (19)

Full binary tree

Every node has 0 or 2 children.

Complete binary tree

All levels are completely filled except possibly the last level and all nodes in the last level are as far left as possible.This may sound like an odd concept. We will see its usage in the heap section.

Perfect binary tree

All internals nodes have two children and all leaf nodes have the same level.Perfect binary trees are often used to estimate time complexity for combinatorial problems where the search space is a perfect binary tree.

A perfect binary tree has the following properties:

  • Number of nodes in a perfect binary tree is 2^n-1 where n is the number of levels.Calculation: The first level has 1 node, the root. The next level has 2 nodes. The following levels have 4, 8, 16.. nodes. This is a geometric sequence and the sum is a(1-r^n)/(1-r). Plug in a = 1 and r = 2 and we get 2^n-1.
  • Number of internal nodes = number of leaf nodes - 1.Calculation: A perfect binary tree of height n+1 will have 2^n leaf nodes. The internal nodes in the tree of height n+1 form a perfect binary tree of height n with 2^n-1 total nodes. Comparing the two values, we see that # of internal nodes = # leaf nodes - 1.
  • Total number of nodes = 2 * number of leaf nodes - 1. This is a derivative of property #2 and the fact that the total number of nodes = number of leaf nodes + number of internal nodes. So the number of total nodes and leaf nodes are both O(2^n)

Binary Search Tree

A binary search tree (BST) is a special type of binary tree, in which every nodes follows the ordering property of all left descendents < node < all right descendents. Learn more about BST in Binary Search Tree Intro.

Balanced Binary Tree

Every node in a balanced binary tree fullfills the condition--the height difference of the left and right subtree of the node is not more than 1. Searching, insertion, and deletion in a balanced binary tree takes O(log n) instead of O(n) in an unbalanced binary tree.This is an example of a balanced binary tree:

Everything About Trees (20)

Some common types of balanced binary trees are red-black trees and AVL trees. It's good to be aware of these trees but they are too complex to be asked in a coding interview.

Tree Traversal

In-order Traversal

In-order traversal visits the left branch first, then the current node, and finally the right branch. The diagram below shows the traversal order of an in-order traversal on a binary tree.

Everything About Trees (21)

Below is a demonstration of how you would perform in-order traversal on a binary tree using recursion

Pre-order Traversal

Pre-order traversal visits the current node first, then the left subtree, and finally the right subtree. The diagram below shows the traversal order of a pre-order traversal on a binary tree.

Everything About Trees (22)

Post-order Traversal

Post-order traversal visits the left subtree first, then the right subtree, and finally the current node. The diagram below shows the traversal order of a post-order traversal on a binary tree.

Everything About Trees (23)

How AlgoMonster encode trees

When you work through our lessons, you will encounter many problems involving trees. It is helpful to know how we encode trees, so you may be able to make up your own custom tree input to test your code.

How AlgoMonster encode binary trees

We represent a binary tree as a string. The values of each node are separated by an empty space, and null nodes are represented by "x". The function build_tree() in our driver code, which processes a given string into a binary tree, fills the tree with pre-order traversal.This example demonstrates how we represent and encode a binary tree input:

Everything About Trees (24)

The string representation of this tree is 5 4 3 x x 8 x x 6 x x, and this is how build_tree() builds a binary tree from the string input:

Everything About Trees (25)

Everything About Trees (26)

Everything About Trees (27)

Everything About Trees (28)

Everything About Trees (29)

Everything About Trees (30)

Everything About Trees (31)

Everything About Trees (32)

Everything About Trees (33)

Everything About Trees (34)

Everything About Trees (35)

PreviousNext

How AlgoMonster encode non-binary trees

For trees other than binary trees, we define the tree nodes as:

1class Node:2 def __init__(self, val, children=None):3 if children is None:4 children = []5 self.val = val6 self.children = children

Similarly, we also represent a non-binary tree as a string. The values of each node and the number of its children are separated by an empty space.The function build_tree() in our driver code, which processes a given string into a tree, also fills the tree with pre-order traversal.This example demonstrates how we represent and encode a non-binary tree input:

Everything About Trees (36)

The string representation of the tree shown above is 7 3 2 1 5 0 3 0 4 0, and this is how build_tree() builds a tree from the string input:

Everything About Trees (37)

Everything About Trees (38)

Everything About Trees (39)

Everything About Trees (40)

Everything About Trees (41)

PreviousNext

Still not clear? Ask in the Forum, DiscordorSubmitthe part you don't understand to our editors.

Everything About Trees (2024)
Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 6377

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.