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

Data Structures and Algorithms For Information Processing: Lecture 5: Trees

This document discusses trees and binary trees. It covers topics like binary tree terminology including root, leaves, subtrees and levels. It provides examples of using binary trees to represent arithmetic expressions and Chinese characters. It discusses properties of trees and different types of binary trees including full, complete and binary/unary trees. It also covers traversing trees using preorder, inorder and postorder traversal and provides Java code for a binary tree node class and methods for copying, traversing and printing trees.

Uploaded by

sankar_mca227390
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Data Structures and Algorithms For Information Processing: Lecture 5: Trees

This document discusses trees and binary trees. It covers topics like binary tree terminology including root, leaves, subtrees and levels. It provides examples of using binary trees to represent arithmetic expressions and Chinese characters. It discusses properties of trees and different types of binary trees including full, complete and binary/unary trees. It also covers traversing trees using preorder, inorder and postorder traversal and provides Java code for a binary tree node class and methods for copying, traversing and printing trees.

Uploaded by

sankar_mca227390
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Data Structures and Algorithms for

Information Processing
Lecture 5: Trees
Lecture 5: Trees
Binary Trees
A binary tree has nodes, similar to nodes in a
linked list structure.
Data of one sort or another may be stored at
each node.
Each node is either a leaf, having no children,
or an internal node, with one or two children
Lecture 5: Trees
Binary Trees
Lecture 5: Trees
Some Terminology
Root is the unique node with no parent
Leaves or terminals are nodes with no
children
A subtree is a node together with all its
descendents
Level of a node is number of nodes on path
from the node to root
Lecture 5: Trees
Example use of Trees
Arithmetic
Expressions

A*(((B+C)*(D*E))+F)
Lecture 5: Trees
- Each internal node
labeled by an operator
- Each leaf labeled by a
variable or numeric value
-Tree determines a
unique value


Example Use of Trees
Representing Chinese Characters
Characters composed
hierarchically into boxes
Boxes can be oriented
left-right, top-down, or
outside-inside
Each leaf labeled by
one of 300-400 radicals
Lecture 5: Trees
Binary vs. Binary-Unary
Important distinction : Sometimes binary trees are
defined to allow internal nodes with one or two
children.

There is a big difference...
Lecture 5: Trees
Counting Trees
The number of binary trees with internal nodes is




There is no such formula if we allow unary internal nodes!
Lecture 5: Trees
n
|
|
.
|

\
|
+ n
n
n
2
1
1
Exercise
Write efficient Java functions
int numBTrees(int n)
int numBUTrees(int n)
that return the number of binary
trees and binary/unary trees,
respectively.
Lecture 5: Trees
Properties of Trees
There is exactly one path connecting any two
nodes
Any two nodes have a least common ancestor
A tree with N internal nodes has N+1 external
nodes
(easy to prove by induction)
Lecture 5: Trees
The Best Trees
A binary tree is full if internal nodes
fill every level, except possibly the
last.

The height of a full binary tree with
internal nodes is
Lecture 5: Trees
1 log
2
+ s N h
N
Complete Binary Trees
A complete tree is one having levels and
leaves
Complete trees have a simple implementation
using arrays
(How?)
Lecture 5: Trees
n
n
2
Class for Binary Nodes
public class BTNode
{
private Object data;
private BTNode left;
private BTNode right;
...

Lecture 5: Trees
Class for Binary Nodes
public BTNode(Object obj,
BTNode l,
BTNode r)
{
data = obj; left = l; right= r;
}

public boolean isLeaf()
{
return (left == null) &&
(right == null);
}
...
Lecture 5: Trees
Copying Trees
public static BTNode
treeCopy(BTNode t)
{
if (t == null) return null;
else
{
BTNode leftCopy =
treeCopy(t.left);
BTNode rightCopy =
treeCopy(t.right);
return new BTNode(t.data,
leftCopy,
rightCopy);
}
}
Lecture 5: Trees
Tree Traversals
Lecture 5: Trees


A
S
A
M
P
L
E
R
T E
E
Preorder traversal
Tree Traversals
Lecture 5: Trees


A
S
A
M
P
L
E
R
T E
E
Inorder traversal
Tree Traversals
Lecture 5: Trees


A
S
A
M
P
L
E
R
T E
E
Postorder traversal
Tree Traversals
Lecture 5: Trees


A
S
A
M
P
L
E
R
T E
E
Level order traversal
Preorder Traversal
Process the root
Process the nodes in the left subtree
Process the nodes in the right subtree
Lecture 5: Trees
Preorder Print
public void preorderPrint()
{
System.out.println(data);
if (left != null)
left.preorderPrint();
if (right != null)
right.preorderPrint();
}
Lecture 5: Trees
Inorder Traversal
Profess the nodes in the left subtree
Process the root
Process the nodes in the right subtree
Lecture 5: Trees
Inorder Print
public void preorderPrint()
{
if (left != null)
left.preorderPrint();
System.out.println(data);
if (right != null)
right.preorderPrint();
}
Lecture 5: Trees

You might also like