Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Notes on Tree and Graph

The document provides an overview of trees and graphs, focusing on the structure and characteristics of trees, including definitions of nodes, leaf nodes, parent and child nodes, and degrees. It describes various types of trees such as general trees and binary trees, along with their operations and traversal methods like pre-order, in-order, and post-order. Additionally, it outlines the advantages and disadvantages of skewed binary trees and discusses the importance of tree traversals in processing hierarchical data.

Uploaded by

Mahima Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Notes on Tree and Graph

The document provides an overview of trees and graphs, focusing on the structure and characteristics of trees, including definitions of nodes, leaf nodes, parent and child nodes, and degrees. It describes various types of trees such as general trees and binary trees, along with their operations and traversal methods like pre-order, in-order, and post-order. Additionally, it outlines the advantages and disadvantages of skewed binary trees and discusses the importance of tree traversals in processing hierarchical data.

Uploaded by

Mahima Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

Parent node: Every node (excluding a root) in a


tree is connected by a directed edge from exactly
one other node. This node is called a parent.

Child node: the node which is descendant of any


node is called as CHILD node. In simple words, the
node which has a link from its parent node is called
as child node.

Degree: The number of subtrees of a node is called


its degree.
Types of Trees:

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.

Representing General Trees:

- May the number of children per node vary


over a large range (possibly with no logical
upper limit)?

- Setting an upper limit renders some trees un-


representable.

- Even with an upper limit, allocating a fixed


number of pointers within each node may
waste a huge amount of space.

- Supporting a variable number of pointers also


raises performance issues

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

Basic Operation on Binary Tree:

1. Inserting an element.
2. Removing an element.
3. Searching for an element.
4. Traversing the tree.
Application of Binary Trees:

Represent hierarchical data. Used in editing software like


Microsoft Excel and
Diagram of Binary tree spreadsheets.
For implementing priority Used to find elements in less
queues. time.
Used to enable fast memory To perform encoding and
allocation in computers. decoding operations.

❖ Types of Binary trees:-


1. Full Binary Tree
2. Strictly Binary Tree
3. Skewed Binary Trees

1. Full Binary Tree

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.

2. Strict 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.

3. Skewed Binary Tree

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

• They are known for their systematic nature.


• If we maintain and keep a balance between the
various functions, deletion and insertion operations
provide rather quick results as compared to others.
• They are very basic or simple and easy to maintain as
compared to the rest of the trees in data structures.

DISADVANTAGES OF SKEWED BINARY TREE

• In complex cases, it takes a very large amount of time


in searching and therefore provides the result.

Binary Tree Traversals


Often we wish to process a binary tree by “visiting” each of its nodes, each time performing a specific action
such as printing the contents of the node. Any process for visiting all of the nodes in some order is called a
traversal. Any traversal that lists every node in the tree exactly once is called an enumeration of the tree’s
nodes. Some applications do not require that the nodes be visited in any particular order as long as each
node is visited precisely once. For other applications, nodes must be visited in an order that preserves some
relationship.

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.

And hence Post order traversal will move as [D, E, B, C, A]

Advantages

• PostOrder traversal is used to delete the binary tree.


• Traversal should be used while getting postfix expression of a binary tree
• Binary tree traversals give quick searching, insertion, and deletion in
cases where the tree is balanced.

Disadvantages

• Deletion algorithm in traversal in complex comparatively.


Pre-Order Traversal:
Pre order traversal of binary tree is a traversal method, where the root node is visited first, then left subtree
and then the right sub tree. Unlike array and linked lists, linear data structures, we have several ways of
traversing binary trees due to their hierarchical nature.

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:-

• Visit all the nodes in the left subtree


• Visit the root node
• Visit all the nodes in the right subtree

You might also like