0% found this document useful (0 votes)
27 views17 pages

Module 11 TREES

Trees are nonlinear hierarchical data structures consisting of nodes connected by edges. A tree traversal algorithm visits each node in a tree in a specified order. There are three main tree traversal methods: inorder (left, root, right), preorder (root, left, right), and postorder (left, right, root). Tree traversal can be done depth-first or breadth-first. Depth-first prioritizes going deep in one branch before exploring siblings, while breadth-first explores all nodes on the current level before moving to the next level down.
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)
27 views17 pages

Module 11 TREES

Trees are nonlinear hierarchical data structures consisting of nodes connected by edges. A tree traversal algorithm visits each node in a tree in a specified order. There are three main tree traversal methods: inorder (left, root, right), preorder (root, left, right), and postorder (left, right, root). Tree traversal can be done depth-first or breadth-first. Depth-first prioritizes going deep in one branch before exploring siblings, while breadth-first explores all nodes on the current level before moving to the next level down.
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/ 17

IT122

T REES

Joie Ann M. Mac


Objectives:
• To learn about Trees
• To learn about different terminologies of Trees
• To learn about Tree Traversals
What is a Tree ?
• A tree is a nonlinear hierarchical data structure that consists
of nodes connected by edges.
Tree Terminologies

• A node is an entity that contains a key or value and pointers


to its child nodes.
• The last nodes of each path are called leaf nodes or external
nodes that do not contain a link/pointer to child nodes.
• The node having at least a child node is called an internal node.
• An edge is the link between any two nodes.
Tree Terminologies
Tree Terminologies

• Root is the topmost node of a tree.


• The height of a node is the number of
edges from the node to the deepest
leaf (ie. the longest path from the
node to a leaf node).
• The depth of a node is the number of
edges from the root to the node.
• The height of a Tree is the height of
the root node or the depth of the
deepest node.
Tree Terminologies

• Degree of a Node
The degree of a node is the total
number of branches of that
node.

• Forest
A collection of disjoint trees is
called a forest.
Types of Tree

1.Binary Tree
2.Binary Search Tree
3.AVL Tree
4.B-Tree
Tree Traversal
• In order to perform any operation on a tree, you need to reach
to the specific node. The tree traversal algorithm helps in
visiting a required node in the tree.
• Tree traversing in Python refers to the process of visiting each
node in a data structure like a tree. Traversal algorithms tell us
the order in which the nodes of a tree are visited.
• Now, there are 3 main methods for Tree Traversal in Python
with recursion using DFS which are:
1. In order Traversal (left, root, right)
2.Preorder Traversal (root, left, right)
3.Postorder Traversal (left, right, root)
Inorder Tree Traversal
• Using the in-order traversal method, we first visit
the left subtree of the original tree. Then we will
traverse the root node of the tree and lastly the
right subtree of the original tree. Here is the
algorithm for Inorder Tree Traversal in Python:
1.Calling Inorder (left-subtree)
2.Visit the root node
3.Calling Inorder (right subtree)
Inorder Tree Traversal (left,
root, right)
Preorder Tree Traversal

• Using the preorder traversal method, we first visit the


root of the original tree. Then we will traverse the left
subtree of the original tree and lastly the right subtree
of the original tree. Below is the Python code for
Preorder Tree Traversal with recursion:
1. Visit the root node
2.Calling preorder (left-subtree)
3.Calling preorder (right subtree)
Preorder Tree Traversal (root,
left, right)
Postorder Tree Traversal

• Using the postorder tree traversal method we first visit the


left subtree of the original tree followed by the right
subtree and lastly the root node of the original tree. Below
is the Python code for Postorder Tree Traversal with
recursion:
1. Calling left-subtree
2.Calling right-subtree
3.Visit root node

Postorder Tree Traversal (left,
right, root)
What are the 2 Types of Tree
Traversal?
• Depth-First Traversal: Beginning at the root of a tree and proceeding as far down
as feasible before turning around is known as a "depth -first traversal," which
requires stopping at every node along the way. Pre-order traversal, in-order
traversal, and post-order traversal are the three most popular techniques for
depth-first navigation. We visit the current node, the left subtree, and the right
subtree in the pre-order traversal. We visit the left subtree, the current node, and
the right subtree in an in-order traversal. The left subtree, followed by the right
subtree, and finally the present node are all visited during post-order traversal. You
can do depth-first traversal iteratively or recursively. Although it is typically easier
to build, recursive traversal can be less effective for very deep trees.
• Breadth-First Traversal: In a breadth-first traversal, each node in a tree is visited
one level at a time, working down from the root. Before going to the next level, we
visit every node at the previous level. A queue is frequently used to keep track of
the nodes we need to visit during breadth-first traversal. The root node is first
added to the queue, after which the next node in the queue is repeatedly
dequeued, visited, and re-enqueued with its children. This procedure is carried out
until the queue is empty.
Time Complexity

• In tree traversal in Python using recursion, the time


complexity is O(n) where there are n nodes in the
tree. While the space complexity is also O(n) for n
nodes present in an answer array.

You might also like