Skip to content

Commit 3036b5f

Browse files
committed
restructered Tree folder and add AVL tree Cpp
1 parent 197990b commit 3036b5f

File tree

7 files changed

+472
-0
lines changed

7 files changed

+472
-0
lines changed

Tree/AVL/AVL.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*AVL TREE INSERTION*/
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
struct Node{
6+
int data;
7+
int height;
8+
Node* left;
9+
Node* right;
10+
};
11+
12+
int height(Node* root){
13+
if(root==NULL)
14+
return 0;
15+
return max(height(root->left),height(root->right))+1;
16+
}
17+
18+
void preOrder(Node * root){
19+
if(root==NULL)
20+
return;
21+
cout<<root->data<<" ";
22+
preOrder(root->left);
23+
preOrder(root->right);
24+
return;
25+
}
26+
27+
Node* createNewNode(int val){
28+
Node* newNode = new Node();
29+
newNode->data = val;
30+
newNode->left = NULL;
31+
newNode->right = NULL;
32+
return newNode;
33+
}
34+
35+
Node * rotateRight(Node * root){
36+
Node * newroot = root->left;
37+
root->left = newroot->right;
38+
newroot->right = root;
39+
root->height = height(root);
40+
newroot->height = height(newroot);
41+
return newroot;
42+
}
43+
44+
Node * rotateLeft(Node * root){
45+
Node* newroot = root->right;
46+
root->right = newroot->left;
47+
newroot->left = root;
48+
root->height = height(root);
49+
newroot->height = height(newroot);
50+
return newroot;
51+
}
52+
53+
Node* insertAVL(Node* root, int val){
54+
if(root==NULL){
55+
root = createNewNode(val);
56+
}
57+
else if(val<root->data){
58+
root->left = insertAVL(root->left, val);
59+
}
60+
else if(val>root->data){
61+
root->right = insertAVL(root->right, val);
62+
}
63+
int balance = height(root->left) - height(root->right);
64+
if(balance>1){ //left height is greater
65+
if(height(root->left->left) >= height(root->left->right)){
66+
//LL imbalance
67+
root = rotateRight(root);
68+
}
69+
else{
70+
//LR imabalace
71+
root->left = rotateLeft(root->left);
72+
root = rotateRight(root);
73+
}
74+
}
75+
if(balance<-1){
76+
if(height(root->right->right) >= height(root->right->left)){
77+
//RR imbalance
78+
root = rotateLeft(root);
79+
}
80+
else{
81+
//RL imabalace
82+
root->right = rotateRight(root->right);
83+
root = rotateLeft(root);
84+
}
85+
}
86+
root->height = height(root);
87+
return root;
88+
}
89+
90+
Node* createAVL(int arr[], int n, Node* root){
91+
for(int i=0;i<n;i++){
92+
root = insertAVL(root, arr[i]);
93+
}
94+
return root;
95+
}
96+
97+
int main()
98+
{
99+
int arr[] = {40,20,10,25,30,22,50};
100+
int n = sizeof(arr)/sizeof(arr[0]);
101+
Node* root = NULL;
102+
root = createAVL(arr,n,root);
103+
preOrder(root);
104+
cout<<endl;
105+
insertAVL(root,60);
106+
preOrder(root);
107+
cout<<endl;
108+
return 0;
109+
}

Tree/Tree Traversal/BFS/C++/BFS.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<iostream>
2+
using namespace std;
3+
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
4+
int main()
5+
{
6+
int m;
7+
cout <<"Enter no of nodes(max 9):";
8+
cin >> n;
9+
cout <<"Enter no of edges:";
10+
cin >> m;
11+
cout <<"\nEnter edges starting from source node: \n";
12+
for(k=1; k<=m; k++)
13+
{
14+
cin >>i>>j;
15+
cost[i][j]=1;
16+
}
17+
cout <<"Re-enter source node to traverse from:";
18+
cin >>v;
19+
cout <<"Visitied nodes:";
20+
cout <<v<<" ";
21+
visited[v]=1;
22+
k=1;
23+
while(k<n)
24+
{
25+
for(j=1; j<=n; j++)
26+
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
27+
{
28+
visit[j]=1;
29+
qu[rare++]=j;
30+
}
31+
v=qu[front++];
32+
cout<<v <<" ";
33+
k++;
34+
visit[v]=0;
35+
visited[v]=1;
36+
}
37+
return 0;
38+
}

Tree/Tree Traversal/BFS/Python/bfs.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
Ref: https://www.geeksforgeeks.org/level-order-tree-traversal/
3+
4+
"""
5+
6+
# Recursive Python program for level order traversal of Binary Tree
7+
8+
# A node structure
9+
class Node:
10+
11+
# A utility function to create a new node
12+
def __init__(self, key):
13+
self.data = key
14+
self.left = None
15+
self.right = None
16+
17+
18+
# Function to print level order traversal of tree
19+
def printLevelOrder(root):
20+
h = height(root)
21+
for i in range(1, h+1):
22+
printGivenLevel(root, i)
23+
24+
25+
# Print nodes at a given level
26+
def printGivenLevel(root , level):
27+
if root is None:
28+
return
29+
if level == 1:
30+
print "%d" %(root.data),
31+
elif level > 1 :
32+
printGivenLevel(root.left , level-1)
33+
printGivenLevel(root.right , level-1)
34+
35+
36+
""" Compute the height of a tree--the number of nodes
37+
along the longest path from the root node down to
38+
the farthest leaf node
39+
"""
40+
def height(node):
41+
if node is None:
42+
return 0
43+
else :
44+
# Compute the height of each subtree
45+
lheight = height(node.left)
46+
rheight = height(node.right)
47+
48+
#Use the larger one
49+
if lheight > rheight :
50+
return lheight+1
51+
else:
52+
return rheight+1
53+
54+
# Driver program to test above function
55+
root = Node(1)
56+
root.left = Node(2)
57+
root.right = Node(3)
58+
root.left.left = Node(4)
59+
root.left.right = Node(5)
60+
61+
print "Level order traversal of binary tree is -"
62+
printLevelOrder(root)
63+
64+
#This code is contributed by Nikhil Kumar Singh(nickzuck_007)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// C++ program for different tree traversals
2+
#include <iostream>
3+
using namespace std;
4+
5+
/* A binary tree node has data, pointer to left child
6+
and a pointer to right child */
7+
struct Node
8+
{
9+
int data;
10+
struct Node* left, *right;
11+
Node(int data)
12+
{
13+
this->data = data;
14+
left = right = NULL;
15+
}
16+
};
17+
18+
/* Given a binary tree, print its nodes according to the
19+
"bottom-up" postorder traversal. */
20+
void printPostorder(struct Node* node)
21+
{
22+
if (node == NULL)
23+
return;
24+
25+
// first recur on left subtree
26+
printPostorder(node->left);
27+
28+
// then recur on right subtree
29+
printPostorder(node->right);
30+
31+
// now deal with the node
32+
cout << node->data << " ";
33+
}
34+
35+
/* Given a binary tree, print its nodes in inorder*/
36+
void printInorder(struct Node* node)
37+
{
38+
if (node == NULL)
39+
return;
40+
41+
/* first recur on left child */
42+
printInorder(node->left);
43+
44+
/* then print the data of node */
45+
cout << node->data << " ";
46+
47+
/* now recur on right child */
48+
printInorder(node->right);
49+
}
50+
51+
/* Given a binary tree, print its nodes in preorder*/
52+
void printPreorder(struct Node* node)
53+
{
54+
if (node == NULL)
55+
return;
56+
57+
/* first print data of node */
58+
cout << node->data << " ";
59+
60+
/* then recur on left sutree */
61+
printPreorder(node->left);
62+
63+
/* now recur on right subtree */
64+
printPreorder(node->right);
65+
}
66+
67+
/* Driver program to test above functions*/
68+
int main()
69+
{
70+
struct Node *root = new Node(1);
71+
root->left = new Node(2);
72+
root->right = new Node(3);
73+
root->left->left = new Node(4);
74+
root->left->right = new Node(5);
75+
76+
cout << "\nPreorder traversal of binary tree is \n";
77+
printPreorder(root);
78+
79+
cout << "\nInorder traversal of binary tree is \n";
80+
printInorder(root);
81+
82+
cout << "\nPostorder traversal of binary tree is \n";
83+
printPostorder(root);
84+
85+
return 0;
86+
}

Tree/Tree Traversal/DFS/C++/DFS.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
bool adj_matrix[101][101]={0};
5+
bool visited[101]={0};
6+
int n;
7+
8+
void DFS(int s)
9+
{
10+
visited[s]=1;
11+
cout<<s<<" ";
12+
for(int i=1;i<=n;i++)
13+
if(adj_matrix[s][i]==1 && !visited[i])
14+
DFS(i);
15+
}
16+
17+
int main()
18+
{
19+
cout<<"Enter number of nodes: ";
20+
cin>>n;
21+
int m;
22+
cout<<"Enter number of edges: ";
23+
cin>>m;
24+
cout<<"Enter edges starting from source node:\n";
25+
for(int i=0;i<m;i++)
26+
{
27+
int x,y;
28+
cin>>x>>y;
29+
adj_matrix[x][y]=1;
30+
31+
}
32+
int s;
33+
cout<<"Re-enter source node";
34+
cin>>s;
35+
cout<<"DFS with Source "<<s<<"\n";
36+
DFS(s);
37+
return 0;
38+
}

Tree/Tree Traversal/ReversalAlgo.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* REVERSAL ALGORITHM FOR ARRAY ROTATION */
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
/*Function to reverse arr[] from index start to end*/
7+
void rvereseArray(int arr[], int start, int end)
8+
{
9+
while (start < end) {
10+
int temp = arr[start];
11+
arr[start] = arr[end];
12+
arr[end] = temp;
13+
start++;
14+
end--;
15+
}
16+
}
17+
18+
/* Function to left rotate arr[] of size n by d */
19+
void leftRotate(int arr[], int d, int n)
20+
{
21+
if (d == 0)
22+
return;
23+
rvereseArray(arr, 0, d - 1);
24+
rvereseArray(arr, d, n - 1);
25+
rvereseArray(arr, 0, n - 1);
26+
}
27+
28+
// Function to print an array
29+
void printArray(int arr[], int size)
30+
{
31+
for (int i = 0; i < size; i++)
32+
cout << arr[i] << " ";
33+
}
34+
35+
/* Driver program to test above functions */
36+
int main()
37+
{
38+
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
39+
int n = sizeof(arr) / sizeof(arr[0]);
40+
int d = 2;
41+
42+
// in case the rotating factor is
43+
// greater than array length
44+
d = d % n;
45+
46+
// Function calling
47+
leftRotate(arr, d, n);
48+
printArray(arr, n);
49+
50+
return 0;
51+
}

0 commit comments

Comments
 (0)