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

COMP6458 - Struktur Data Pert5 - Introduction To Tree - Binary Tree and Expression Tree

This document discusses trees and binary trees, including their concepts, properties, representations, and expression trees. Key points covered include defining tree and binary tree structures, the different types of binary trees, and algorithms for constructing and traversing expression trees from prefix, postfix, and infix notations. Traversals of expression trees must handle operator precedence for accurate infix conversion.

Uploaded by

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

COMP6458 - Struktur Data Pert5 - Introduction To Tree - Binary Tree and Expression Tree

This document discusses trees and binary trees, including their concepts, properties, representations, and expression trees. Key points covered include defining tree and binary tree structures, the different types of binary trees, and algorithms for constructing and traversing expression trees from prefix, postfix, and infix notations. Traversals of expression trees must handle operator precedence for accurate infix conversion.

Uploaded by

Alfath Fachrezy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Course : COMP6458 – DATA STRUCTURES

Year : 2016

Introduction to Tree,
Binary Tree And Expression Tree
Learning Outcomes

At the end of this session, student will be able to:


• Define tree and binary tree concept (LO1 & LO3)

2
Sub Topics

Tree & Binary Tree:


- Tree Concept
- Binary Tree Concept
- Type of Binary Tree
- Property of Binary Tree
- Representation of Binary Tree
- Expression Tree Concept
- Create Expression Tree from Prefix, Postfix and Infix
- Prefix, Postfix and Infix Traversal

3
Tree Concept

Tree is a collection of one or more nodes.

DEGREE of TREE = 3
LEVEL 1 A DEGREE of C = 2
HEIGHT = 3
LEVEL 2 B C D PARENT of C = A
CHILDREN of A = B, C, D
SIBILING of F = G
LEVEL 3 E F G ANCESTOR of F = A, C
DESCENDANT of C = F, G
TREE

4
Tree Concept

• Node at the top is called as root.


• A line connecting the parent to the child is edge.
• Nodes that do not have children are called leaf.
• Nodes that have the same parent are called sibling.
• Degree of node is the total sub tree of the node.
• Height/Depth is the maximum degree of nodes in a tree.
• If there is a line that connects p to q, then p is called the
ancestor of q, and q is a descendant of p.

5
Binary Tree Concept

• Binary tree is a rooted tree data structure in which each


node has at most two children.

• Those two children usually distinguished as left child and


right child.

• Node which doesn’t have any child is called leaf.

6
Binary Tree Concept

A sample of binary tree


of 9 nodes, rooted on
node which contains 18.

Leaves are nodes which


contain 9, 12, 10 and 23.

7
Type of Binary Tree

• PERFECT binary tree is a binary tree in which every level are at


the same depth.

• COMPLETE binary tree is a binary tree in which every level, except


possibly the last, is completely filled, and all nodes are as far left as
possible. A perfect binary tree is a complete binary tree.

• SKEWED binary tree is a binary tree in which each node has at


most one child.

• BALANCED binary tree is a binary tree in which no leaf is much


farther away from the root than any other leaf (different balancing
scheme allows different definitions of “much farther”).

8
PERFECT Binary Tree

9
COMPLETE Binary Tree

A perfect binary tree is also a complete binary tree.

10
SKEWED Binary Tree

11
Property of Binary Tree

Maximum number of nodes on level k of a binary tree is 2k.

In some
literatures,
level of binary
tree
starts with 1
(root).

12
Property of Binary Tree

Maximum number of nodes on a binary tree of height h is


2h+1 - 1.

Maximum nodes
of a binary tree of
height 3
=1+2+4+8
= 20 + 21 + 22 + 23
= 24 – 1
= 15

13
Property of Binary Tree

Minimum height of a binary tree of n nodes is 2log(n).


Maximum height of a binary tree of n nodes is n - 1.

Skewed binary trees


have maximum height

14
Representation of Binary Tree

• Implementation using array

Index on array represents node number


Index 0 is Root node
Index Left Child is 2p + 1, where p is parent index
Index Right Child is 2p + 2
Index Parent is (p-1)/2 15
Representation of Binary Tree

• Implementation using linked list

struct node {
int data;
struct node *left;
struct node *right;
struct node *parent;
};

struct node *root = NULL;

16
Expression Tree Concept

• Recall our discussion on stack application about


arithmetic notation (session 6).

• Here we will discuss such arithmetic notation using


expression tree.

17
Expression Tree Concept

Prefix : *+ab/-cde
Postfix : ab+cd-e/*
Infix : (a+b)*((c-d)/e)

18
Expression Tree Concept

We will use this structure for each node in the tree:

struct tnode {
char chr;
struct tnode *left;
struct tnode *right;
};

It is a binary tree.

19
Create Expression Tree from Prefix

We can create an expression tree from a prefix by recursive.

main function
char s[MAXN]; struct tnode *root = newnode(s[0]);
int p = 0; f(root);

void f(struct tnode *curr) {


if ( is_operator(s[p]) ) {
p++; curr->left = newnode(s[p]);
f(curr->left);
p++; curr->right = newnode(s[p]);
f(curr->right);
}
}

20
Prefix Traversal

Doing a prefix or postfix traversal in an expression tree is


simple.

void prefix(struct tnode *curr) {


printf( “%c “, curr->chr );
if ( curr->left != 0 ) prefix(curr->left);
if ( curr->right != 0 ) prefix(curr->right);
}
In prefix, you have to print/process before its child are
processed.

21
Postfix Traversal

void postfix(struct tnode *curr) {


if ( curr->left != 0 ) postfix(curr->left);
if ( curr->right != 0 ) postfix(curr->right);
printf( “%c“, curr->chr );
}
In postfix, you have to print/process after its child have
been processed.

22
Infix Traversal

How about infix? Can we just do like this code below?

void infix(struct tnode *curr) {


if ( curr->left != 0 ) infix(curr->left);
printf( “%c“, curr->chr );
if ( curr->right != 0 ) infix(curr->right);
}

23
Infix Traversal

It’s seems right, but infix may have operator precedence ambiguity
without brackets.

For example * + a b c in prefix will be encoded in infix as a + b *


c with the previous code, while the correct infix is (a + b) * c.

a + b * c : b is multiplied by c and then added by a


(a + b) * c : a is added by b and then multiplied by c

24
Infix Traversal
Prefix : *+abc
Postfix : ab+c*
Wrong infix : a+b*c
Correct infix : (a+b)*c

25
Infix Traversal
To fix that, we can add brackets () when expanding an operator.
void infix(tnode *curr) {
if ( is_operator(curr->chr) ) putchar( '(' );
if ( curr->left != 0 ) infix(curr->left);
printf( "%c", curr->chr );
if ( curr->right != 0 ) infix(curr->right);
if ( is_operator(curr->chr) ) putchar( ')' );
}

So * + a b c in prefix will be encoded as ((a + b) * c) in


infix, which is correct.

26
Create Exp-Tree from Postfix
Now, can you create an expression tree given a postfix
notation?

Hint: scan from right to left.

27
Summary

• Tree is a collection of one or more nodes.


• Binary tree is a rooted tree data structure in which each node
has at most two children.
• 4 types of Binary Tree:
– PERFECT binary tree
– COMPLETE binary tree
– SKEWED binary tree
– BALANCED binary tree

28
Summary

• We can create an expression tree from a prefix or postfix


by recursive
• In prefix, you have to print/process before its child are
processed.
• In postfix, you have to print/process after its child have
been processed.

29
References

• Reema Thareja,. 2014. Data structures using C. OXFOR. New Delhi.


ISBN:978-0-19-809930-7 Chapter 9 & 10

30
END
Introduction to Tree, Binary Tree and Expression Tree

31

You might also like