0% found this document useful (0 votes)
13 views34 pages

Lecture8 Trees Binary Tree

Uploaded by

Bakunzi Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views34 pages

Lecture8 Trees Binary Tree

Uploaded by

Bakunzi Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Trees

Special Gratitude to Professor Richard F. Gilberg for his inspirational


book out of which this presentation was prepared
Introduction
• Understand and use basic tree terminologies and concepts
• Recognize and define the basic attributes of a binary tree
• Process trees using depth-first and breadth-first traversals
• Parse expressions using a binary tree
• Design and implement Huffman trees
• Understand the basic use and processing of general trees
Basic Tree
Concepts
• A tree consists of finite set
of elements, called nodes,
and a finite set of directed Edge
lines called branches
(edges), that connect the
nodes.
• The number of branches
associated with a node is Node
the degree of the node.

Data Structures: A Pseudocode Approach with C 3


Basic Tree Concepts
• Indegree branch is directed towards the node
• Outdegree branch is directed away from the node
• The sum of the indegree and outdegree branches is the
degree of the node.
• If the tree is not empty, the first node is called the root.
• The indegree of the root is, by definition, zero.
• With the exception of the root, all of the nodes in a tree must have an
indegree of exactly one; that is, they may have only one predecessor.

Data Structures: A Pseudocode Approach with C 5


Basic Tree Concepts
• All nodes in the tree can have zero, one, or more branches
leaving them; that is, they may have outdegree of zero,
one, or more.
• A leaf is any node with an outdegree of zero, that is, a node
with no successors.
• A node that is not a root or a leaf is known as an internal
node.
• A node is a parent if it has successor nodes; that is, if it has
outdegree greater than zero.
• A node with a predecessor is called a child.
Data Structures: A Pseudocode Approach with C 8
Basic Tree Concepts
• Two or more nodes with the same parents are called siblings.
• An ancestor is any node in the path from the root to the node.
• A descendant is any node in the path below the parent node;
that is, all nodes in the paths from a given node to a leaf are
descendants of that node.
• A path is a sequence of nodes in which each node is adjacent
to the next node.
• The level of a node is its distance from the root. The root is at
level 0, its children are at level 1, etc. …
• A Tree has n vertices and n-1 edges

Data Structures: A Pseudocode Approach with C 9


Basic Tree Concepts

• The height of the


tree is the level of
the leaf in the
longest path from
the root plus 1. By
definition the height
of any empty tree is
-1.

Data Structures: A Pseudocode Approach with C 11


Basic Tree Concepts
• A subtree is any
connected structure
below the root. The
first node in the
subtree is known as
the root of the
subtree.

Data Structures: A Pseudocode Approach with C 13


Recursive definition of a tree

• A tree is a set of nodes that either:


is empty or
has a designated node, called the root, from which hierarchically
descend zero or more subtrees, which are also trees.

Data Structures: A Pseudocode Approach with C 14


General Tree – organization chart format

Data Structures: A Pseudocode Approach with C 15


Indented list – bill-of-materials system in which a parts list
represents the assembly structure of an item

Data Structures: A Pseudocode Approach with C 16


Parenthetical Listing

• Parenthetical Listing – the


algebraic expression, where
each open parenthesis
indicates the start of a new
level and
• each closing parenthesis
completes the current level
and moves up one level in
the tree. A (B (C D) E F (G H I) )
Data Structures: A Pseudocode Approach with C 17
Binary Trees

A binary tree can have no more than two descendents. In this


section we discuss the properties of binary trees, four different
binary tree traversals

• Properties
• Binary Tree Traversals
• Expression Trees
• Huffman Code (infix, postfix and prefix expressions)

Data Structures: A Pseudocode Approach with C 19


Binary Trees

• A binary tree is a tree in which no node can have more than


two subtrees; the maximum outdegree for a node is two.
• In other words, a node can have zero, one, or two subtrees.
• These subtrees are designated as the left subtree and the
right subtree.

Data Structures: A Pseudocode Approach with C 20


A Binary tree has the recursive
appearance
Parent
• A Binary tree has three types of vertices :
• A vertex which has 2 edges incident on it
 Vertex of degree 2  Bivalent Vertex
• A vertex which has 3 edges incident on it L. child R. child
 Vertex of degree 3  Trivalent Vertex
• A vertex which has only one edge
incident on it  Vertex of degree 1 
Monovalent Vertex  Pendent Vertex
A null
tree is a
tree with
no nodes

Data Structures: A Pseudocode Approach with C 23


Complete and nearly complete binary
trees
• A complete tree has the maximum
number of entries for its height. The
maximum number is reached when
the last level is full.
• A tree is considered nearly
complete if it has the minimum
height for its nodes and all nodes in
the last level are found on the left
• NOTE: some books call complete
tree a full tree and nearly complete
tree a complete tree
Data Structures: A Pseudocode Approach with C 24
Depth ( Height) Calculation in a full binary tree of N vertices

Level 0 . . . . . . . . . . . . . 1 ..……………….. 20

Level 1 . . . . 2 3 ….. 21

Level 2 . . 4 5 6 7 ... 22
: :
: :
Level l . . . 2l

20 + 21 + 22 + . . . + 2l = N in case of a full binary tree


Binary Tree Traversal

• A binary tree traversal requires that each node of the tree be


processed once and only once in a predetermined sequence.
• In the depth-first traversal processing process along a path
from the root through one child to the most distant
descendant of that first child before processing a second
child.
• In a breadth first traversal, process all the siblings before
going to the next level

Data Structures: A Pseudocode Approach with C 27


Binary traversal

Data Structures: A Pseudocode Approach with C 28


Data Structures: A Pseudocode Approach with C 30
Data Structures: A Pseudocode Approach with C 31
Preorder traversal

A AB

ABC ABCD

ABCDE ABCDEF

Data Structures: A Pseudocode Approach with C 32


Data Structures: A Pseudocode Approach with C 33
Data Structures: A Pseudocode Approach with C 34
Data Structures: A Pseudocode Approach with C 35
Data Structures: A Pseudocode Approach with C 36
Data Structures: A Pseudocode Approach with C 37
Breadth first
traversal is back
tracking algorithm
design strategy

Data Structures: A Pseudocode Approach with C 38


Data Structures: A Pseudocode Approach with C 39
Data Structures: A Pseudocode Approach with C 40
Data Structures: A Pseudocode Approach with C 41
Data Structures: A Pseudocode Approach with C 42
Data Structures: A Pseudocode Approach with C 43

You might also like