Skip to content

Commit 07d6071

Browse files
Merge pull request #676 from avi181197/master
Created tree traversal in c
2 parents 41ab20d + b344ff1 commit 07d6071

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed

jobSequencing.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Program to find the maximum profit job sequence from a given array
2+
// of jobs with deadlines and profits
3+
#include<iostream>
4+
#include<algorithm>
5+
using namespace std;
6+
7+
// A structure to represent a job
8+
struct Job
9+
{
10+
char id; // Job Id
11+
int dead; // Deadline of job
12+
int profit; // Profit if job is over before or on deadline
13+
};
14+
15+
// This function is used for sorting all jobs according to profit
16+
bool comparison(Job a, Job b)
17+
{
18+
return (a.profit > b.profit);
19+
}
20+
21+
// Returns minimum number of platforms reqquired
22+
void printJobScheduling(Job arr[], int n)
23+
{
24+
// Sort all jobs according to decreasing order of prfit
25+
sort(arr, arr+n, comparison);
26+
27+
int result[n]; // To store result (Sequence of jobs)
28+
bool slot[n]; // To keep track of free time slots
29+
30+
// Initialize all slots to be free
31+
for (int i=0; i<n; i++)
32+
slot[i] = false;
33+
34+
// Iterate through all given jobs
35+
for (int i=0; i<n; i++)
36+
{
37+
// Find a free slot for this job (Note that we start
38+
// from the last possible slot)
39+
for (int j=min(n, arr[i].dead)-1; j>=0; j--)
40+
{
41+
// Free slot found
42+
if (slot[j]==false)
43+
{
44+
result[j] = i; // Add this job to result
45+
slot[j] = true; // Make this slot occupied
46+
break;
47+
}
48+
}
49+
}
50+
51+
// Print the result
52+
for (int i=0; i<n; i++)
53+
if (slot[i])
54+
cout << arr[result[i]].id << " ";
55+
}
56+
57+
// Driver program to test methods
58+
int main()
59+
{
60+
Job arr[] = { {'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27},
61+
{'d', 1, 25}, {'e', 3, 15}};
62+
int n = sizeof(arr)/sizeof(arr[0]);
63+
cout << "Following is maximum profit sequence of jobsn";
64+
printJobScheduling(arr, n);
65+
return 0;
66+
}
67+
68+
Output:
69+
Followi

tree traversal using c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//Binary tree traversal:
2+
3+
#include <stdio.h>
4+
#include <conio.h>
5+
#include <malloc.h>
6+
struct node
7+
{
8+
struct node *left;
9+
int data;
10+
struct node *right;
11+
};
12+
13+
void main()
14+
{
15+
void insert(struct node **,int);
16+
void inorder(struct node *);
17+
void postorder(struct node *);
18+
void preorder(struct node *);
19+
20+
struct node *ptr;
21+
int no,i,num;
22+
23+
ptr = NULL;
24+
ptr->data=NULL;
25+
clrscr();
26+
27+
printf("\nProgram for Tree Traversal\n");
28+
printf("Enter the number of nodes to add to the tree.<BR>\n");
29+
scanf("%d",&no);
30+
31+
for(i=0;i<no;i++)
32+
{
33+
printf("Enter the item\n");
34+
scanf("%d",&num);
35+
insert(&ptr,num);
36+
}
37+
38+
//getch();
39+
printf("\nINORDER TRAVERSAL\n");
40+
inorder(ptr);
41+
42+
printf("\nPREORDER TRAVERSAL\n");
43+
preorder(ptr);
44+
45+
printf("\nPOSTORDER TRAVERSAL\n");
46+
postorder(ptr);
47+
48+
getch();
49+
}
50+
51+
void insert(struct node **p,int num)
52+
{
53+
if((*p)==NULL)
54+
{
55+
printf("Leaf node created.");
56+
(*p)=malloc(sizeof(struct node));
57+
(*p)->left = NULL;
58+
(*p)->right = NULL;
59+
(*p)->data = num;
60+
return;
61+
}
62+
else
63+
{
64+
if(num==(*p)->data)
65+
{
66+
printf("\nREPEATED ENTRY ERROR VALUE REJECTED\n");
67+
return;
68+
}
69+
if(num<(*p)->data)
70+
{
71+
printf("\nDirected to left link.\n");
72+
insert(&((*p)->left),num);
73+
}
74+
else
75+
{
76+
printf("Directed to right link.\n");
77+
insert(&((*p)->right),num);
78+
}
79+
}
80+
return;
81+
}
82+
83+
void inorder(struct node *p)
84+
{
85+
if(p!=NULL)
86+
{
87+
inorder(p->left);
88+
printf("\nData :%d",p->data);
89+
inorder(p->right);
90+
}
91+
else
92+
return;
93+
}
94+
95+
void preorder(struct node *p)
96+
{
97+
if(p!=NULL)
98+
{
99+
printf("\nData :%d",p->data);
100+
preorder(p->left);
101+
preorder(p->right);
102+
}
103+
else
104+
return;
105+
}
106+
107+
void postorder(struct node *p)
108+
{
109+
if(p!=NULL)
110+
{
111+
postorder(p->left);
112+
postorder(p->right);
113+
printf("\nData :%d",p->data);
114+
}
115+
else
116+
return;
117+
}
118+

0 commit comments

Comments
 (0)