Ad Block Detected!

Our website is made possible by displaying ads to our visitors. Please support us by whitelisting our website or allowing necessary trackers.

What is a tree

A graph is a dynamic data structure that can represent non-linear relationships betweens different items.

What does a tree consist of

Root node
Child Nodes
Parent Nodes
Edges

Trees

In a tree, a root node is the starting node, and edges are what connects the nodes together. The child nodes derive from the parent nodes. The number of edges in a tree is equal to the number of nodes - 1. E = N -1.

Illustration of a general tree data structure with nodes and edges

What is a Binary Tree

A binary tree is a dynamic data structure with nodes only being able to hold a maximum of two other nodes.

Binary Trees

Binary trees are built by connecting the smaller value of a parent node to the left and the larger value to the right. The example below shows a binary tree with the numbers [9, 7, 11, 6, 8, 5, 11, 10] inserted in this order. A quick explanation is that the number 6 is smaller than the number 9 (the root node) so it moves to the left. Because the number 6 is smaller than the number 7, it will be moved to the left and placed there. The number ten is greater than 9, so it goes to the right, but it is less than 11, so it goes to the left. The binary tree is built in the order of the given list.

Diagram of a binary tree data structure showing nodes with values and their left and right children