#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef struct Node
{
int val;
struct Node* left;
struct Node* right;
}Node;
typedef struct Tree
{
Node* root;
int len;
}Tree;
Node* initNode(int val)
{
Node* n = (Node*)malloc(sizeof(Node));
n->val = val;
n->right = NULL;
n->left = NULL;
return n;
}
void freeNode(Node* n)
{
if (!n) return;
free(n);
return;
}
Tree* initTree()
{
Tree* t = (Tree*)malloc(sizeof(Tree));
t->root = NULL;
t->len = 0;
return t;
}
//Node* insertNode(Node* root,int val)
//{
// if (!root)
// {
// Node* n = initNode(val);
// return n;
// }
// if (val > root->val)
// root->right=insertNode(root->right, val);
// else
// root->left = insertNode(root->left, val);
// return root;
//}
void insertNode(Node** addr, int val)
{
if (!(*addr))
{
*addr= initNode(val);
return;
}
if (val > (*addr)->val)
insertNode(&((*addr)->right), val);
else
insertNode(&((*addr)->left), val);
return;
}
void insertTree(Tree* t, int val)
{
if (!t) return;
insertNode(&(t->root), val);
t->len++;
}
void preOrderTrav(Node* root)
{
if (!root) return;
printf("%d,", root->val);
preOrderTrav(root->left);
preOrderTrav(root->right);
return;
}
void inOrderTrav(Node* root)
{
if (!root) return;
inOrderTrav(root->left);
printf("%d,", root->val);
inOrderTrav(root->right);
return;
}
void postOrderTrav(Node* root)
{
if (!root) return;
postOrderTrav(root->left);
postOrderTrav(root->right);
printf("%d,", root->val);
return;
}
void freeAll(Node* root)
{
if (!root) return;
freeAll(root->left);
freeAll(root->right);
freeNode(root);
}
void preOrderTree(Tree* t)
{
if (!t) return;
printf("Pre :[");
preOrderTrav(t->root);
printf("]\n");
}
void inOrderTree(Tree* t)
{
if (!t) return;
printf("in :[");
inOrderTrav(t->root);
printf("]\n");
}
void postOrderTree(Tree* t)
{
if (!t) return;
printf("post:[");
postOrderTrav(t->root);
printf("]\n");
}
void outputTableNode(Node *root)
{
if (!root) return;
printf("%d", root->val);
if (!root->left && !root->right) return;
printf("(");
outputTableNode(root->left);
printf(",");
outputTableNode(root->right);
printf(")");
return;
}
void outputTableTree(Tree* t)
{
if (!t) return;
printf("Table:[");
outputTableNode(t->root);
printf("]\n");
return;
}
Node* findNode(Node* root, int val)
{
if (!root) return NULL;
if (val == root->val) return root;
if (val > root->val) return findNode(root->right,val);
else return findNode(root->left,val);
}
Node* findTree(Tree* t, int val)
{
if (!t) return NULL;
return findNode(t->root,val);
}
void freeTree(Tree* t)
{
if (!t) return;
freeAll(t->root);
free(t);
return;
}
//实现栈 数据元素的类型:Node*
typedef struct Stack
{
Node** data;
int size;
int top;
}Stack;
Stack* initStack(int n)
{
Stack* s = (Stack*)malloc(sizeof(Stack));
s->data = (Node**)malloc(sizeof(Node*) * n);
s->top = -1;
s->size = n;
return s;
}
void freeStack(Stack* s)
{
if (!s) return;
free(s->data);
free(s);
return ;
}
bool push(Stack* s, Node* n)
{
if (!s) return false;
if (s->top == s->size - 1) return false;
s->data[++s->top] = n;
return true;
}
bool isEmpty(Stack* s)
{
return !(s&&s->top!=-1);
}
Node* pop(Stack* s)
{
return s->data[s->top--];
}
Node* buildTree(char* str)
{
Stack* s = initStack(strlen(str));
Node* root,*n;
int flag=0;
int num=0;
while (*str)
{
switch (str[0])
{
case '(': //遇到左括号,压入结点,作为后续操作的根
push(s, n);
flag = 0;
break;
case ',':
flag = 1;
break;
case ')':
root = pop(s);
break;
default:
if (str[0] < '0' || str[0]>'9') break;
//计算数字
num = 0;
while ((str[0] >= '0' && str[0]<='9'))
{
num = num * 10 + str[0] - '0';
str++;
}
str--;
//根据flag插入栈顶的子树
n = initNode(num);
if (!isEmpty(s))
flag ? (s->data[s->top]->right = n) : (s->data[s->top]->left = n);
}
str++;
}
freeStack(s);
return root;
}
int main()
{
Tree* t = initTree();
char str[100];
scanf("%s", str);
printf("---");
t->root = buildTree(str);
preOrderTree(t);
outputTableTree(t);
freeTree(t);
return 0;
}
06-22
115