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

CS DataStructure-Lecture 7 - Introduction To Tree

Data strructure

Uploaded by

AsmaaGhoniem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CS DataStructure-Lecture 7 - Introduction To Tree

Data strructure

Uploaded by

AsmaaGhoniem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Data Structure

Dr. Ahmed Hesham Mostafa


Lecture 7 – Introduction to Tree
Online Martials
• CS214: Data Structures by Prof. Dr Waleed A. Yousef
• https://www.youtube.com/playlist?list=PLoK2Lr1miEm-
5zCzKE8siQezj9rvQlnca
• Data Structures Learning Course by Dr Mohammed El-Said
• https://www.youtube.com/playlist?list=PLfay0LLBd0wiNeOR_SGoYfC
3w-NxFwd0D
• Lectures Source code (updated frequently)
• https://github.com/ahmedheshamostafa/DataStructure
Tree – What?

• A tree is a collection of nodes.

• The collection can be empty.

• If not empty, a tree consists of:


✓ a node r (the root)
✓ zero or more nonempty subtrees T1, T2, ...., Tk, each of
whose subtrees are connected by an edge from r.
Tree
• Recursive Definition:
• A tree consists of a
root, and zero or more
subtrees T1, T2, … ,
Tk such that there is an
edge from the root of
the tree to the root of
each subtree.

Terminologies
• Node : an object containing a data value and children
➢Root : topmost node of a tree
➢Leaf : a node that has no child
➢Parent : a node that refers to this one.
➢Child : a node that this node refers to.
➢Sibling : a node with a common parent node.
Terminologies
• Path : a sequence of edges.
• Size : the number of nodes in a tree.
• Subtree : a smaller tree of nodes, which is one of the current node
children.
• Height : the number of edges on the longest path from the node to a
leaf.
• Depth : the number of edges from the node to the tree's root node.
• Level : length of the path from a root to a given node.
• Degree : the maximum number of subtrees.
Binary Trees
• A tree in which no node can have more than two children
(tree of degree 2)
• A binary tree is an empty tree, or is a root node that has
two children each of them is a binary tree
Left Right
successor successor
Generic
Examples binary tree
Never joined
Binary Tree

Left Skewed Right Skewed

• If a binary tree has only right sub trees, then it is


called right skewed binary tree.
• If a binary tree has only left sub trees, then it is called
left skewed binary tree.
Binary Tree
• There are different types of binary tree but here we are going
to discuss about the difference of Complete binary
tree and Full binary tree.
Full Binary Tree:
• full binary tree is a binary tree in which all of the nodes have
either 0 or 2 offspring.
• In other terms, a full binary tree is a binary tree in which all
nodes, except the leaf nodes, have two offspring.
Complete Binary Tree:
• A binary tree is said to be a complete binary tree if all its levels,
except possibly the last level, have the maximum number of possible
nodes, and all the nodes in the last level appear as far left as possible.
• There are 2 points that you can recognize from here,
1.The leftmost side of the leaf node must always be filled first.
2.It isn’t necessary for the last leaf node to have a right sibling.
Examples
• Neither complete nor full
• Node C has just one child therefore, it is not a Full binary
tree.
• Node C also has a right child but no left child, therefore it is
also not a Complete binary tree.
Examples
• Full but not complete
• All of the nodes have either 0 or 2 offspring, therefore, it is a
Full binary tree.
• It is not a Complete binary tree because node B has no
children whereas node C has children, and according to a
complete binary tree, nodes should be filled from the left side.
Examples
• Complete but not full
• it is a complete binary tree as all the nodes are left filled.
• Node B has just one child, therefore, it is not a full binary tree.
Examples
• Complete and full
• It is a Complete binary tree because all the nodes are left
filled.
• All of the nodes have either 0 or 2 offspring, therefore, it is
a full binary tree.
Binary Tree Traversals
• Traversal: An examination of the elements of a tree.
• Common orderings for traversals:
– pre-order: process root node, then its left/right subtrees
– in-order: process left subtree, then root node, then right
– post-order: process left/right subtrees, then root node
Inorder Traversal
1.Traverse the left subtree, i.e., call Inorder(left->subtree)
2.Visit the root.
3.Traverse the right subtree, i.e., call Inorder(right->subtree)
Preorder Traversal
1.Visit the root.
2.Traverse the left subtree, i.e., call Preorder(left->subtree)
3.Traverse the right subtree, i.e., call Preorder(right->subtree)
Postorder Traversal
1.Traverse the left subtree, i.e., call Postorder(left->subtree)
2.Traverse the right subtree, i.e., call Postorder(right->subtree)
3.Visit the root
Example
Traversal example
• in-order (LVR): Root

41 17
6
17 41 9
81
9 6 81

1.Traverse the left subtree, i.e., call Inorder(left->subtree)


2.Visit the root.
3.Traverse the right subtree, i.e., call Inorder(right->subtree)
Traversal example
• pre-order (VLR): Root

17 17
41
6 41 9
9
81 6 81

1.Visit the root.


2.Traverse the left subtree, i.e., call Preorder(left->subtree)
3.Traverse the right subtree, i.e., call Preorder(right->subtree)
Traversal example
• post-order (LRV): Root

6 17
41
81 41 9
9
17 6 81

1.Traverse the left subtree, i.e., call Postorder(left->subtree)


2.Traverse the right subtree, i.e., call Postorder(right->subtree)
3.Visit the root
Traversal example
– Pre-order: Root
42 15 27 48 9 86 12 5 3 39
42

– In-order:
15 9
15 48 27 42 86 5 12 9 3 39
27 86 3
– Post-order:
48 27 15 5 12 86 39 3 9 42 48 12 39

5
Example: Expression Trees
• It is a binary tree contains an arithmetic
expression with some operators and operands.

• Leaves are operands (constants or variables)


*
• The internal nodes contain operators a b

• For each node contains an operator, its left subtree a* b

gives the left operand, and its right subtree gives


the right operand.
Example: Expression Trees
• Building Expression Trees has great importance in
syntactical analysis and parsing, along with the validity
of expressions
Example: Expression Trees
(d * e + f ) *g -
b
-b

++
*
a
a b
a++
a* b
Example: Expression Trees
References
• Introduction to Tree - Data Structure and Algorithm Tutorials –
GeeksforGeeks
• Difference between Full and Complete Binary Tree – GeeksforGeeks
• Tree Traversals (Inorder, Preorder and Postorder) - GeeksforGeeks

You might also like