DATA STRUCTURES
TREES
UNIT 2 PART 1
BCA, NGMC
Introduction
• One of the disadvantages of using an array or linked list
to store data is the time necessary to search for an item.
• Since both the arrays and Linked Lists are linear
structures the time required to search a “linear” list is
proportional to the size of the data set.
• For example, if the size of the data set is n, then the
number of comparisons needed to find (or not find) an
item may be as bad as some multiple of n. So imagine
doing the search on a linked list (or array) with n =
106 nodes.
• Therefore it seems that better (more efficient) data
structures are needed to store and search data.
• In this chapter, we can extend the concept of
linked data structure (linked list, stack, queue) to
a structure that may have multiple relations
among its nodes. Such a structure is called a tree.
• A tree is a collection of nodes connected by
directed (or undirected) edges. A tree is
a nonlinear data structure, compared to arrays,
linked lists, stacks and queues which are linear
data structures.
• A tree can be empty with no nodes or a tree is a
structure consisting of one node called
the root and zero or one or more subtrees.
• A tree has following general properties:
– One node is distinguished as a root;
– Every node (exclude a root) is connected by a
directed edge from exactly one other node; A
direction is: parent -> children
Advantages of Trees
• Trees are so useful and frequently used, because
they have some very serious advantages:
• Trees reflect structural relationships in the data.
• Trees are used to represent hierarchies.
• Trees provide an efficient insertion and searching.
• Trees are very flexible data, allowing to move sub
trees around with minimum effort.
Node: A node is a structure which may contain a value
or condition, or represent a separate data structure.
Root: The top node in a tree, the prime ancestor.
Child: A node directly connected to another node
when moving away from the root, an immediate
descendant.
Parent: The converse notion of a child, an immediate
ancestor.
Siblings: A group of nodes with the same parent.
Neighbor: Parent or child.
Descendant: A node reachable by repeated proceeding
from parent to child. Also known as subchild.
Ancestor: A node reachable by repeated proceeding
from child to parent.
Leaf node: A node with no children.
Branch node: A node with at least one child.
Degree: For a given node, its number of children. A
leaf is necessarily degree zero. The degree of a tree
is the degree of its root.
Degree of tree: The degree of the root.
Edge: The connection between one node and
another.
Path: A sequence of nodes and edges connecting a
node with a descendant.
Distance: The number of edges along the shortest
path between two nodes.
Depth: The distance between a node and the root.
Level: 1 + the number of edges between a node
and the root, i.e. (Depth + 1)
Height: The number of edges on the longest path
between a node and a descendant leaf.
Width: The number of nodes in a level.
Breadth: The number of leaves.
Height of tree: The height of the root node or the
maximum level of any node in the tree
Forest: A set of n ≥ 0 disjoint trees.
Sub Tree: A tree T is a tree consisting of a node
in T and all of its descendants in T.
Ordered Tree: A rooted tree in which an
ordering is specified for the
children of each vertex.
Size of a tree: Number of nodes in the tree.