Skip to content

Commit a6181b8

Browse files
committed
Day-10- BST predeccesor and successor algo + stack implementation
1 parent ee46343 commit a6181b8

File tree

5 files changed

+236
-40
lines changed

5 files changed

+236
-40
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ include_directories(".")
99
file(GLOB SOURCES "./*/*.cpp")
1010

1111
#compile flags
12-
set(CMAKE_CXX_FLAGS "-Wall -std=c++11")
12+
set(CMAKE_CXX_FLAGS "-Wall -std=c++11 -g -pedantic")
1313

1414
foreach( sourcefile ${SOURCES} )
1515
get_filename_component( demo_name ${sourcefile} NAME_WE )

cracking_the_coding_interview_problems/1-2-old-reverseString.cpp

-39
This file was deleted.

include/stack.h

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/***
2+
* _ __ ___ _
3+
* | |/ /___ ___ _ __ / __|__ _| |_ __
4+
* | ' </ -_) -_) '_ \ | (__/ _` | | ' \
5+
* |_|\_\___\___| .__/ \___\__,_|_|_|_|_|
6+
* |_|
7+
* _
8+
* __ _ _ _ __| |
9+
* / _` | ' \/ _` |
10+
* \__,_|_||_\__,_|
11+
*
12+
* _ _ _ _ _ _
13+
* | | ___ __ _ _ _ _ _ /_\ | |__ _ ___ _ _(_) |_| |_ _ __ ___
14+
* | |__/ -_) _` | '_| ' \ / _ \| / _` / _ \ '_| | _| ' \| ' \(_-<
15+
* |____\___\__,_|_| |_||_| /_/ \_\_\__, \___/_| |_|\__|_||_|_|_|_/__/
16+
* |___/
17+
* Yeah! Ravi let's do it!
18+
*/
19+
20+
#ifndef STACK_H
21+
#define STACK_H
22+
23+
#include <iostream>
24+
#include <exception>
25+
#include <cstdint>
26+
27+
namespace algo {
28+
const uint32_t defaultCapacity = 500;
29+
30+
template <typename T = uintptr_t>
31+
class Stack {
32+
public:
33+
Stack( uint32_t capacity = defaultCapacity )
34+
: _capacity{ capacity }, _size{ 0 }, _elements{ new T[_capacity] }
35+
{
36+
}
37+
38+
Stack( const Stack & st )
39+
: _capacity{ st.capacity }, _size{ st._size }, _elements{ new T[_capacity] }
40+
{
41+
for(uint32_t i = 0; i < _capacity; ++i) {
42+
_elements[i] = st._elements[i];
43+
}
44+
}
45+
46+
bool empty()
47+
{
48+
return (_size == 0 );
49+
}
50+
51+
void pop()
52+
{
53+
if (empty())
54+
return;
55+
_size--;
56+
}
57+
58+
void push( const T & obj ) {
59+
if ( _size == _capacity )
60+
throw stack_out_of_bound_exception;
61+
_elements[_size++] = obj;
62+
}
63+
64+
const T & top()
65+
{
66+
if (empty())
67+
throw stack_empty_exception;
68+
return _elements[_size - 1];
69+
}
70+
71+
uint32_t size()
72+
{
73+
return _size;
74+
}
75+
76+
void print(std::ostream & out = std::cout)
77+
{
78+
for (int i = _size - 1; i >= 0; --i) {
79+
out << _elements[i] << " ";
80+
}
81+
out << std::endl;
82+
}
83+
84+
85+
private:
86+
uint32_t _capacity;
87+
uint32_t _size;
88+
T * _elements;
89+
Stack & operator=(const Stack & st);
90+
91+
class StackEmptyException : public std::exception {
92+
virtual const char * what() const throw()
93+
{
94+
return "Stack is empty";
95+
}
96+
} stack_empty_exception;
97+
98+
class StackOutofBoundException : public std::exception {
99+
virtual const char * what() const throw()
100+
{
101+
return "Stack Index out of bound";
102+
}
103+
} stack_out_of_bound_exception ;
104+
105+
}; // end of class stack
106+
107+
} // end of namespace algo
108+
#endif

stack_problems/stackDemo.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <stack.h>
3+
4+
int main()
5+
{
6+
algo::Stack<double> st{10};
7+
for ( int i = 0; i < 10; ++i) {
8+
st.push( i );
9+
}
10+
st.print();
11+
while (!st.empty()) {
12+
std::cout << st.top() << " ";
13+
st.pop();
14+
}
15+
while (st.size() > 0) {
16+
std::cout << st.top() << " ";
17+
st.pop();
18+
}
19+
20+
std::cout << std::endl;
21+
return 0;
22+
}
+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Goal : To find successor and predecessor ( if exists ) of a key in Binary Search Tree
3+
*
4+
*/
5+
6+
#include <iostream>
7+
#include <generic.h>
8+
9+
/* Node of the tree */
10+
struct Node {
11+
int data;
12+
Node *left;
13+
Node *right;
14+
};
15+
16+
// Helper function to return a new node
17+
Node* newNode( const int & data )
18+
{
19+
Node *node = new Node;
20+
node->data = data;
21+
node->left = node->right = nullptr;
22+
return node;
23+
}
24+
25+
//insert to a binary tree
26+
void insert( Node * & node, const int & data )
27+
{
28+
if ( node == nullptr ) {
29+
node = newNode(data);
30+
return;
31+
}
32+
33+
if ( data < node->data ) {
34+
insert( node->left, data);
35+
} else {
36+
insert( node->right, data);
37+
}
38+
}
39+
40+
// print inorder traversal of the tree
41+
void inorder( Node * node)
42+
{
43+
if ( node != nullptr )
44+
{
45+
inorder(node->left);
46+
std::cout << node->data << " ";
47+
inorder(node->right);
48+
}
49+
}
50+
51+
void predecessorSuccessor( Node *node, Node * &pred, Node * & succ, int key )
52+
{
53+
if ( node == nullptr )
54+
return;
55+
56+
if ( node->data == key )
57+
{
58+
if (node->left) {
59+
Node *temp = node->left;
60+
while (temp->right)
61+
temp = temp->right;
62+
pred = temp;
63+
}
64+
65+
if (node->right) {
66+
Node * temp = node->right;
67+
while (temp->left)
68+
temp = temp->left;
69+
succ = temp;
70+
}
71+
} else if ( key < node->data ) {
72+
succ = node;
73+
predecessorSuccessor( node->left, pred, succ, key );
74+
} else {
75+
pred = node;
76+
predecessorSuccessor( node->right, pred, succ, key );
77+
}
78+
}
79+
80+
int main()
81+
{
82+
Node * root = nullptr;
83+
int key = 20;
84+
for ( int i = 0; i < 10; ++i) {
85+
insert( root, algo::random_range(10, 40));
86+
}
87+
88+
Node *pred = nullptr, *succ = nullptr;
89+
//print the inorder of created tree
90+
std::cout << "Tree's inorder traversal:\n";
91+
inorder(root);
92+
std::cout << "Finding predecessor and successor of key " << key << " (if exists):\n";
93+
predecessorSuccessor(root, pred, succ, key);
94+
if (pred != nullptr ) {
95+
std::cout << "Predeccessor is " << pred->data << std::endl;
96+
} else {
97+
std::cout << "No predecessor\n";
98+
}
99+
if (succ != nullptr) {
100+
std::cout << "Successor is " << succ->data << std::endl;
101+
} else {
102+
std::cout << "No successor\n";
103+
}
104+
return 0;
105+
}

0 commit comments

Comments
 (0)