Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added code for lateral traversal in Binary tree #6768

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
116 changes: 116 additions & 0 deletions code/Binary_Tree/Lateral_Traversal_Binary_Tree.cpp
@@ -0,0 +1,116 @@
#include <iostream>
using namespace std;

/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct Node {
int data;
struct Node *left, *right;
};

// Utility function to create a new tree node
Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = temp->right = nullptr;
return temp;
}

// A simple function to print leaf nodes of a binary tree
void printLeaves(Node* root)
{
if (root == nullptr)
return;

printLeaves(root->left);

// Print it if it is a leaf node
if (!(root->left) && !(root->right))
cout << root->data << " ";

printLeaves(root->right);
}

// A function to print all left boundary nodes, except a
// leaf node. Print the nodes in TOP DOWN manner
void printBoundaryLeft(Node* root)
{
if (root == nullptr)
return;

if (root->left) {

// to ensure top down order, print the node
// before calling itself for left subtree
cout << root->data << " ";
printBoundaryLeft(root->left);
}
else if (root->right) {
cout << root->data << " ";
printBoundaryLeft(root->right);
}
// do nothing if it is a leaf node, this way we avoid
// duplicates in output
}

// A function to print all right boundary nodes, except a
// leaf node Print the nodes in BOTTOM UP manner
void printBoundaryRight(Node* root)
{
if (root == nullptr)
return;

if (root->right) {
// to ensure bottom up order, first call for right
// subtree, then print this node
printBoundaryRight(root->right);
cout << root->data << " ";
}
else if (root->left) {
printBoundaryRight(root->left);
cout << root->data << " ";
}
// do nothing if it is a leaf node, this way we avoid
// duplicates in output
}

// A function to do boundary traversal of a given binary
// tree
void printBoundary(Node* root)
{
if (root == nullptr)
return;

cout << root->data << " ";

// Print the left boundary in top-down manner.
printBoundaryLeft(root->left);

// Print all leaf nodes
printLeaves(root->left);
printLeaves(root->right);

// Print the right boundary in bottom-up manner
printBoundaryRight(root->right);
}

// Driver program to test above functions
int main()
{
// Let us construct the tree given in the above diagram
Node* root = newNode(20);
root->left = newNode(8);
root->left->left = newNode(4);
root->left->right = newNode(12);
root->left->right->left = newNode(10);
root->left->right->right = newNode(14);
root->right = newNode(22);
root->right->right = newNode(25);

printBoundary(root);

return 0;
}


92 changes: 92 additions & 0 deletions code/data_structures/src/binary_heap/binary_heap.dart
@@ -0,0 +1,92 @@
class BinaryHeap {
List<int> _heap ;

BinaryHeap() {
_heap = [];
}

void insert(int value) {
_heap.add(value);
_heapifyUp();
}

int extractMin() {
if (_heap.isEmpty) {
throw Exception("Heap is empty");
}

final min = _heap[0];
final last = _heap.removeLast();

if (_heap.isNotEmpty) {
_heap[0] = last;
_heapifyDown();
}

return min;
}

void _heapifyUp() {
int index = _heap.length - 1;

while (index > 0) {
final parentIndex = (index - 1) ~/ 2;
if (_heap[index] < _heap[parentIndex]) {
final temp = _heap[index];
_heap[index] = _heap[parentIndex];
_heap[parentIndex] = temp;
index = parentIndex;
} else {
break;
}
}
}

void _heapifyDown() {
int index = 0;
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;

while (leftChild < _heap.length) {
int smallerChildIndex = leftChild;
if (rightChild < _heap.length && _heap[rightChild] < _heap[leftChild]) {
smallerChildIndex = rightChild;
}

if (_heap[index] <= _heap[smallerChildIndex]) {
break;
}

final temp = _heap[index];
_heap[index] = _heap[smallerChildIndex];
_heap[smallerChildIndex] = temp;

index = smallerChildIndex;
leftChild = 2 * index + 1;
rightChild = 2 * index + 2;
}
}

bool isEmpty() {
return _heap.isEmpty;
}

int size() {
return _heap.length;
}
}

void main() {
final heap = BinaryHeap();

heap.insert(5);
heap.insert(10);
heap.insert(3);
heap.insert(7);

print("Heap size: ${heap.size()}");

while (!heap.isEmpty()) {
print("Min element: ${heap.extractMin()}");
}
}
101 changes: 101 additions & 0 deletions code/greedy_algorithms/src/job_sequencing/job_sequencing.java
@@ -0,0 +1,101 @@
// Java implementation of above approach

// Program to find the maximum profit
// job sequence from a given array
// of jobs with deadlines and profits
import java.util.*;

public class GFG {

// a class to represent job
static class Job {
char job_id;
int deadline;
int profit;
Job(char job_id, int deadline, int profit)
{
this.deadline = deadline;
this.job_id = job_id;
this.profit = profit;
}
}

static void printJobScheduling(ArrayList<Job> arr)
{
int n = arr.size();

// sorting the array on the
// basis of their deadlines
Collections.sort(arr, (a, b) -> {
return a.deadline - b.deadline;
});

// initialise the result array and maxHeap
ArrayList<Job> result = new ArrayList<>();
PriorityQueue<Job> maxHeap = new PriorityQueue<>(
(a, b) -> { return b.profit - a.profit; });

// starting the iteration from the end
for (int i = n - 1; i > -1; i--) {
int slot_available;

// calculate slots between two deadlines
if (i == 0) {
slot_available = arr.get(i).deadline;
}
else {
slot_available = arr.get(i).deadline
- arr.get(i - 1).deadline;
}

// include the profit of job(as priority),
// deadline and job_id in maxHeap
maxHeap.add(arr.get(i));

while (slot_available > 0
&& maxHeap.size() > 0) {

// get the job with max_profit
Job job = maxHeap.remove();

// reduce the slots
slot_available--;

// include the job in the result array
result.add(job);
}
}

// jobs included might be shuffled
// sort the result array by their deadlines
Collections.sort(result, (a, b) -> {
return a.deadline - b.deadline;
});

for (Job job : result) {
System.out.print(job.job_id + " ");
}

System.out.println();
}

// Driver's Code
public static void main(String[] args)
{
ArrayList<Job> arr = new ArrayList<Job>();

arr.add(new Job('a', 2, 100));
arr.add(new Job('b', 1, 19));
arr.add(new Job('c', 2, 27));
arr.add(new Job('d', 1, 25));
arr.add(new Job('e', 3, 15));

System.out.println("Following is maximum "
+ "profit sequence of jobs");

// Function call
printJobScheduling(arr);
}
}