Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture 3 Trees in Data Structure

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Trees in Data

Structure
Lecture 2
Introduction
• Tree is a nonlinear hierarchical data structure, comprising
of a collection of entities known as nodes.
• It connects each node in the tree data structure using
"edges”, both directed and undirected.
Why tree in Data Structures
• The non-linear structure of trees enhances the data
storing, data accessing, and manipulation processes by
employing advanced control methods traversing through
it.
• Unlike arrays, linked-list, stacks, and queues with linear
data structures (data stored in sequential order), the time
complexity increases with increasing data size to perform
operations like insertion and deletion on these linear data
structures.
Terminologies used in Tree Data
Structure
• Tree Node: A node is a structure that contains a key or value
and pointers in its child node in the tree data structure.
• Syntax for defining tree node
• struct node
• {
• int data;
• struct node *leftchild;
• struct node *rightchild;
• }
Terminologies Cont’d
• Root is the first node of the tree or the initial node of the
tree in data structures.
• Edge: The connecting link of any two nodes.
• Parent is the predecessor of any node or a node with a
branch from itself to any other successive node.
• Child is a descendant of any node.
• Note: any number of parent nodes can have any number
of child nodes
Terminologies Cont’d
• Sibling: A nodes that belong to the same parent
• Leaf: A node with no child (known as external or terminal
nodes).
• Internal Nodes: Have at least one child node (nodes other
than leaf nodes are internal nodes).
• Degree: the total number of children of a node.
Level

• The root node is said to be at level 0, and the root node's


children are at level 1, and the children of that node at
level 1 will be level 2, and so on.
Height
• The number of edges from the leaf node to a particular
node in the longest path is known as the height of that
node.
• The height of the root node is the "Height of Tree“.
• The tree height of all leaf nodes is 0.
Depth
• Many edges from the root node to a particular node.
• The total number of edges from the root node to the leaf
node in the longest path is known as "Depth of Tree".
• The depth of the root node is 0.
Path
• The sequence of nodes and edges from one node to
another node (path between two nodes).
• The length of a path is the total number of nodes in a
path.
Subtree
• Each child from a node shapes a sub-tree recursively and
every child in the tree will form a sub-tree on its parent
node.
Type of Tree in Data Structure
• General tree: type of tree where there are no constraints
on the hierarchical structure.
• Property: A node can have any number of nodes.
Binary Tree
Properties
• Follows all properties of the tree data structure.
• Can have at most two child nodes.
• These two children are called the left child and the right
child.
Binary Search Tree
• A type of tree that is a more constricted extension of a
binary tree data structure.
• Follows all properties of the tree data structure.
• Has a unique property “the binary search property”.
• that the value of a left child node of the tree should be less than or
equal to the parent node value of the tree. And the value of the
right child node should be greater than or equal to the parent
value.
Binary Search Tree Illustration
Reading Assignment
• AVL
• Red-Black
• Treap Tree
Tree Traversal
• Traversal of the tree in data structures is a process of
visiting each node and prints their value.
• Three ways to traverse tree data structure.
1. Pre-order Traversal
2. In-Order Traversal
3. Post-order Traversal
In-Order Traversal
• In the in-order traversal, the left subtree is visited first,
then the root, and later the right subtree.
• Algorithm:
• Step 1- Recursively traverse the left subtree
• Step 2- Visit root node
• Step 3- Recursively traverse right subtree
In-order Traversal
• Visit the left subtree, then the root and later the
• right sub-tree.
• Remember every node may represent a subtree itself.
• If a binary tree is traversed in-order,
• the output will produce sorted key values in an
• ascending order.
• The output of in-order traversal of this tree will be
• D→B→E→A→F→C→G
Pre-Order Traversal
• In pre-order traversal, it visits the root node first, then the
left subtree, and lastly right subtree.
• Algorithm:
• Step 1- Visit root node
• Step 2- Recursively traverse the left subtree
• Step 3- Recursively traverse right subtree
Pre-Order Traversal
• Visit A itself and then move to its left subtree B.
• B is also traversed pre-order.
• Also, traverse right subtree pre-order.
• The output of pre-order traversal of this tree
will be A → B → D → E → C → F → G
Post-Order Traversal
• It visits the left subtree first in post-order traversal, then
the right subtree, and finally the root node.
• Algorithm:
• Step 1- Recursively traverse the left subtree
• Step 2- Recursively traverse right subtree
• Step 3- Visit root node
Post-order Traversal
• First, traverse the left subtree, then the right subtree and
finally the root node.
• Visit the left subtree B. B is also traversed
• post-order. Traverse right subtree post-order
• The output of post-order traversal of this tree will be
• D→E→B→F→G→C→A
Level-Order Traversal
• Level-order: process nodes left-to-right on each level in
turn, from the top downwards
• This is a breadth-first traversal: process an entire level of
the tree at the same time
Example 1
Level-order: L-to-R, level by level. F, B, G, A, D,
I, C, E, H
Pre-order: Parent, then repeat L-to-R on
children.
F, B, A, D, C, E, G, I, H
Post-order: L-to-R on children, then parent.
A, C, E, D, B, H, I, G, F
In-order: Left subtree, then root, then right
subtree. A, B, C,
Note: Pre-order, D, E, F, G,
post-order, H,in-order
and I are all depth-
first traversals
(Go as deep as possible in each branch before moving
to the next one)
Traversal Implementation
• Consider a BT below. Suppose you are to destroy the BT,
what traversal method would you use? And why?
• Answer: We use post-order
• Reason: We do not want to destroy the
parent node and its pointers
until the sub-trees below it are
safely destroyed
Expression Tree
• An expression tree is a binary tree corresponding
to some mathematical expression.
• Each internal node is an arithmetical operator
• Each leaf is a value or a declared variable
• The expression corresponding to the tree can be
evaluated by traversing the elements and
performing computation as the elements are
traversed
Example 1
• We can traverse the tree to print or evaluate the represented expression
• In-order == infix: 3 + 1 * 4 / 2 * 5 – 5
• (3 + 1) * (((4 / 2) * 5) – 5)
• Pre-order == prefix:
• *+31-*/4255
• Post-order == postfix (RPN-Reverse Polish Notation):
• 31+42/5*5-*
• Level-order possible as well, but does not match any
• standard operator notation order: * + - 3 1 * 5 / 5 4 2
Example 2
i. Represent 3 + ((5+9)*2) using expression tree
ii. Find the in-order, pre-order and post-order
representation
from the tree expression

You might also like