Professional Documents
Culture Documents
A graph is a data structure that consist a sets of vertices (called nodes) and edges. There
are two ways to store Graphs into the computer's memory:
• Sequential representation (or, Adjacency matrix representation)
• Linked list representation (or, Adjacency list representation)
Sequential representation
An adjacency matrix is a 2D array of V x V vertices. Each row and column represent a vertex.
If the value of any element a[i][j] is 1, it represents that there is an edge connecting vertex i and
vertex j.
Linked list representation
An adjacency list represents a graph as an array of linked lists.
The index of the array represents a vertex and each element in its linked list represents
the other vertices that form an edge with the vertex.
Incidence Matrix
The Incidence Matrix describes which branches are incident at which nodes and what
are the orientations relative to the respective nodes. This incidence matrix is also
termed a complete incidence matrix.
The incidence matrix represents a network symbolically. It also makes testing and
identifying the independent variables easier. An incidence matrix is a matrix that
uniquely represents a graph.
1.We will write the +1 if there is an outgoing branch in the kth node.
2.We will write the -1 if there is an incoming branch in the kth node.
3.We will write 0 in all the other branches.
TREES
A tree is a non-linear abstract data type with a hierarchy-based structure. It consists of
nodes (where the data is stored) that are connected via links. The tree data structure
stems from a single node called a root node and has subtrees connected to the root.
A tree is also one of the data structures that represent hierarchical data.
TREE TERMINOLGIES
•Root: The root node is the topmost node in the tree hierarchy.
In other words, the root node is the one that doesn't have any
parent. In the above structure, node numbered 1 is the root
node of the tree. If a node is directly linked to some other
node, it would be called a parent-child relationship.
•Child node: If the node is a descendant of any node, then the
node is known as a child node.
•Parent: If the node contains any sub-node, then that node is
said to be the parent of that sub-node.
•Sibling: The nodes that have the same parent are known as
siblings.
•Leaf Node:- The node of the tree, which doesn't have any
child node, is called a leaf node. A leaf node is the bottom-most
node of the tree. There can be any number of leaf nodes present
in a general tree. Leaf nodes can also be called external nodes.
•Internal nodes: A node has atleast one child node known as
an internal
Tree –Memory Representation
Binary Tree
The Binary tree means that the node can have maximum two children. Here, binary
name itself suggests that 'two'; therefore, each node can have either 0, 1 or 2 children.
The above tree is a binary tree because each node contains the
utmost two children. The logical representation of the above
tree is given below:
The complete binary tree is a tree in which all the nodes are completely filled except
the last level.
In the last level, all the nodes must be as left as possible.
In a complete binary tree, the nodes should be added from the left.
A tree is a perfect binary tree if all the internal nodes have 2 children, and all the leaf
nodes are at the same level.
Note: All the perfect binary trees are the complete binary
trees as well as the full binary tree, but vice versa is not
true, i.e., all complete binary trees and full binary trees are
the perfect binary trees.
4. Skewed Binary Tree
The balanced binary tree is a tree in which both the left and right trees differ by atmost
1.
For example, AVL and Red-Black trees are balanced binary tree.
Expression tree
The expression tree is a tree used to represent the various expressions. The tree
7 + ((1+8)*3)
data structure is used to represent the expressional statements. In this tree, the
internal node always denotes the operators.
• The leaf nodes always denote the operands.
• The operations are always performed on these operands.
• The operator present in the depth of the tree is always at the highest priority.
• The operator, which is not much at the depth in the tree, is always at the lowest
priority compared to the operators lying at the depth.
• The operand will always present at a depth of the tree; hence it is considered
the highest priority among all the operators.
• In short, we can summarize it as the value present at a depth of the tree is at
the highest priority compared with the other operators present at the top of the
tree.
• The main use of these expression trees is that it is used to evaluate,
analyze and modify the various expressions.
Tree traversal (Inorder, Preorder an Postorder)
The term 'tree traversal' means traversing or visiting each node of a tree. There is a
single way to traverse the linear data structure such as linked list, queue, and stack.
Whereas, there are multiple ways to traverse a tree that are listed as follows -
• Preorder traversal
• Inorder traversal
• Postorder traversal
Preorder traversal
follows the 'root left right’,
It means that, first root node is visited after that the left subtree is traversed
recursively, and finally, right subtree is recursively traversed. As the root node
is traversed before (or pre) the left and right subtree, it is called preorder
traversal.
Algorithm
1. Until all nodes of the tree are not visited
2.