0% found this document useful (0 votes)
29 views52 pages

CHP 6 Tree

This document discusses trees and binary trees. It begins by defining tree terminology like root, parent, child, leaf, internal node, ancestor, descendant, subtree, depth, height, and path. It then explains different types of trees, including general trees and binary trees. Binary trees are the simplest form of tree and can only have two children per node. The document discusses types of binary trees like strictly binary trees, full binary trees, expression trees, and binary search trees. It provides examples of building expression trees from prefix and postfix notation. Finally, it discusses evaluating expression trees by replacing leaves with values and applying operators to evaluate subtrees.

Uploaded by

2022842434
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)
29 views52 pages

CHP 6 Tree

This document discusses trees and binary trees. It begins by defining tree terminology like root, parent, child, leaf, internal node, ancestor, descendant, subtree, depth, height, and path. It then explains different types of trees, including general trees and binary trees. Binary trees are the simplest form of tree and can only have two children per node. The document discusses types of binary trees like strictly binary trees, full binary trees, expression trees, and binary search trees. It provides examples of building expression trees from prefix and postfix notation. Finally, it discusses evaluating expression trees by replacing leaves with values and applying operators to evaluate subtrees.

Uploaded by

2022842434
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/ 52

CHAPTER 6

TREE
FUNDAMENTALS OF DATA STRUCTURES (CSC248)

PREPARED BY:
MISS MAZNIE MANAF
Contents
 Basic tree concept
 Types of tree
 Expression tree
 Binary search tree (BST) concept
 Implementation of BST
 Application of BST
Chapter Objective
 To understand the concept of tree
 To understand the concept of binary tree
 To traverse tree using
 preOrder traversal
 inOrder traversal
 postOrder traversal
 Application applied with binary Tree
Trees
 Previous data organizations place data in linear order
e.g.: list, stack, or queue
 Some data organizations require categorizing data into
groups, subgroups
 This is hierarchical classification
 Data items appear at various levels within the organization
 Trees or binary trees are a hierarchical data structure
Hierarchical Organization
 Example: File directories

Fig.1 Computer files organized into folders.


Hierarchical Organization
 Example: A university's organization

Fig. 2 A university's administrative structure.


Hierarchical Organization
 Example: Family trees

Fig. 3 Carole's children and grandchildren.


Tree Terminology
 A tree is
 A set of nodes
 Linked by edges/branch
 The edges/branch indicate relationships among nodes
 Nodes arranged in levels 4
 Indicate the nodes' hierarchy
nodes
 Top level is a single node called
the root 2 5
edge
1 3
Tree Terminology
Tree Terminology
 Parent / Child
 Parent: a node that has one or more nodes connected below it
(children)
 Child: a node that has a connected node above it (parent)
 A parent node references one or more nodes (children nodes) that are
“lower” in the tree hierarchy
 If node u is the parent of node v, u root
then v is the child of u
 Except for the root (no parent), v
every node has exactly one parent
(by definition)
 A tree has only one root node
Tree Terminology
 Siblings
 Two nodes that are children of the same parent.
Tree Terminology
 Leaf (External node)
 A node is a leaf if it has no children
Tree Terminology
 Internal node
 A node is internal if it has one or more children
Tree Terminology
 Ancestor / Descendent
 A node that is in the same path as a node but that is located
closer to the root is
called an ancestor of
the node.
 The node itself is a
descendant of its
ancestor node.
Tree Terminology
 Subtree
 A parent and all the nodes below it

Root of subtree

v
v
Tree Terminology
if x is the root element
 Depth of a node
level(x) = 0
 The depth of a node v is the number of else

branches from the root to v (A branch is level(x) = 1 + level(parent(x))

a line drawn from an element to its left


or right subtree)
 More formally: v
 If v is the root, the depth of v is 0
 Otherwise, the depth of v is one v
plus the depth of the parent of v

depth of v = 1 depth of v = 3
Tree Terminology tree depth = 0

 Height (or depth) of a tree


 The depth of a tree is the maximum
depth of any of its leaves; or
 The height of a binary tree is the number
of branches from the root to the farthest
leaf tree depth = 2

if t is empty
height(t) = -1
else
height(t) = 1 + max of [height(leftTree(t),
height(rightTree)]

tree depth = 3
Tree Terminology
 Path
 As sequence of element
Level

branch
1

Height of the tree = 3


Path between 2 to 5 = 2 7 6 5 2

leaf Example
Example 1
 Descriptions :
A Level 0

- has 9 nodes
- node B is the left subtree of node A B C Level 1

- node C is the right subtree of node A


D E Level 2
- right subtree of node B and E is null F

(eg : no right subtree for node E)


- node D, G, H, I is a leaf (has null left G
H I Level 3
and right subtree)
- the depth of Tree ABC is 3 (max level,
where first level starts with 0)
Types of Trees
 General tree – a node can  Binary tree – a node can
have any number of have at most two children
children
General Trees
 Nodes of a general tree can have any number of subtrees
 A general tree can be represented using a binary tree
BINARY TREE
Binary Trees
 The simplest form of tree is a Binary Tree
Binary tree
 A Binary Tree consists of
 (a) A node (called the root node) and
 (b) Left and right subtrees
 Both the subtrees are themselves binary trees
 Note: this is a recursive definition
 (A node can’t have more than 2 children)
General tree
Types of Binary Tree
 Strictly Binary Tree (SBT)
 Each node that’s not a leaf has both left
and right subtree

 Full Binary Tree (FBT)


 A tree with leaves only at the last
level
Some Types of Binary Tree
 Expression tree
 Each node contains an operator or an operand
 Huffman tree
 Represents Huffman codes for characters that might appear in a
text file
 Huffman code uses different numbers of bits to encode letters as
opposed to ASCII or Unicode
 Binary search trees
 All elements in the left subtree precede those in the right subtree.
Will be explain in detail in the following chapter.
Some Types of Binary Trees (continued)
Fullness and Completeness
 Trees grow from the top down
 Each new value is inserted in a new leaf node
 A binary tree is full if every node has two children except
for the leaves
APPLICATION OF BINARY TREE
 Expression Tree
 We can use Binary Tree to represent arithmetic expression (infix,
prefix and pstfix)
 Suppose we have the following expression:
3+7x2-1
 The expression can be represented in fully parenthesized form as:
{[3 + (7 x 2)] – 1}
 To illustrate the expression, choose a pair of leaves, apply the
operator at their root and replace the subtree with the result of
the calculation
Expression Tree for 3 + 7 x 2 - 1

+ 1

3 x

7 2
Building Expression Tree
 Using prefix expression Infix => (A + B) * C * ( D - E)

Evaluate prefix expression from LEFT to prefix => * * + A B C – D E


RIGHT. Left Right
Any operator must be at the ROOT, and *
any operand must be at the leaf
Always open the LEFT branch first until * -
no more branch can be opened. Then open
the RIGHT branch. + D E
C

A B
Building Expression Tree
 Using postfix expression Infix => (A + B) * C * ( D - E)

Evaluate postfix expression from RIGHT to postfix => A B + C * D E - *


LEFT. Left Right
Any operator must be at the ROOT, and *
any operand must be at the leaf
Always open the RIGHT branch first until * -
no more branch can be opened. Then open
the LEFT branch. + D E
C

A B
Exercise 1
 Draw the tree using given expression
1. Prefix => + A * - B C $ D * E F
2. Postfix => A B C – D E F * $ * +
Tree Evaluation
 Evaluating Expression Tree

 Steps :
Replace leaf (operand or variable) with the given value

Start with LEFT leaf


then OPERATOR and
RIGHT for every sub tree

Evaluate the expression tree using the arithmetic process.


Tree Evaluation
 Example (a) : if A= B =C=D=E=F=2 then RESULT is 144
144
$

12 F 2
-

16 $ $ 4

4 + 2 C
D 2 C 2

A 2 B 2
Tree Evaluation
 Example (b) : if A= 5, B = 4, C=3, D=2, E=1 then RESULT is -19

-19
-

A 5 * 24

6
B 4 *

C 3 $ 2

D 2 E 1
Exercise 2
 Given the following arithmetic expression:
Y = (T * S) + ( ( ( U $ N ) * ( A / M ) ) – I)

 Draw the expression tree for the above arithmetic expression


 Using the above tree, evaluate the expression Y if T = 4, S = 2, U =
3, N = 2, A = 4, M = 2, I = 5
Tree Traversal
 The process of accessing/visiting the elements of a tree, called
a tree traversal
 Types of traversal
 In-order (left, root, right)
 Pre-order (root, left, right)
 Post-order (left, right, root)

 The three traversals are nearly identical – the only difference is


when the root is visited
In-order Traversal
 For each tree or subtree – traversal starts
 from LEFT node,
 the ROOT and then
 the RIGHT node

 Algorithm
If the tree is not empty
inOrderTraverse(leftchild)
visit the root
inOrderTraverse(rightchild)

 For an expression tree, inOrder traversal produce infix notation


Self-Check
1

2 3

4 5 6 7

8 9 10 11 12

Show the order in which elements would be processed for the


inOrder traversal
The output: 8 4 9 2 5 10 1 11 6 12 3 7
Pre-order Traversal
 For each tree or subtree – traversal starts
 from ROOT node,
 the LEFT and then
 the RIGHT node

 Algorithm
If the tree is not empty
visit the root
preOrderTraverse(leftchild)
preOrderTraverse(rightchild)

 For an expression tree, preOrder traversal produce prefix notation


Self-Check
1

2 3

4 5 6 7

8 9 10 11 12

Show the order in which elements would be processed for the


preOrder traversal

The output: 1 2 4 8 9 5 10 3 6 11 12 7
POST-ORDER TRAVERSAL
 For each tree or subtree – traversal starts
 from LEFT node,
 the RIGHT and then
 the ROOT node

 Algorithm:
If the tree is not empty
postOrderTraverse(leftchild)
postOrderTraversal(rightchild)
visit the root

 For an expression tree, postOrder traversal produce postfix notation


Self-Check
1

2 3

4 5 6 7

8 9 10 11 12

Show the order in which elements would be processed for the


postOrder traversal

The output: 8 9 4 10 5 2 11 12 6 7 3 1
Example
Example (a) : A

B C

D E F G

Inorder – D H B E A F C G
Preorder – A B D H E C F G
Postorder – H D E B F G C A
Example
Example (b) : P

Q R

W
X

Inorder – Y W Q X P R
Preorder – P Q W Y X R
Postorder – Y W X Q R P
Summary of Tree Traversals
 Preorder: Visit root node, traverse TL, traverse TR
 Inorder: Traverse TL, visit root node, traverse TR
 Postorder: Traverse TL, Traverse TR, visit root node
Exercise 3
 Based on the diagram below, state the visited valued if
these traversal method used with the given binary tree:
 inOrder traversal
80
 postOrder traversal
 preOrder traversal
58 87

10 28 48

25 62 75
Exercise 4
-

/ -

* + * 8

+ 3 - 2 5 -

4 5 8 7 7 5
 Based on the diagram, state the visited valued if these traversal method
used with the given binary tree:
 inOrder traversal
 postOrder traversal
 preOrder traversal
Exercise 5
Given the product of a tree traversal as follows:

Preorder traversal 80 58 10 25 28 87 48 62 75
Inorder traversal 10 25 58 28 80 62 48 75 87

- Base on the preorder and inorder traversal above draw


the tree.
Exercise 6
Given the following arithmetic expression.

Y= + * $ T R A + - V / E L * * H A J

a) Draw the expression trees of Y.


b) Traverse the trees and give the infix and postfix expressions.
c) Determine the value of A if the value arithmetic expression Y
= 25 and the value of the operands are equal to 2.
d) What is the depth of the expression tree that is produced in
(a)
Summary
 Tree
 Tree treminology
 Type of Tree
 General Tree
 Binary tree
 Binary Tree
 Type of Binary tree
 Strictly Binary Tree
 Full Binary Tree
 Expression tree
 Huffman Tree
 Binary Search Tree
 Application of Binary Tree
 Expression tree
 Tree traversal
 preOrder
 inOrder
 postOrder
End

You might also like