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

Lecture 13 Tree

The document outlines the properties and types of trees in computer science, including definitions of full and complete binary trees, as well as traversal algorithms such as preorder, inorder, and postorder. It also explains how to represent and evaluate arithmetic expressions using ordered rooted trees and different notations like infix, prefix, and postfix. Examples are provided to illustrate these concepts and their applications.

Uploaded by

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

Lecture 13 Tree

The document outlines the properties and types of trees in computer science, including definitions of full and complete binary trees, as well as traversal algorithms such as preorder, inorder, and postorder. It also explains how to represent and evaluate arithmetic expressions using ordered rooted trees and different notations like infix, prefix, and postfix. Examples are provided to illustrate these concepts and their applications.

Uploaded by

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

Properties of Trees

• A tree with n vertices has n1 edges.


• An full m-ary tree with i internal vertices
contains n = mi + 1 vertices.
• A rooted m-ary tree of height h is called
balanced if all leaves are at levels h or h–1.
Example

Is this tree balanced?


Example

Is this tree balanced?


Example

Is this tree balanced?


Full and Complete Binary Trees

A full binary tree is a binary tree in which


each node is either a leaf node or has degree 2
(i.e., has exactly 2 children).

A complete binary tree is a full binary tree


in which all leaves have the same depth.
Examples

Full binary tree: Complete binary tree:


Binary Search Trees
Section 10.3
Tree Traversal
Traversal Algorithms

•A traversal algorithm is a procedure for


systematically visiting every vertex of an
ordered rooted tree
• An ordered rooted tree is a rooted tree where the
children of each internal vertex are ordered
•The three common traversals are:
• Preorder traversal
• Inorder traversal
• Postorder traversal
Tree Traversal

• Pre-order traversal
• Visit node, traverse left subtree, traverse right subtree
• Node-left-right

• In-order traversal
• Traverse left subtree, visit node, traverse right subtree
• Left-node-right

• Post-order traversal
• Traverse left subtree, traverse right subtree, visit node
• Left-right-node
Example

Tree: M

A Y E

J R H

P Q T

Visiting sequence:
M A J Y R H P Q T E
The Preorder Traversal of T

In which order
does a preorder
traversal visit
the vertices in
the ordered
rooted tree T
shown to the
left?
Preorder:
Visit root, then
visit subtrees
left to right.
The Preorder Traversal of T
© The
McGraw-Hill
Companies,
Inc. all rights
reserved

Preorder:
Visit root, then
visit subtrees
left to right.
The Preorder Traversal of T
© The McGraw-Hill Companies, Inc. all rights reserved
The Preorder Traversal of T
© The McGraw-Hill Companies, Inc. all rights reserved
The Preorder Traversal of T
© The McGraw-Hill Companies, Inc. all rights reserved
Example

Tree: M

A Y E

J R H

P Q T

Visiting sequence:
J A M R Y P H Q T E
The Inorder Traversal of T

In which order
does an inorder
traversal visit the
vertices in the
ordered rooted tree
T shown to the
left?

Inorder:
Visit leftmost tree,
visit root, visit
other subtrees left
to right.
The Inorder Traversal of T
© The McGraw-Hill
Companies, Inc. all rights
reserved

Inorder:
Visit
leftmost tree,
visit root,
visit other
subtrees left
to right.
The Inorder Traversal of T

© The McGraw-Hill Companies, Inc. all rights reserved


The Inorder Traversal of T

© The McGraw-Hill Companies, Inc. all rights reserved


The Inorder Traversal of T

© The McGraw-Hill Companies, Inc. all rights reserved


Example

Tree: M

A Y E

J R H

P Q T

Visiting sequence:
J A R P Q T H Y E M
The Postorder Traversal of T
In which order
does a postorder
traversal visit
the vertices in
the ordered
rooted tree T
shown to the
left?
Postorder:
Visit subtrees
left to right, then
visit root.
The Postorder Traversal of T
© The
McGraw-Hill
Companies,
Inc. all rights
reserved

Postorder:
Visit
subtrees left
to right,
then visit
root.
The Postorder Traversal of T
© The McGraw-Hill Companies, Inc. all rights reserved
The Postorder Traversal of T
© The McGraw-Hill Companies, Inc. all rights reserved
The Postorder Traversal of T

© The McGraw-Hill Companies, Inc. all rights reserved


Representing Arithmetic Expressions

•Complicated arithmetic expressions can be


represented by an ordered rooted tree
• Internal vertices represent operators
• Leaves represent operands
•Build the tree bottom-up
• Construct smaller subtrees
• Incorporate the smaller subtrees as part of larger
subtrees
Example

(x+y)2 + (x-3)/(y+2)

+
 /

+ 2 – +

x y x 3 y 2
Infix Notation

• Traverse in inorder adding parentheses for


each operation
+
 /

+ 2 – +

x y x 3 y 2
(((x + y)  2)+((x – 3) / (y + 2)))
Prefix Notation (Polish Notation)

• Traverse in preorder:

+
 /

+ 2 – +

x y x 3 y 2

+  +x y 2 / – x3+y2
Evaluating Prefix Notation

•In an prefix expression, a binary operator


precedes its two operands
•The expression is evaluated right-left
•Look for the first operator from the right
•Evaluate the operator with the two operands
immediately to its right
Example

+ / + 2 2 2 / – 3 2 +
1 0
+ / + 2 2 2 / – 3
2 1
+ / + 2 2 2 / 1
1
+ / + 2 2 2
1
+ / 4 2 1
+ 2 1
3
Postfix Notation (Reverse Polish)

• Traverse in postorder
+
 /

+ 2 – +

x y x 3 y 2

x y +2  x 3 – y 2 + / +
Evaluating Postfix Notation

•In an postfix expression, a binary operator


follows its two operands
•The expression is evaluated left-right
•Look for the first operator from the left
•Evaluate the operator with the two
operands immediately to its left
Example

2 2 + 2 / 3 2 – 1 0 +
/ +
4 2 / 3 2 – 1 0 + /
+
2 3 2 – 1 0 + /
+
2 1 1 0 + /
+
2 1 1 / +
2 1 +
3
Any Questions?

You might also like