Created
December 31, 2018 14:19
-
-
Save kerolloz/e2043e3cb1b917b13e43faf13914eedb to your computer and use it in GitHub Desktop.
convert function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct tree_node{ | |
unsigned int data; | |
tree_node *left = NULL; | |
tree_node *right = NULL; | |
}; | |
struct list_node{ | |
int data; | |
list_node *next; | |
}; | |
void print_list(list_node *head){ | |
if(head == NULL) cout << "EMPTY LIST"; | |
while(head){ | |
cout << head->data << " "; | |
head = head->next; | |
} | |
cout << endl; | |
} | |
list_node* create_list_node(int value){ | |
list_node *new1 = new list_node; | |
new1->data = value; | |
new1->next = NULL; | |
return new1; | |
} | |
void append_at_list(list_node *&head, int value){ | |
if(!head){ // if the head is null | |
head = create_list_node(value); | |
return; | |
} | |
list_node *temp = head; | |
while(temp->next) | |
temp = temp->next; | |
temp->next = create_list_node(value); | |
} | |
tree_node* create_tree_node(unsigned int x){ | |
tree_node *new1 = new tree_node; | |
new1->data = x; | |
new1->left = NULL; | |
new1->right = NULL; | |
return new1; | |
} | |
void insert_tree_node(tree_node *& t, unsigned int x){ | |
tree_node *temp1 = t, *temp2 = NULL; | |
tree_node *new1 = create_tree_node(x); | |
if(t == NULL){ | |
t = new1; | |
} | |
else{ | |
while(temp1 != NULL){ | |
temp2 = temp1; | |
if(x <= temp1->data){ | |
temp1 = temp1->left; | |
} | |
else{ | |
temp1 = temp1->right; | |
} | |
} | |
if(x <= temp2->data) | |
temp2->left = new1; | |
else | |
temp2->right = new1; | |
} | |
} | |
tree_node* convert(list_node *& L){ // L is the head of the list | |
// L (will be destroyed) so we should make all nodes equal null; | |
// but after taking the data from it | |
tree_node *root = NULL; | |
while(L){ | |
int data = L->data; // take the data in the list node | |
insert_tree_node(root, data); // put the data in a tree node | |
// now it's time to get rid of the list node | |
// BUT first we should keep the next node in a temporary variable | |
list_node *next_node = L->next; | |
delete L; // now we can delete the located space for the current node | |
L = next_node; // let L equal the next_node; | |
} | |
return root; | |
} | |
void printInorder(tree_node *t){ | |
if(t == NULL) | |
return; | |
if(t != NULL){ | |
printInorder(t->left); | |
cout << t->data << " " ; | |
printInorder(t->right); | |
} | |
} | |
int main() | |
{ | |
list_node *head = NULL; | |
append_at_list(head, 5); | |
append_at_list(head, 1); | |
append_at_list(head, 9); | |
append_at_list(head, 2); | |
append_at_list(head, 4); | |
append_at_list(head, 6); | |
append_at_list(head, 3); | |
print_list(head); | |
tree_node *root = convert(head); | |
// this should print empty list because we destroyed all the nodes | |
// (i.e.: delete them -- freed there space in memory) | |
print_list(head); | |
printInorder(root); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment