C语言基础数据结构:二叉树及与广义表的转换

二叉树建立与广义表输出

#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);
	}
	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>
//#include <stack>//这里就不打栈代码了

//char str[1000];
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;
}

//广义表输入转二叉树
//遇左括号将元素p入栈,左括号后元素q为栈顶元素左孩子,逗号后元素为栈顶右孩子
//右括号出栈
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;//栈满
	//expand
	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;//p为根节点
	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;//只有一个根,此时p为空
	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(){
	/*
	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);
	}
	output(tree);
	pre_order(tree);
#undef maxop
	clear_tree(tree);*/

	//scanf("%[^\n]s", str);
	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;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值