Trees
Trees
that makes it easy to navigate and search through that data, it is essentially a collection of nodes
which are connected by edges and they have a hierarchical relationship between the nodes
The first node (topmost node) in the tree is referred to as the root, and nodes that arise from that
node are referred to as child nodes. Each node can have multiple child nodes and those child nodes
can also have multiple child nodes, this continues to form a recursive structure
Terminologies
Binary Tree:
This is a tree where each node can have a maximum of two children. Common types of binary trees
are Full binary trees and Complete binary trees
➢ A Full Binary Tree is a binary tree where each node either has zero or two child nodes
➢ A Complete Binary Tree is a binary tree where all levels of the tree are filled completely
(with the exception of the lowest level) where the nodes are filled as far left as possible
Only the nodes in the last level can only have one A node cannot have just one child node
child node
The nodes should be filled from left to right There is no order to filling the nodes
All leaf nodes must be at the exact same depth Leaf nodes do not need to be at the same depth
They represent the relationship between data and that data can be represented in the form of a
hierarchical. Binary trees provide effective searching, insertion and deletion of data
We also have Binary Search Trees, which is a special kind of binary tree in which the values of the
nodes increase from left to right, this property needs to be kept through all operations. So whether
we are inserting to or deleting from this type of tree, the property that the values remain increasing
from left to right must still be true. [We will take a look at Binary Search Trees as well as insertion
and deletion operations in more depth when we take a look at AVL Trees]
➢ Depth-First Search (DFS) – this is a traversal method which starts at the root node and
traverses as far as possible along each branch (edge) depth-wise before backtracking. Its
called Depth-First Search because it goes as deep as it can before exploring siblings or
parent nodes.
➢ Breadth-First Search (BFS) – this is a traversal method that starts at the root node and
traverses all neighbouring nodes at the present depth before moving onto nodes at the next
depth level. Its called Breadth-First Search because it explores the breadth of a tree before
going deeper
➢ Pre-order traversal
➢ In-order traversal
➢ Post-order traversal
Pre-order traversal: Here, the traversal route is Root-Left-Right, which means the root node is
visited first, then the left subtree and finally the right subtree
In-order traversal: Here, the traversal route is Left-Root-Right, which means the left subtree is
visited first, then the root node and finally the right subtree
Post-order traversal: Here, the traversal route is Left-Right-Root, which means the left subtree is
visited first, then the right subtree and finally the root node
➢ Level-order traversal
Level-order traversal: Here we visit nodes level-by-level and left-to-right at each level