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

Trees: May 2, 2013 Applied Discrete Mathematics Week 14: Trees 1

The document discusses trees and binary trees. Some key points: - A tree is a connected graph without cycles. It can be empty or consist of a root node connected to zero or more subtrees. - Binary trees restrict nodes to having no more than two children. Traversal of binary trees can be preorder, inorder, or postorder. - Tree traversal involves recursively visiting nodes in a specified order (such as preorder: root, left subtree, right subtree). This can be used to print out tree data.

Uploaded by

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

Trees: May 2, 2013 Applied Discrete Mathematics Week 14: Trees 1

The document discusses trees and binary trees. Some key points: - A tree is a connected graph without cycles. It can be empty or consist of a root node connected to zero or more subtrees. - Binary trees restrict nodes to having no more than two children. Traversal of binary trees can be preorder, inorder, or postorder. - Tree traversal involves recursively visiting nodes in a specified order (such as preorder: root, left subtree, right subtree). This can be used to print out tree data.

Uploaded by

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

Trees

•Definition: A tree is a connected undirected graph with no


simple circuits.
•Since a tree cannot have a simple circuit, a tree cannot
contain multiple edges or loops.
•Therefore, any tree must be a simple graph.

•Theorem: An undirected graph is a tree if and only if there


is a unique simple path between any of its vertices.

Applied Discrete Mathematics


May 2, 2013 1
Week 14: Trees
Trees
• A tree is a collection of nodes
– The collection can be empty
– (recursive definition) If not empty, a tree consists
of a distinguished node r (the root), and zero or
more nonempty subtrees T1, T2, ...., Tk, each of
whose roots are connected by a directed edge
from r
Some Terminologies

• Child and Parent


– Every node except the root has one parent
– A node can have an zero or more children
• Leaves
– Leaves are nodes with no children
• Sibling
– nodes with same parent
More Terminologies
• Path
– A sequence of edges
• Length of a path
– number of edges on the path
• Depth of a node
– length of the unique path from the root to that node
• Height of a node
– length of the longest path from that node to a leaf
– all leaves are at height 0
• The height of a tree = the height of the root
= the depth of the deepest leaf
• Ancestor and descendant
– If there is a path from n1 to n2
– n1 is an ancestor of n2, n2 is a descendant of n1
– Proper ancestor and proper descendant
Trees
•Example: Are the following graphs trees?

No. Yes.

Yes. No.

Applied Discrete Mathematics


May 2, 2013 5
Week 14: Trees
Trees
•Definition: An undirected graph that does not contain
simple circuits and is not necessarily connected is called a
forest.
•In general, we use trees to represent hierarchical
structures.
•We often designate a particular vertex of a tree as the
root. Since there is a unique path from the root to each
vertex of the graph, we direct each edge away from the
root.
•Thus, a tree together with its root produces a directed
graph called a rooted tree.
Applied Discrete Mathematics
May 2, 2013 6
Week 14: Trees
Tree Terminology
•If v is a vertex in a rooted tree other than the root, the
parent of v is the unique vertex u such that there is a
directed edge from u to v.
•When u is the parent of v, v is called the child of u.
•Vertices with the same parent are called siblings.
•The ancestors of a vertex other than the root are the
vertices in the path from the root to this vertex, excluding
the vertex itself and including the root.

Applied Discrete Mathematics


May 2, 2013 7
Week 14: Trees
Tree Terminology
•The descendants of a vertex v are those vertices that have
v as an ancestor.
•A vertex of a tree is called a leaf if it has no children.
•Vertices that have children are called internal vertices.
•If a is a vertex in a tree, then the subtree with a as its root
is the subgraph of the tree consisting of a and its
descendants and all edges incident to these descendants.

Applied Discrete Mathematics


May 2, 2013 8
Week 14: Trees
Tree Terminology

•The level of a vertex v in a rooted tree is the length of the


unique path from the root to this vertex.

•The level of the root is defined to be zero.

•The height of a rooted tree is the maximum of the levels


of vertices.

Applied Discrete Mathematics


May 2, 2013 9
Week 14: Trees
Trees
•Example I: Family tree

James

Christine Bob

Frank Joyce Petra

Applied Discrete Mathematics


May 2, 2013 10
Week 14: Trees
Trees
•Example II: File system

usr bin temp

bin spool ls

Applied Discrete Mathematics


May 2, 2013 11
Week 14: Trees
Trees
•Example III: Arithmetic expressions

+ -

y z x y

This tree represents the expression (y + z) (x - y).

Applied Discrete Mathematics


May 2, 2013 12
Week 14: Trees
Trees
•Definition: A rooted tree is called an m-ary tree if every
internal vertex has no more than m children.
•The tree is called a full m-ary tree if every internal vertex
has exactly m children.
•An m-ary tree with m = 2 is called a binary tree.
•Theorem: A tree with n vertices has (n – 1) edges.
•Theorem: A full m-ary tree with i internal vertices contains
n = mi + 1 vertices.

Applied Discrete Mathematics


May 2, 2013 13
Week 14: Trees
Trees
• A tree is a loop-free connected graph that
contains no cycles.
• Each pair of vertices of a tree is connected by
a unique simple path.

A Tree Not a tree


Trees
• A tree with at least two vertices has at least
two vertices of degree 1.
• For every tree <V,E>, #V = 1 + #E.

A Tree Not a tree


Trees
• Let G=<V,E> be a loop free graph, then the following are
equivalent statements:
• G is a tree
• G is connected and the removal of any edge yields two trees
• G contains no cycles and #V = 1 + #E
• G is connected and #V = 1 + #E
• G has no cycles and adding one edge introduces one cycle
Example: Expression Trees

• Leaves are operands (constants or variables)


• The internal nodes contain operators
• Will not be a binary tree if some operators are not binary
Tree Traversal
• Used to print out the data in a tree in a certain
order
• Pre-order traversal
– Print the data at the root
– Recursively print out all data in the left subtree
– Recursively print out all data in the right subtree
Preorder, Postorder and Inorder
• Preorder traversal
– node, left, right
– prefix expression
• ++a*bc*+*defg
Preorder, Postorder and Inorder
• Postorder traversal • Inorder traversal
– left, right, node – left, node, right
– postfix expression – infix expression
• abc*+de*f+g*+ • a+b*c+d*e+f*g
Example: Unix Directory Traversal
PreOrder PostOrder
Preorder, Postorder and Inorder
Pseudo Code
Binary Trees
• A tree in which no node can have more than two children

Generic
binary tree

• The depth of an “average” binary tree is considerably smaller than N,


even though in the worst case, the depth can be as large as N – 1.

Worst-case
binary tree
Node Struct of Binary Tree
• Possible operations on the Binary Tree ADT
– Parent, left_child, right_child, sibling, root, etc
• Implementation
– Because a binary tree has at most two children,
we can keep direct pointers to them
Convert a Generic Tree to a Binary Tree

You might also like