二叉树建立与广义表输出
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Node{
int data;
struct Node *lchild, *rchild;
}Node;
typedef struct Tree{
Node *root;
int n;
}Tree;
Node *init_node(int val){
Node *p = (Node *)malloc(sizeof(Node));
p->data = val;
p->lchild = p->rchild = NULL;
return p;
}
Tree *init_tree(){
Tree *tree = (Tree *)malloc(sizeof(Tree));
tree->root = NULL;
tree->n = 0;
return tree;
}
void clear_node(Node *node){
if (node == NULL) return;
clear_node(node->lchild);
clear_node(node->rchild);
free(node);
return;
}
void clear_tree(Tree *tree){
if (tree == NULL) return;
clear_node(tree->root);
free(tree);
return;
}
Node *insert_node(Node *root, int val, int *flag){
if (root == NULL) {
*flag = 1;
return init_node(val);
}
if (root->data == val) return root;
if (val < root->data) root->lchild = insert_node(root->lchild, val, flag);
if (val > root->data) root->rchild = insert_node(root->rchild, val, flag);
return root;
}
void insert(Tree *tree, int val){
int flag = 0;
tree->root = insert_node(tree->root, val, &flag);
tree->n += flag;
return;
}
void pre_order_node(Node *node){
if (node == NULL){
return;
}
printf("%d ", node->data);
pre_order_node(node->lchild);
pre_order_node(node->rchild);
return;
}
void pre_order(Tree *tree){
if (tree == NULL) return;
printf("qianxu\n");
pre_order_node(tree->root);
printf("\n");
return;
}
void in_order_node(Node *node){
if (node == NULL){
return;
}
in_order_node(node->lchild);
printf("%d", node->data);
in_order_node(node->rchild);
return;
}
void in_order(Tree *tree){
if (tree == NULL) return;
printf("zhongxu\n");
in_order_node(tree->root);
printf("\n");
return;
}
void post_order_node(Node *node){
if (node == NULL){
return;
}
post_order_node(node->lchild);
post_order_node(node->rchild);
printf("%d", node->data);
return;
}
void post_order(Tree *tree){
if (tree == NULL) return;
printf("houxu\n");
post_order_node(tree->root);
printf("\n");
return;
}
void output_node(Node *node){
if (node == NULL) return;
printf("%d", node->data);
if (node->lchild == NULL && node->rchild == NULL)
return;
printf("(");
output_node(node->lchild);
printf(",");
output_node(node->rchild);
printf(")");
return;
}
void output(Tree *tree){
if (tree == NULL) return;
printf("tree(%d)\n", tree->n);
output_node(tree->root);
printf("\n");
return;
}
int main(){
srand(time(0));
Tree *tree = init_tree();
#define maxop 10
for (int i = 0; i < maxop; i++){
int val = rand() % 100;
insert(tree, val);
}
output(tree);
pre_order(tree);
#undef maxop
clear_tree(tree);
return 0;
}
广义表建树
Node* generalized_build(const string &input_str){
stack<Node *> s;
Node *root = NULL;
Node *p, *temp;
if (input_str == ""){
return NULL;
}
int k = -1;
for (int i = 0; i < input_str.length(); i++){
if (input_str[i] == '('){
k = 0;
s.push(p);
}else if(input_str[i] == ','){
k = 1;
}else if(input_str[i] == ')'){
s.pop();
}else{
p = new Node(input_str[i]);
if (k == -1){
root = p;
s.push(root);
}else if(k == 0){
temp = s.top();
temp->lchild = p;
}else if(k == 1){
temp = s.top();
temp->rchild = p;
}
}
}
return root;
}
完整版广义建表
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int node_num = 0;
typedef struct Node{
int data;
struct Node *lchild, *rchild;
}Node;
typedef struct Tree{
Node *root;
int n;
}Tree;
Node *init_node(int val){
Node *p = (Node *)malloc(sizeof(Node));
p->data = val;
p->lchild = p->rchild = NULL;
return p;
}
Tree *init_tree(){
Tree *tree = (Tree *)malloc(sizeof(Tree));
tree->root = NULL;
tree->n = 0;
return tree;
}
void clear_node(Node *node){
if (node == NULL) return;
clear_node(node->lchild);
clear_node(node->rchild);
free(node);
return;
}
void clear_tree(Tree *tree){
if (tree == NULL) return;
clear_node(tree->root);
free(tree);
return;
}
Node *insert_node(Node *root, int val, int *flag){
if (root == NULL) {
*flag = 1;
return init_node(val);
}
if (root->data == val) return root;
if (val < root->data) root->lchild = insert_node(root->lchild, val, flag);
if (val > root->data) root->rchild = insert_node(root->rchild, val, flag);
return root;
}
void insert(Tree *tree, int val){
int flag = 0;
tree->root = insert_node(tree->root, val, &flag);
tree->n += flag;
return;
}
typedef struct Stack{
Node **data;
int top, size;
} Stack;
Stack *init_stack(int n){
Stack *s = (Stack *)malloc(sizeof(Stack));
s->data = (Node **)malloc(sizeof(Node *)* n);
s->size = n;
s->top = -1;
return s;
}
void clear_stack(Stack *s){
if (s == NULL) return ;
free(s->data);
free(s);
return;
}
Node *top(Stack *s){
return s->data[s->top];
}
int empty(Stack *s){
return s->top == -1;
}
int push(Stack *s, Node *val){
if (s == NULL) return 0;
if (s->top == s->size - 1) return 0;
s->data[++(s->top)] = val;
return 1;
}
int pop(Stack *s){
if (s == NULL) return 0;
if (empty(s)) return 0;
s->top--;
return 1;
}
Node *table_build(const char *str, int *node_num){
Stack *s = init_stack(strlen(str));
Node *temp = NULL, *p = NULL;
int flag = 0;
while(str[0]){
switch (str[0]){
case '(':
push(s, temp);
flag = 0;
break;
case ',':
flag = 1;
break;
case ')':
p = top(s);
pop(s);
break;
default:
temp = init_node(str[0]);
if(!empty(s) && flag == 0){
top(s)->lchild = temp;
}else if(!empty(s) && flag == 1){
top(s)->rchild = temp;
}
break;
}
str++;
}
clear_stack(s);
if (temp && !p) p = temp;
return p;
}
void pre_order_node(Node *node){
if (node == NULL){
return;
}
printf("%d ", node->data);
pre_order_node(node->lchild);
pre_order_node(node->rchild);
return;
}
void pre_order(Tree *tree){
if (tree == NULL) return;
printf("qianxu\n");
pre_order_node(tree->root);
printf("\n");
return;
}
void in_order_node(Node *node){
if (node == NULL){
return;
}
in_order_node(node->lchild);
printf("%c ", node->data);
in_order_node(node->rchild);
return;
}
void in_order(Tree *tree){
if (tree == NULL) return;
printf("zhongxu\n");
in_order_node(tree->root);
printf("\n");
return;
}
void post_order_node(Node *node){
if (node == NULL){
return;
}
post_order_node(node->lchild);
post_order_node(node->rchild);
printf("%d", node->data);
return;
}
void post_order(Tree *tree){
if (tree == NULL) return;
printf("houxu\n");
post_order_node(tree->root);
printf("\n");
return;
}
void output_node(Node *node){
if (node == NULL) return;
printf("%d", node->data);
if (node->lchild == NULL && node->rchild == NULL)
return;
printf("(");
output_node(node->lchild);
printf(",");
output_node(node->rchild);
printf(")");
return;
}
void output(Tree *tree){
if (tree == NULL) return;
printf("tree(%d)\n", tree->n);
output_node(tree->root);
printf("\n");
return;
}
int main(){
const char *str = "A(B(E,),C(,F))";
Tree *tree = init_tree();
tree->root = table_build(str, &node_num);
tree->n = node_num;
in_order(tree);
return 0;
}