MS Week 14 - Trees (Part 1)
MS Week 14 - Trees (Part 1)
ALGORITHMS
CS – 102
Instructor: Maria N. Samad
April 7th, 2025
TREES
• A non-linear data structure in which items are arranged in a
sorted sequence.
• It is used to impose a hierarchical structure on a collection of
data items.
• A tree is recursively defined as a set of one or more nodes where
one node is designated as the root of the tree and all the
remaining nodes can be partitioned into non-empty sets each of
which is a subtree of the root.
• A node is a structure which may contain a value, a condition, or
represent a separate data structure (which could be a tree of its
own).
2
TREES
• A tree consists of a finite set of elements, called nodes, and a
finite set of directed lines, called branches, that connect the
nodes
• The number of branches associated with a node is the degree of
the node
• When the branch is directed towards the node, it is an indegree
branch
• When the branch is directed away from the node, it is an
outdegree branch
• The sum of the indegree and outdegree branches is the degree
of the node
3
TREES vs GRAPHS
• A tree is a special type of graph that is connected and
acyclic, meaning that there are no cycles in a tree.
• In a tree, there is a unique path between any two vertices,
and there is a single vertex called the root that is used as the
starting point for traversing the tree downwards.
• Trees can be used to model hierarchical relationships, such
as the structure of a file system or the organization of a
company.
4
TREES
• A node that has a child is called the child's parent node.
• A node has at most one parent.
• Parent node is also known as predecessor/ancestor node
• Child node is also called as a successor node
• There is a special data item called the root of the tree. And
its remaining data items are partitioned into number of
mutually exclusive subsets, each of which is itself a tree. And
they are called subtrees.
5
TREES
• If the tree is not empty, the first node is always 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 parent
• All nodes in the tree can have zero, one, or more branches
leaving them; that is, they may have an outdegree of zero,
one, or more (zero or more children)
6
TREES
7
TREES
• Can implement many algorithms much faster than when
using linear data structures, such as arrays/lists or linked lists
• Derived from the family tree structure
• Except for the root, each element has a parent element, and
zero or more children elements
8
TREES
• A leaf is any node with an outdegree of zero, that is, a node
with no successors/child nodes
• A node is a parent if it has successor nodes – that is, if it has
an outdegree greater than zero
• A node is a child if it has one predecessor – that is, it has an
indegree of one
9
TREES
10
TREES
• Two nodes that are children of same parent are called
siblings
• Two nodes that are children of different parents, but are at
the same level in a tree are called cousins
• A node v is external node if it has no children.
• Known as the leaves of the tree
• A node v is internal node if it has one or more children.
12
TREES
• A node u is an ancestor of a node v if u is an ancestor of the
parent of v
• A node v is descendant of node u if u is an ancestor of v
• The subtree of tree T rooted at node v is the tree consisting
of all descendants of v in T including v itself
• An edge of tree is a pair of nodes (u,v) such that u is the
parent of v
• A path of tree is a sequence of nodes such that any two
consecutive nodes in the sequence form an edge
13
TREES
• Used extensively in computer science:
• To represent algebraic formulas
• As an efficient method for searching large, dynamic lists
• For applications such as artificial intelligence systems and
• Encoding algorithms
14
TREES
15
TREES
• Inheritance relationship between classes in Python programs
forms a tree
16
TREES
• A tree is ordered if there is a meaningful linear order among
the children of each node
• Done by arranging siblings left to right in a tree
17
TREE ABSTRACT DATA TYPE
• An element is stored at each node position, and these
positions satisfy parent-child relationships
• Must support the method:
• p.element( ) – Returns the element stored at node p
18
TREE ABSTRACT DATA TYPE
• Other accessor methods:
• T.root ( ) – Returns the root node of the tree, T or None if empty
• T.is_root(p) – Returns True if node p is the root of tree T
• T.parent(p) - Return the parent of node p, or None if p is the root of T
• T.num_children(p): Return the number of children of node p
• T.children(p): Generate an iteration of the children of node p
• T.is_leaf(p): Return True if node p does not have any children
• len(T): Return the number of nodes that are contained in tree T
• T.is_empty( ): Return True if tree T does not contain any nodes
• iter(T): Generate an iteration of all nodes stored within tree T
19
TREE ABSTRACT DATA TYPE
• Any of these methods that accepts “node” as argument should
generate a ValueError if that node is invalid for T
• If T is ordered then T.children(p) reports them in order
• If p is leaf, then T.children(p) generates an empty iteration
• If T is empty, then iter(T) generates an empty list
• Not defining how to create or modify a tree because it is defined
according to its type, eg. Binary Trees, Linked Trees, etc
20
DEPTH OF A TREE
• The depth of p is the number of ancestors of p excluding p itself
• The depth of root node is 0
• Can be recursively defined as follows:
• If p is the root, then depth is 0
• Otherwise, depth of p is one plus the depth of parent of p
21
DEPTH OF A TREE - ALGORITHM
• def depth(Tree, p):
if (Tree.is_root(p)):
return 0
else:
return 1 + depth(Tree.parent(p))
22
HEIGHT OF A TREE
• A height is the number of edges to its most distant leaf node, i.e. the
number of edges/branches on the longest path
• The height of a non-empty tree T including just the root is the height
of the root of T, i.e. 1
• Can be recursively defined as:
• If p is a leaf node, then height of p is 1
• Otherwise, height of p is one more than the max of heights of p’s children
23
HEIGHT OF A TREE - ALGORITHM
• def height(Tree, p):
if (Tree.is_leaf(p)):
return 1
else:
return 1 + max(height(c) for c in Tree.children(p))
24
PATH IN TREES
• The distance of a node from the root determines how efficiently it can
be located
• The children of any node can be accessed by following only one
branch path, the one that leads to the desired node
• The nodes at level 1, which are children of the root, can be accessed
by following only one branch
• Similarly, the nodes at level 2 of a tree can all be accessed by
following only two branches from the root
• The shorter the tree, the easier it is to locate any desired node in the
tree
25
PATH IN TREES
• For example, the following tree highlights a path going from Node 43
to Node 76
• Therefore, Path is: Node 43 → Node 887 → Node 46 → Node 78 → Node 76
26
TREES
• Activity Sheet
27
TREES
• Activity Sheet Solution
• Tree Terminology Solution
28
BINARY TREES
• An ordered tree with following properties:
• Every node has at most two children
• Each child is labeled as left child and right child according to their position
• A left child precedes a right child
• The subtree rooted at left or right child of an internal node v, is called
a left subtree or right subtree respectively, of v
• All the nodes are defined at certain levels
• Root node is assumed to be at Level 0
• The children of root node are at Level 1
• And it continues until the last level, which is Height – 1
29
BINARY TREES
• A binary tree is said to be proper if each node has either zero or two
children
• Also called full binary trees
• A binary tree that is not proper is said to be improper
30
BALANCE OF A BINARY TREE
• To determine whether a tree is balanced, we calculate its balance
factor
• The balance factor of a binary tree is the difference in height between
its left (HL) and right subtrees (HR), i.e.
B = HL – HR
• Using the definition for balance factor given above we say that a
subtree is left-heavy if the balance factor is greater than zero.
• If the balance factor is less than zero then the subtree is right heavy.
• If the balance factor is zero then the tree is perfectly in balance.
31
BALANCE FACTORS
33
BALANCE OF A BINARY TREE
• Example of Balanced Binary Tree
34
BALANCE OF A BINARY TREE
• Example of Unbalanced Binary Tree
35
BINARY TREE ADT
• A specialization of general Tree ADT that supports the following
additional accessor methods:
• T.left(p) – Returns the position that represents the left child of p, or None if p
has no left child
• T.right(p) – Returns the position that represents the right child of p, or None if
p has no right child
• T.sibling(p) – Returns the position that represents the sibling of p, or None if p
has no sibling
• Again not implementing the instantiation or updating of Binary Trees
36
DECISION TREES
• Each node is associated with a Question that can be answered as
either “Yes” or “No”
• Depending on that answer, the left or right child is accessed
• With each decision on a node, follow an edge from a parent to a
child, eventually tracing a path in the tree from the root to a leaf
• Such trees are known as decision trees because a leaf position will
represent the decision of what to do in the given situation
• A decision tree is a proper binary tree
37
DECISION TREES
• Example: Decision Tree providing investment advice
38
EXPRESSION TREES
• An arithmetic expression can also be represented by a binary tree
• Leaves are associated with variables and constants
• Internal nodes are associated with the operators involved in the expression
• Each node has a value associated with it as follows:
• If a node is leaf, then its value is that of its variable or constant
• If a node is internal, then its value is defined by applying its operation to the
values of its children
39
EXPRESSION TREES
• Example: Arithmetic expression tree
40
RECURSIVE BINARY TREE
• Can define binary tree in a recursive way such that a binary tree is
empty or it consists of:
• A node r called the root node of the tree T that stores an element
• A binary tree (could be empty) calls the left subtree of T
• A binary tree (could be empty) calls the right subtree of T
41
BINARY TREE PROPERTIES
• The maximum number of nodes at level, L of a binary tree is 2L
• The maximum number of nodes in a Binary tree of height h i.e. the
total number of nodes in the tree is 2h – 1
• In a Binary Tree with N nodes, minimum possible height or minimum
number of levels is log2(N + 1)
• A binary tree with nL leaves has at least ceil(log2 nL) + 1 levels
• In a Binary Tree where each node has 0 or 2 children, the number of
leaf nodes is always one more than nodes with two children, i.e.
nL = ni + 1
42
BINARY TREE PROPERTIES
• If a binary tree has ni internal nodes then total number of nodes is:
N = 2ni + 1
• If a binary tree has a total of N nodes, then number of internal nodes
is: ni = (N – 1)/2
• If a binary tree has a total of N nodes, then the number of leaves is:
nL = (N + 1)/2
• If a tree has nL leaves, then total number of nodes is:
N = 2nL – 1
• If a Binary tree has nL leaves then the number of internal nodes is:
ni = nL – 1
43