Skip to content

Instantly share code, notes, and snippets.

@axsddlr
Last active May 21, 2018 00:33
Show Gist options
  • Save axsddlr/9e9e301b6cb7a6c8fe1692f0858b3e6a to your computer and use it in GitHub Desktop.
Save axsddlr/9e9e301b6cb7a6c8fe1692f0858b3e6a to your computer and use it in GitHub Desktop.
mp4-proj created by Ayysir - https://repl.it/@Ayysir/mp4-proj
asaddler@debian9VM:~$ g++ mp4-proj.cpp -o mp4
asaddler@debian9VM:~$ ./mp4
> 7 3 10 2 5 8 11 4 -1
> > > > > > > >
2
3
4
5
7
8
10
11
----------------
7
3
10
2
5
8
11
4
asaddler@debian9VM:~$
/*
* Andre Saddler
* CS 341 - Mon & Tues - 10am-11:50am
* Machine Program #4
* This Program is to visit a Binary Tree in Preorder and Levelorder
*/
#include <iostream>
#include <cassert>
using namespace std;
struct tnode
{
int item;
tnode *left, *right;
tnode(int Item, tnode* Left, tnode* Right)
{
item = Item;
left = Left;
right = Right;
}
};
struct qnode
{
tnode* node;
struct qnode* next;
};
class queue
{
private:
qnode* head;
public:
queue()
{
head = NULL;
}
void inQueue(qnode* n)
{
qnode* temp = new qnode;
temp->node = n->node;
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
qnode* t = head;
while (t->next)
t = t->next;
t->next = temp;
}
}
qnode* Dqueue()
{
qnode* t = head;
head = head->next;
return t;
}
bool isEmpty()
{
if (head == NULL)
return true;
return false;
}
};
struct snode
{
int counter;
tnode* node;
snode(int c = 0, tnode* n = 0)
{
counter = c;
node = n;
}
};
class stack
{
private:
enum
{
STACK_SIZE = 128
};
snode _stack[STACK_SIZE];
int _top;
public:
stack()
{
_top = 0;
}
void push(snode);
snode pop();
snode top()
{
assert(_top > 0);
return _stack[_top - 1];
}
bool is_empty()
{
return !_top;
}
};
void stack::push(snode sval)
{
assert(_top < STACK_SIZE);
_stack[_top++] = sval;
}
snode stack::pop()
{
assert(_top > 0);
return _stack[--_top];
}
tnode* buildTree()
{
tnode *tree = 0, *prev, *curr, *trptr;
int ival;
cout << "> ";
cin >> ival;
while (ival > 0)
{
tnode* tptr = new tnode(ival, 0, 0);
if (!tree)
{
tree = tptr;
}
else
{
curr = tree;
while (curr)
{
prev = curr;
if (ival < curr->item)
curr = curr->left;
else
curr = curr->right;
}
if (ival < prev->item)
prev->left = tptr;
else
prev->right = tptr;
}
cout << "> ";
cin >> ival;
}
return tree;
}
void visit(tnode* t); // prototyping
void inorder(tnode* tree); // prototyping
void levelorder(tnode* tree); // prototyping
int main()
{
tnode* tree = buildTree();
inorder(tree);
cout << "----------------\n";
levelorder(tree);
}
void levelorder(tnode* tree)
{
if (tree == NULL)
return;
queue q;
qnode* nd;
tnode* t = tree;
if (t)
visit(t);
nd->node = t->left;
q.inQueue(nd);
nd->node = t->right;
q.inQueue(nd);
while (!q.isEmpty())
{
nd = q.Dqueue();
t = nd->node;
visit(t);
if (t->left)
{
nd->node = t->left;
q.inQueue(nd);
}
if (t->right)
{
nd->node = t->right;
q.inQueue(nd);
}
}
}
void inorder(tnode* tree)
{
stack st;
struct snode nd;
nd.node = tree;
st.push(nd); // push to stack
tnode* t = tree;
t = t->left;
while (1)
{
if (t != NULL)
{
struct snode nd;
nd.node = t;
st.push(nd);
t = t->left;
}
else if (!st.is_empty())
{
struct snode nd = st.top();
t = nd.node;
st.pop();
visit(t);
t = t->right;
}
else
return;
}
}
void visit(tnode* t)
{
cout << t->item << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment