BST to Linked List Level by Level
/***
4.4 Given a binary search tree, design an algorithm which creates
a linked list of all the nodes at each depth
(i.e., if you have a tree with depth D, you’ll have D linked lists).
**/
#include<iostream>
#include<vector>
using namespace std;
/***
DFS..
***/
struct node {
int val;
node *left;
node *right;
};
struct listnode {
int val;
listnode *next;
};
struct linkedlist {
listnode *entry;
};
void linkedlist_insert(node *n, linkedlist *mylist) {
listnode *lnode = new listnode();
lnode->val = n->val;
lnode->next = mylist->entry;
mylist->entry = lnode;
}
void LinkedlistFromBST(node *n, vector<linkedlist> *lists, int level) {
if(n == nullptr) return;
if(lists->size() == level) { //level not contains
linkedlist li;
li.entry = nullptr;
linkedlist_insert(n, &li);
lists->push_back(li);
} else {
linkedlist_insert(n, &((*lists)[level]));
}
int ll = level+1;
LinkedlistFromBST(n->left, lists, ll);
LinkedlistFromBST(n->right, lists, ll);
}
void main() {
node nodes[7];
nodes[0].left = &nodes[1];
nodes[0].right = &nodes[2];
nodes[1].left = &nodes[3];
nodes[1].right = &nodes[4];
nodes[2].left = &nodes[5];
nodes[2].right = &nodes[6];
for(int i=0; i<7; i++) {
nodes[i].val = i;
if (i > 2)
{
nodes[i].left = nullptr;
nodes[i].right = nullptr;
}
}
node *root = nodes;
vector<linkedlist> mylist;
linkedlist li;
listnode *ln;
LinkedlistFromBST(root, &mylist, 0);
// test
for(int i=0; i < mylist.size(); ++i) {
li = mylist.back();
mylist.pop_back();
ln = li.entry;
while (ln != nullptr)
{
cout << ln->val <<" ";
ln = ln->next;
}
cout << endl;
}
}
4.4 Given a binary search tree, design an algorithm which creates
a linked list of all the nodes at each depth
(i.e., if you have a tree with depth D, you’ll have D linked lists).
**/
#include<iostream>
#include<vector>
using namespace std;
/***
DFS..
***/
struct node {
int val;
node *left;
node *right;
};
struct listnode {
int val;
listnode *next;
};
struct linkedlist {
listnode *entry;
};
void linkedlist_insert(node *n, linkedlist *mylist) {
listnode *lnode = new listnode();
lnode->val = n->val;
lnode->next = mylist->entry;
mylist->entry = lnode;
}
void LinkedlistFromBST(node *n, vector<linkedlist> *lists, int level) {
if(n == nullptr) return;
if(lists->size() == level) { //level not contains
linkedlist li;
li.entry = nullptr;
linkedlist_insert(n, &li);
lists->push_back(li);
} else {
linkedlist_insert(n, &((*lists)[level]));
}
int ll = level+1;
LinkedlistFromBST(n->left, lists, ll);
LinkedlistFromBST(n->right, lists, ll);
}
void main() {
node nodes[7];
nodes[0].left = &nodes[1];
nodes[0].right = &nodes[2];
nodes[1].left = &nodes[3];
nodes[1].right = &nodes[4];
nodes[2].left = &nodes[5];
nodes[2].right = &nodes[6];
for(int i=0; i<7; i++) {
nodes[i].val = i;
if (i > 2)
{
nodes[i].left = nullptr;
nodes[i].right = nullptr;
}
}
node *root = nodes;
vector<linkedlist> mylist;
linkedlist li;
listnode *ln;
LinkedlistFromBST(root, &mylist, 0);
// test
for(int i=0; i < mylist.size(); ++i) {
li = mylist.back();
mylist.pop_back();
ln = li.entry;
while (ln != nullptr)
{
cout << ln->val <<" ";
ln = ln->next;
}
cout << endl;
}
}
Comments
Post a Comment