Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Programming in C++ using Data Structures

Unit - V
Trees and Graphs
Tree
A tree is a finite set of one or more nodes such that there is a specially designated node called the root,
and zero or more non empty sub trees T1, T2…..Tk, each of whose roots are connected by a directed edge
from root node.

• Root node: This is the topmost node in the tree hierarchy. In the above diagram, Node A is the root node.
Note that the root node doesn’t have any parent.
Trees…Cont’d
• Leaf node: It is the Bottom most node in a tree hierarchy. Leaf nodes are the nodes that do not have
any child nodes. They are also known as external nodes. Nodes E, F, G, H and C in the above tree are all
leaf nodes.
• Subtree: Subtree represents various descendants of a node when the root is not null. A tree usually
consists of a root node and one or more subtrees. In the above diagram, (B-E, B-F) and (D-G, D-H) are
subtrees.
• Parent node: Any node except the root node that has a child node and an edge upward towards the
parent.
• Ancestor Node: It is any predecessor node on a path from the root to that node. Note that the root
does not have any ancestors. In the above diagram, A and B are the ancestors of E.
• Key: It represents the value of a node.
• Level: Represents the generation of a node. A root node is always at level 1. Child nodes of the root are
at level 2, grandchildren of the root are at level 3 and so on. In general, each node is at a level higher
than its parent.
• Path: The path is a sequence of consecutive edges. In the above diagram, the path to E is A=>B->E.
• Degree: Degree of a node indicates the number of children that a node has. In the above diagram, the
degree of B and D is 2 each whereas the degree of C is 0.
Trees…Cont’d
General Tree
The general tree is the basic representation of a tree. It has a node and one or more child nodes. The top-
level node i.e. the root node is present at level 1 and all the other nodes may be present at various levels.
General tree has any number of children.
As shown in the diagram, a general tree may contain
any number of subtrees. The nodes B, C, and D are
present at level 2 and are sibling nodes. Similarly, nodes E,
F, G, and H are also sibling nodes.

The nodes present at different levels may exhibit a


parent-child relationship. In the above figure, nodes B, C
and D are children of A. Nodes E and F are children of B
whereas nodes G and H are children of D.
Trees…Cont’d

Forest
Whenever we delete the root node from the tree and the edges joining the next level elements and the
root, we obtain disjoint sets of trees as shown below.
In the diagram, we obtained two forests by
deleting the root node A and the three edges that were
connecting the root node to nodes B, C, and D.

Binary Tree
A tree data structure in which each node has at most two child nodes is called a binary tree. A binary tree
is the most popular tree data structure and is used in a range of applications like expression evaluation,
databases, etc.
In the diagram, The nodes A, B, and D have two
children each. A binary tree in which each node has
exactly zero or two children is called a full binary tree. In
this tree, there are no nodes that have one child.
A complete binary tree has a binary tree that is
completely filled with the exception of the lowest level
that is filled from left to right. The binary tree shown
above is a full binary tree.
Trees…Cont’d

Binary search Tree (BST)


The binary tree that is ordered is called the binary search tree. In a binary search tree, the nodes to the left are less
than the root node while the nodes to the right are greater than or equal to the root node.

In the diagram, the left nodes are all less than 20


which is the root element. The right nodes, on the other
hand, are greater than the root node. The binary search
tree is used in searching and sorting techniques.

BST Basic Operations


The basic operations that can be performed on a binary search tree data structure, are the following
Insert − Inserts an element in a tree/create a tree.
Search − Searches an element in a tree.
Preorder Traversal − Traverses a tree in a pre-order manner.
Inorder Traversal − Traverses a tree in an in-order manner.
Postorder Traversal − Traverses a tree in a post-order manner.
We shall learn creating (inserting into) a tree structure and searching a data item in a tree in this chapter. We shall learn
about tree traversing methods in the coming chapter.
Trees…Cont’d

Insert Operation
The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first locate its
proper location. Start searching from the root node, then if the data is less than the key value, search for the
empty location in the left subtree and insert the data. Otherwise, search for the empty location in the right
subtree and insert the data.
Algorithm
Trees…Cont’d

Search Operation
Whenever an element is to be searched, start searching from the root node, then if the data is less than
the key value, search for the element in the left subtree. Otherwise, search for the element in the right
subtree. Follow the same algorithm for each node.
Algorithm
Trees…Cont’d

Tree Traversal
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes
are connected via edges (links) we always start from the root (head) node. That is, we cannot randomly
access a node in a tree. There are three ways which we use to traverse a tree

• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
Trees…Cont’d

In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We should
always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order.

We start from A, and following in-order traversal, we


move to its left subtree B. B is also traversed in-order.
The process goes on until all the nodes are visited.
The output of inorder traversal of this tree will be −

D→B→E→A→F→C→G

Algorithm:
Trees…Cont’d

Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.

We start from A, and following pre-order traversal, we


first visit A itself and then move to its left subtree B. B
is also traversed pre-order. The process goes on until
all the nodes are visited. The output of pre-order
traversal of this tree will be

A→B→D→E→C→F→G
Algorithm:
Trees…Cont’d

Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then
the right subtree and finally the root node.

We start from A, and following Post-order traversal,


we first visit the left subtree B. B is also traversed
post-order. The process goes on until all the nodes are
visited. The output of post-order traversal of this tree
will be −

D→E→B→F→G→C→A

Algorithm:

You might also like