Notes on Tree and Graph
Notes on Tree and Graph
What is Tree?
• Degree of node: Degree of a node is the total
number of children of that node. Degree of a
• Tree is a Non-linear data structure which tree is the highest degree of a node among all
representing a hierarchical data. the nodes in the tree.
• In a tree the items are arranged in • Degree of tree: The highest degree of the node
sorted sequence. among all the nodes in a tree is called the
• Tree always stores the information Degree of Tree.
naturally in a form of hierarchy style. • Height of Tree: In a tree data structure, the
number of edges from the leaf node to the
particular node in the longest path is known as
the height of that node. In the tree, the height
of the root node is called "Height of Tree".
• Sibling nodes: Sibling nodes are nodes on the
same hierarchical level under the same parent
node.
• IN-Degree: The total count of incoming edges
to a particular vertex is known as the in-degree
of that vertex.
• OUT-Degree: the total number of outgoing
edges from a particular vertex is known as the
Definition: out-degree of that vertex.
• Edge: Edge represents a path between two
Node: It is a data element which contains vertices or a line between two vertices.
information or as well as address of next node that • Ancestor: A node that is connected to all
is called as Node. lower-level nodes is called an "ancestor".
• Descendant node: A descendant is the inverse
Leaf node: A node which do not have child that is relationship of ancestor.
called as Leaf node.
1. General Tree:-
A general tree is a tree where each node may
have zero or more children. General trees are
used to model applications such as file systems.
2. Binary Tree:- Binary Tree is defined as a tree data structure where each node has
at most 2 children. Since each element in a binary tree can have
only 2 children
1. Inserting an element.
2. Removing an element.
3. Searching for an element.
4. Traversing the tree.
Application of Binary Trees:
A Binary Tree is a full binary tree if every node has 0 or 2 children. The following are examples of a full
binary tree. We can also say a full binary tree is a binary tree in which all nodes except leaf nodes have two
children. A full Binary tree is a special type of binary tree in which every parent node/internal node has
either two or no children. It is also known as a proper binary tree.
A Strict Binary Tree is also known as Proper Binary Tree and Full Binary Tree. A Binary Tree is identified as a
Strict Binary Tree if each parent node contains either no or two children. All nodes contain two children in a
Strict Binary Tree except the leaf nodes which have 0 children.
The above figure is an example of a Strict Binary Tree. The internal nodes
(node 1, node 2, node 3, and node 5) have 2 children each, whereas the
leaf nodes (node 4, node 6, node 7, node 8, node 9) have no children.
If a tree which is dominated by left child node or right child node, is said to be a Skewed Binary Tree. In a
skewed binary tree, all nodes except one have only one child node. The remaining node has no child.
a) In a left skewed tree, most of the nodes have the left child without corresponding right child.
b) In a right skewed tree, most of the nodes have the right child without corresponding left child.
ADVANTAGES OF SKEWED BINARY TREE
These tree traversal algorithms are divided into two types: 1.Depth First Algorithms, 2.Breadth-First
Algorithms.
In-Depth First Algorithm, trees are traversed downwards. On the other hand, pre Order, In Order, Post
Order are actually depth-first traversal algorithms. Post-Order traversal is used to delete the binary tree. Let
us dig deeper into the Post Order traversal of a Binary tree and see how to pre order traversal is
implemented and so on.
Post-Order Traversal:
Post order traversal of Binary tree is a traversal method, where left subtree is visited first, then right subtree, and finally
root node. Unlike array and linked lists, being linear data structures, we have several ways of traversing binary trees
due to their hierarchical nature.
Let us consider an example of a Binary tree for Post Order traversing. The steps followed to get PostOrder is simple,
Visit the left subtree first, i.e., D, then traverse to the right subtree, i.e., E, then root node B, traverse to the right
subtree, i.e., C, and then the root node for this subtree, i.e., A.
Advantages
Disadvantages
Pre-order traversal is used to create a copy of the tree. Pre-order traversal is also used to get prefix
expressions on an expression tree.
• Steps followed to get Preorder is simple, visit the root
node first, i.e. A, then traverse to the left node of the
root, i.e. B, as node B has child nodes, traverse to the
left node of the subtree, i.e., D.
• As there are no other subtrees to node D, move to
the right node, i.e., E.
• As there are no other subtrees to node E, move to the
top and check if there is any other right node to the
root node, i.e. C.
• And preorder traversal will move as [A, B, D, E, C].
IN-Order Traversal:
If we want to traverse the nodes in ascending order, then we use the inorder traversal. Following are the
steps required for the inorder traversal:-