Created
December 14, 2013 11:22
-
-
Save VardanGrigoryan/7958104 to your computer and use it in GitHub Desktop.
Տրված է երկուական փնտրման ծառ։ Տպել ծառը մակարդակ առ մակարդակ (level order), ալգորիթմի բարդություն է O(n):
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> | |
#include <queue> | |
#include <stdlib.h> | |
struct node | |
{ | |
struct node* left; | |
struct node* rigth; | |
int data; | |
}; | |
struct node* add_node(int d) | |
{ | |
struct node* n = (struct node*) malloc(sizeof(struct node)); | |
n->data = d; | |
n->left = 0; | |
n->rigth = 0; | |
} | |
void print_level_order(struct node* r) | |
{ | |
struct node* t = r; | |
std::queue<struct node*> q; | |
q.push(t); | |
while(t != 0 && q.size() != 0) | |
{ | |
t = q.front(); | |
q.pop(); | |
std::cout << t->data << " "; | |
if(t->left != 0) | |
{ | |
q.push(t->left); | |
} | |
if(t->rigth != 0) | |
{ | |
q.push(t->rigth); | |
} | |
} | |
} | |
int main() | |
{ | |
struct node* r = add_node(4); | |
r->left = add_node(2); | |
r->rigth = add_node(5); | |
r->left->left = add_node(1); | |
r->left->rigth = add_node(3); | |
r->rigth = add_node(5); | |
r->rigth->rigth = add_node(6); | |
print_level_order(r); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment