0% found this document useful (0 votes)
4 views

Trees

The document explains the tree data structure, highlighting its hierarchical nature with nodes and edges, and defines key terminologies such as root, child, leaf, and height. It also discusses binary trees, their types (full and complete), and the importance of binary trees for efficient data operations. Additionally, it covers tree traversal methods, including Depth-First Search (DFS) and Breadth-First Search (BFS), along with their specific traversal techniques.

Uploaded by

kgaumogoje351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Trees

The document explains the tree data structure, highlighting its hierarchical nature with nodes and edges, and defines key terminologies such as root, child, leaf, and height. It also discusses binary trees, their types (full and complete), and the importance of binary trees for efficient data operations. Additionally, it covers tree traversal methods, including Depth-First Search (DFS) and Breadth-First Search (BFS), along with their specific traversal techniques.

Uploaded by

kgaumogoje351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

A tree data structure is a hierarchical structure that we use to represent and organised data in a way

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

Parent A node that produces another node


Child A node that is produced by another node
Root The topmost node of a tree
Leaf Nodes at the bottom of the tree, they cannot have
children
Siblings Children of the same parent node
Edge The link between two nodes
Level (Depth) of a node The amount of edges on the path taken from the
root node to that particular (distance)
Subtree An isolated node of a tree along with its
descendants
Descendant A successor node of any particular nodes
Ancestor Predecessor nodes of a particular node
Height of a tree The distance from the root node to the leaf nodes
Degree of a node The number of children a node has

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

Complete binary tree Full binary tree

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

Why use a binary tree data structure?

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]

Let us take a look at Binary Tree traversals

Tree traversals can be classified into two categories:

➢ 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

DFS can be further classified into 3 categories:

➢ 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

BFS can be further classified into one category:

➢ Level-order traversal

Level-order traversal: Here we visit nodes level-by-level and left-to-right at each level

You might also like