Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created February 10, 2017 05:06
Show Gist options
  • Save fpdjsns/4a1960b5e22a4a7d62e02454f4c0e362 to your computer and use it in GitHub Desktop.
Save fpdjsns/4a1960b5e22a4a7d62e02454f4c0e362 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
#define node_num 26
//node struct
typedef struct node
{
char alphabet; //alphabet
struct node* left; //left child node
struct node* right; //right child node
}node;
//set up information of node
void set_node(node* n, char alphabet, node* left, node* right)
{
n->alphabet = alphabet;
n->left = left;
n->right = right;
}
//preorder
void preorder(node* root)
{
printf("%c", root->alphabet); //print node
if(root->left) preorder(root->left); //left child
if (root->right) preorder(root->right); //right child
}
//inorder
void inorder(node* root)
{
if (root->left) inorder(root->left); //left child
printf("%c", root->alphabet); //print node
if (root->right) inorder(root->right); //right child
}
//postorder
void postorder(node* root)
{
if (root->left) postorder(root->left); //left child
if (root->right) postorder(root->right); //right child
printf("%c", root->alphabet); //print node
}
int main()
{
int i, num;
char me, left, right;
node* arr_node[node_num];
scanf("%d", &num);
for (i = 0; i < num; i++)
arr_node[i] = (node*)malloc(sizeof(node));
for (i = 0; i < num; i++)
{
//arr_node[i] = (node*)malloc(sizeof(node));
getchar();
scanf("%c %c %c", &me, &left, &right);
if (left == '.')
{
if(right=='.')
set_node(arr_node[me-'A'], me, NULL, NULL);
else
set_node(arr_node[me - 'A'], me, NULL, arr_node[right-'A']);
}
else if (right == '.')
set_node(arr_node[me - 'A'], me, arr_node[left-'A'], NULL);
else
set_node(arr_node[me - 'A'], me, arr_node[left-'A'], arr_node[right-'A']);
}
preorder(arr_node[0]); printf("\n");
inorder(arr_node[0]); printf("\n");
postorder(arr_node[0]); printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment