Professional Documents
Culture Documents
Document 3 Final
Document 3 Final
Document 3 Final
I. Tree
Definition: A tree is a nonlinear hierarchical data structure that consists of
nodes connected by edges.
Terminologies:
o Node: an entity that contains a key or value and pointers to its
child nodes.The last nodes of each path are called leaf nodes or
external nodes that do not contain a link/pointer to child nodes.
The node having at least a child node is called an internal node.
o Edge: the link between any two nodes
o Root: the topmost node of a tree
o Height of a node: number of edges from the node to the
deepest leaf
o Depth of a Node: The depth of a node is the number of edges
from the root to the node.
o Height of a Tree: height of the root node or the depth of the
deepest node.
o Degree of a Node: total number of branches of that node.
o Forest: collection of disjoint trees is called a forest.
Types:
o BINARY TREE: a tree data structure in which each parent
node can have at most two children. Each node of a binary tree
consists of three items: data item, address of left child, address
of right child.
o BINARY SEARCH TREE: A binary search tree is
distinguished from a regular binary tree by three key properties.
Firstly, in a binary search tree, all nodes in the left subtree are
strictly smaller than the root node. Secondly, all nodes in the
right subtree are strictly greater than the root node. Lastly, these
properties apply recursively to both the left and right subtrees
of every node, ensuring that each subtree is itself a valid binary
search tree.
Code:
Code Output
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
return root;
}
void inorderTraversal(TreeNode*
root) {
if (root != NULL) {
inorderTraversal(root->left);
cout << root->val << " ";
inorderTraversal(root->right);
}
}
int main() {
TreeNode* root = NULL;
return 0;
}
II. Graphs
Definition: a collection of nodes that have data and are connected to other
nodes.
Terminologies:
o Adjacency: A vertex is said to be adjacent to another vertex if
there is an edge connecting them. Vertices 2 and 3 are not
adjacent because there is no edge between them.
o Path: A sequence of edges that allows you to go from vertex A
to vertex B is called a path. 0-1, 1-2 and 0-2 are paths from
vertex 0 to vertex 2.
o Directed Graph: A graph in which an edge (u,v) doesn't
necessarily mean that there is an edge (v, u) as well. The edges
in such a graph are represented by arrows to show the direction
of the edge.
Graph is represented in two ways:
Code:
Code Output
#include <iostream>
#include <vector>
using namespace std;
class Graph {
public:
Graph(int vertices);
void addEdge(int v, int w);
void printGraph();
private:
int vertices;
vector<vector<int> >
adjacencyList;
};
Graph::Graph(int vertices) {
this->vertices = vertices;
adjacencyList.resize(vertices);
}
void Graph::printGraph() {
for (int i = 0; i < vertices; ++i) {
cout << "Vertex " << i << ": ";
for (vector<int>::iterator it =
adjacencyList[i].begin(); it !=
adjacencyList[i].end(); ++it) {
cout << *it << " ";
}
cout << endl;
}
}
int main() {
int vertices, edges;
cout << "Enter the number of
vertices: ";
cin >> vertices;
Graph graph(vertices);
III. Maps
Definition: Map data structure (also known as a dictionary, associative array,
or hash map) is defined as a data structure that stores a collection of key-value
pairs, where each key is associated with a single value.
Types:
o Hash Map: a data structure that uses a hash function to map
keys to indices in an array.
o Tree Map: a data structure that uses a hash function to map
keys to indices in an array.
o Linked Hash Map: a type of map that maintains a doubly-
linked list of the entries in the map, in the order in which they
were inserted.
o Trie Map: type of map that is used to store a set of strings,
where each node in the tree represents a prefix of one or more
strings.
o Bloom Filter Map: a type of map that uses a bloom filter, a
probabilistic data structure, to determine whether a key is
present in the map or not.
Code:
Code Output
#include <iostream>
#include <map>
using namespace std;
void demonstrateMap() {
map<int, string> myMap;
myMap[1] = "One";
myMap[2] = "Two";
myMap[3] = "Three";
myMap[4] = "Four";
myMap[5] = "Five";
int main() {
demonstrateMap();
return 0;
}
IV. Sets
Definition: Sets is a data structure that stores unique elements of the same type
in a sorted order. Each value is a key, which means that we access each value
using the value itself.
Code:
Code Output
#include <iostream>
#include <set>
using namespace std;
void demonstrateSet() {
set<int> mySet;
mySet.insert(5);
mySet.insert(2);
mySet.insert(8);
mySet.insert(1);
mySet.insert(10);
for (set<int>::iterator it =
mySet.begin(); it != mySet.end(); +
+it) {
cout << *it << endl;
}
}
int main() {
demonstrateSet();
return 0;
}
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int value) :
data(value), left(NULL),
right(NULL) {}
};
TreeNode* createNode(int
data) {
return new TreeNode(data);
}
void insertLeft(TreeNode*
parent, int data) {
if (parent) {
parent->left =
createNode(data);
cout << "Inserted: " <<
data << " as the left child of "
<< parent->data << endl;
}
}
void insertRight(TreeNode*
parent, int data) {
if (parent) {
parent->right =
createNode(data);
cout << "Inserted: " <<
data << " as the right child of "
<< parent->data << endl;
}
}
int main() {
TreeNode* root =
createNode(7);
cout << "Inserted: " << root-
>data << " as the root" << endl;
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);
insertRight(root->left, 5);
return 0;
}
struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int val) :
data(val), left(NULL),
right(NULL) {}
};
TreeNode*
insertNode(TreeNode* root,
int key) {
if (root == NULL) {
return new
TreeNode(key);
}
return root;
}
TreeNode*
findMin(TreeNode* node) {
while (node->left != NULL)
{
node = node->left;
}
return node;
}
TreeNode*
deleteNode(TreeNode* root,
int key) {
if (root == NULL) {
return root;
}
TreeNode* temp =
findMin(root->right);
root->data = temp->data;
root->right =
deleteNode(root->right, temp-
>data);
}
return root;
}
int main() {
TreeNode* root = NULL;
return 0;
}
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int value) :
data(value), left(NULL),
right(NULL) {}
};
void inorderTraversal(TreeNode*
root) {
if (root) {
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root-
>right);
}
}
int main() {
TreeNode* root = new
TreeNode(4);
root->left = new TreeNode(2);
root->right = new TreeNode(6);
root->left->left = new
TreeNode(1);
root->left->right = new
TreeNode(3);
root->right->left = new
TreeNode(5);
root->right->right = new
TreeNode(7);
return 0;
}
References:
Tree Data Structure (programiz.com)
What is Graph in Data Structure & Types of Graph? (simplilearn.com)
Graph Data Structure (programiz.com)
Introduction to Map – Data Structure and Algorithm Tutorials - GeeksforGeeks
Sets in C++ Explained with Examples | Udacity