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

TREE:

 A tree is a non-linear and hierarchical data structure where the


elements are arranged in a tree-like structure.
 In a tree, the topmost node is called the root node. Each node
contains some data, and data can be of any type.
 It consists of a central node, structural nodes, and sub-nodes which
are connected via edges.
 Different tree data structures allow quicker and easier access to the
data as it is a non-linear data structure. A tree has various
terminologies like Node, Root, Edge, Height of a tree, Degree of a
tree, etc.
Characteristics of a Tree:
 Characteristics of a Tree:
 The tree has various different characteristics which are as
follows:
A tree is also known as a Recursive data structure.
 In a tree, the Height of the root can be defined as the
longest path from the root node to the leaf node.
 Ina tree, one can also calculate the depth from the top to
any node. The root node has a depth of 0.
Applications of Tree:
 Different applications of Tree are as follows:
 Heap is a tree data structure that is implemented using arrays and used to
implement priority queues.
 B-Tree and B+ Tree are used to implement indexing in databases.
 Syntax Tree helps in scanning, parsing, generation of code, and
evaluation of arithmetic expressions in Compiler design.
 K-D Tree is a space partitioning tree used to organize points in K-
dimensional space.
 Spanning trees are used in routers in computer networks.
Operation performed on tree:
 A tree is a non-linear data structure that consists of nodes connected by edges. Here are some common
operations performed on trees:
 Insertion: New nodes can be added to the tree to create a new branch or to increase the height of the
tree.
 Deletion: Nodes can be removed from the tree by updating the references of the parent node to remove
the reference to the current node.
 Search: Elements can be searched for in a tree by starting from the root node and traversing the tree
based on the value of the current node until the desired node is found.
 Traversal: The elements in a tree can be traversed in several different ways, including in-order, pre-
order, and post-order traversal.
 Height: The height of the tree can be determined by counting the number of edges from the root node to
the furthest leaf node.
 Depth: The depth of a node can be determined by counting the number of edges from the root node to
the current node.
 Balancing: The tree can be balanced to ensure that the height of the tree is minimized and the
distribution of nodes is as even as possible.
Real-Life Applications of Tree:

 In real life, tree data structure helps in Game Development.


 It also helps in indexing in databases.
 A Decision Tree is an efficient machine-learning tool, commonly used in
decision analysis. It has a flowchart-like structure that helps to understand
data.
 Domain Name Server also uses a tree data structure.
 The most common use case of a tree is any social networking site.
Graph:
A graph is a non-linear data structure that consists of
vertices (or nodes) and edges.
 It consists of a finite set of vertices and set of edges that
connect a pair of nodes.
 The graph is used to solve the most challenging and
complex programming problems.
 It has different terminologies which are Path, Degree,
Adjacent vertices, Connected components, etc.
Characteristics of Graph:

 The graph has various different characteristics which are as follows:

 The maximum distance from a vertex to all the other vertices is considered
the Eccentricity of that vertex.
 The vertex having minimum Eccentricity is considered the central point of the
graph.
 The minimum value of Eccentricity from all vertices is considered the radius
of a connected graph.
Applications of Graph:

 Different applications of Graphs are as follows:


 The graph is used to represent the flow of computation.
 It is used in modeling graphs.
 The operating system uses Resource Allocation Graph.
 Also used in the World Wide Web where the web pages represent the nodes.
Operation performed on Graph:
 A graph is a non-linear data structure consisting of nodes and edges. Here are some common
operations performed on graphs:
 Add Vertex: New vertices can be added to the graph to represent a new node.
 Add Edge: Edges can be added between vertices to represent a relationship between nodes.
 Remove Vertex: Vertices can be removed from the graph by updating the references of adjacent
vertices to remove the reference to the current vertex.
 Remove Edge: Edges can be removed by updating the references of the adjacent vertices to
remove the reference to the current edge.
 Depth-First Search (DFS): A graph can be traversed using a depth-first search by visiting the
vertices in a depth-first manner.
 Breadth-First Search (BFS): A graph can be traversed using a breadth-first search by visiting the
vertices in a breadth-first manner.
 Shortest Path: The shortest path between two vertices can be determined using algorithms such as
Dijkstra’s algorithm or A* algorithm.
 Connected Components: The connected components of a graph can be determined by finding sets
of vertices that are connected to each other but not to any other vertices in the graph.
 Cycle Detection: Cycles in a graph can be detected by checking for back edges during a depth-first
search.
Binary Search Tree
 A Binary Search Tree is a data structure used in computer science
for organizing and storing data in a sorted manner.

 Each node in a Binary Search Tree has at most two children,


a left child and a right child, with the left child containing values
less than the parent node and the right child containing values
greater than the parent node.

 This hierarchical structure allows for efficient searching, insertion,


and deletion operations on the data stored in the tree.
 The properties that separate a binary search tree from a regular binary tree is

1. All nodes of left subtree are less than the root node
2. All nodes of right subtree are more than the root node
3. Both subtrees of each node are also BSTs i.e. they have the above two
properties
A tree having a right subtree with one value smaller than the root is shown to demonstrate that it is not a
valid binary search tree
4 is not found so, traverse through the right subtree of 3
4 is not found so, traverse through the right subtree of 3
Search Operation
The algorithm depends on the property of BST that if each left subtree has values below root and
each right subtree has values above the root.

If the value is below the root, we can say for sure that the value is not in the right subtree; we need
to only search in the left subtree and if the value is above the root,

we can say for sure that the value is not in the left subtree; we need to only search in the right
subtree.
If root == NULL
 return NULL;
If number == root->data
 return root->data;
If number < root->data
 return search(root->left)
If number > root->data
 return search(root->right)
Tree Traversal

 Tree Traversal techniques include various ways to visit all the nodes of the
tree.

 Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which
have only one logical way to traverse them, trees can be traversed in
different ways.
Tree Traversal Meaning
 Tree Traversal refers to the process of visiting or accessing each node of
the tree exactly once in a certain order.
 Tree traversal algorithms help us to visit and process all the nodes of the
tree.
 Since tree is not a linear data structure, there are multiple nodes which
we can visit after visiting a certain node. There are multiple tree
traversal techniques which decide the order in which the nodes of the
tree are to be visited.
Inorder Traversal:
 Inorder traversal visits the node in the order: Left -> Root ->
Right
 Uses of Inorder Traversal:
n the case of binary search trees (BST), Inorder traversal gives
nodes in non-decreasing order.
 To get nodes of BST in non-increasing order, a variation of
Inorder traversal where Inorder traversal is reversed can be
used.
 Inorder traversal can be used to evaluate arithmetic
expressions stored in expression trees.
TREE TRAVERSAL

 IN ORDER
 PRE ORDER
 POST ORDER
 Output:
Inorder Traversal: 10 20 30 100 150 200 300
Preorder Traversal: 100 20 10 30 200 150 300
Postorder Traversal: 10 30 20 150 300 200 100
Inorder Traversal:
Below is the idea to solve the problem:
At first traverse left subtree then visit the root and then traverse the right
subtree.
Follow the below steps to implement the idea:
 Traverse left subtree
 Visit the root and print the data.
 Traverse the right subtree
Preorder Traversal:
Below is the idea to solve the problem:
At first visit the root then traverse left subtree and then traverse the right subtree.
Follow the below steps to implement the idea:
•Visit the root and print the data.
•Traverse left subtree
•Traverse the right subtree
Postorder Traversal:
Below is the idea to solve the problem:

At first traverse left subtree then traverse the right subtree and
then visit the root.

Follow the below steps to implement the idea:


•Traverse left subtree
•Traverse the right subtree
•Visit the root and print the data.
Insertion in Binary Search Tree (BST)
 How to Insert a value in a Binary Search Tree:
 A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start searching
for a key from the root until we hit a leaf node.
 Once a leaf node is found, the new node is added as a child of the leaf node.
 The below steps are followed while we try to insert a node into a binary search tree:
• Check the value to be inserted (say X) with the value of the current node (say val) we are in:
• If X is less than val move to the left subtree.
• Otherwise, move to the right subtree.
• Once the leaf node is reached, insert X to its right or left based on the relation between X and the leaf node’s
value.
#include <stdio.h>
#include <stdlib.h>

struct node {
int key;
struct node *left, *right;
};

// A utility function to create a new BST node


struct node* newNode(int item)
{
struct node* temp
= (struct node*)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// A utility function to do inorder traversal of BST


void inorder(struct node* root)
{
if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
 Deleting a node with both children is not so simple. Here we have to delete the node is such a way, that the
resulting tree follows the properties of a BST.
 The trick is to find the inorder successor of the node. Copy contents of the inorder successor to the node,
and delete the inorder successor.

You might also like