C语言实现逆序打印叶子节点路径

C语言实现逆序打印叶子节点路径

实现的功能,输入一个广义表,实现打印所有叶子结点到根节点的逆序路径
算法思想:
首先通过字符串的形式读入广义表
然后通过分析字符串进行广义表和二叉树的转换:

//根据广义表创建二叉树
BT* createBTByGLists(char* gLists, int nodeQuantity) {
    BT* rootNode = NULL;
    BT* currentNode = NULL;

    //创建指针数组作为栈结构
    BT** stack = (BT**) malloc(sizeof(BT*)* nodeQuantity);
    int top = -1;
    int flag = 0;
    int index = 0;
    char c = gLists[index];
    while (c != '\0') {
        switch (c) {
            case '(':
                stack[++top] = currentNode;
                flag = LEFT;
                break;
            case ',':
                flag = RIGHT;
                break;
            case ')':
                top--;
                break;
            case ' ':
                break;
            default:
                currentNode = (BT*) malloc(sizeof(BT));
                currentNode->data = c;
                currentNode->left = currentNode->right = NULL;

                if (rootNode == NULL) {
                    rootNode = currentNode;
                }         case LEFT:
                            stack[top]->left = currentNode;
                            break;
                        case RIGHT:
                            stack[top]->right = currentNode;
                            break;
                    }
                }
        }
        c = gLists[++index];
    }
    free(stack);
    return rootNode;
}

逆序打印叶子结点的路径

//叶子结点到根节点的路径 
void routeToRoot(BT* root, Stack* stack){
	if (root) {
        push(stack, root->data);//只要root不为空,就将root的值入栈
        if (root->left == NULL && root->right == NULL){
			printStack(stack);
			printf("\n");
		} else {
            routeToRoot(root->left, stack);
            routeToRoot(root->right, stack);
        }
        pop(stack);
    }
}

输入:
例如:a(b(c(d,e)),f(g,h(i,j)))
输出:

d c b a

e c b a

g f a

i h f a 

j h f a

完整代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define LEFT  1
#define RIGHT 2 
//树节点 
typedef struct node {
    char data;
    struct node *left, *right;
} BT;
//栈结构 
typedef struct Stack{
	char* base;
	char* top;
}Stack;
BT* createBTByGLists(char *gLists, int nodeQuantity);
char* getString();
int getNodeQuantity(char* str);
void push(Stack* stack, char ch);
char pop(Stack* stack);
void printStack(Stack* stack); 
void routeToRoot(BT* root, Stack* stack);
void stackInit(Stack* stack);
int main(int argc, const char* argv[]) {

    char* gLists = getString();
    Stack stack;
    stackInit(&stack);
	int nodeQuantity = getNodeQuantity(gLists);
	//printf("%d",nodeQuantity);
    BT* rootNode = createBTByGLists(gLists, nodeQuantity);
	routeToRoot(rootNode, &stack);
    return 0;
}
//栈的初始化 
void stackInit(Stack* stack){
	stack->base = (char*)malloc(sizeof(char)*50);
	if(!stack->base){
		exit(-1);
	}
	stack->top = stack->base;
}
//读取字符串 
char* getString(){
	char* str = (char*)malloc(sizeof(char)*50);
	if(!str){
		printf("ERROR");
		exit(-1);
	}
	printf("请输入广义表:"); 
	gets(str);
	return str;
} 
//返回节点个数 
int getNodeQuantity(char* str){
	int i = 0;
	int count = 0;
	while(str[i] != '\0'){
		if(isalnum(str[i])){
			count++;
		}
		i++;
	}
	return count;
} 
//根据广义表创建二叉树
BT* createBTByGLists(char* gLists, int nodeQuantity) {
    BT* rootNode = NULL;
    BT* currentNode = NULL;

    //创建指针数组作为栈结构
    BT** stack = (BT**) malloc(sizeof(BT*)* nodeQuantity);
    int top = -1;
    int flag = 0;
    int index = 0;
    char c = gLists[index];
    while (c != '\0') {
        switch (c) {
            case '(':
                stack[++top] = currentNode;
                flag = LEFT;
                break;
            case ',':
                flag = RIGHT;
                break;
            case ')':
                top--;
                break;
            case ' ':
                break;
            default:
                currentNode = (BT*) malloc(sizeof(BT));
                currentNode->data = c;
                currentNode->left = currentNode->right = NULL;

                if (rootNode == NULL) {
                    rootNode = currentNode;
                }         case LEFT:
                            stack[top]->left = currentNode;
                            break;
                        case RIGHT:
                            stack[top]->right = currentNode;
                            break;
                    }
                }
        }
        c = gLists[++index];
    }
    free(stack);
    return rootNode;
}
//叶子结点到根节点的路径 
void routeToRoot(BT* root, Stack* stack){
	if (root) {
        push(stack, root->data);//只要root不为空,就将root的值入栈
        if (root->left == NULL && root->right == NULL){
			printStack(stack);
			printf("\n");
		} else {
            routeToRoot(root->left, stack);
            routeToRoot(root->right, stack);
        }
        pop(stack);
    }
}
//压栈 
void push(Stack* stack, char ch){
	*stack->top = ch;
	stack->top++;
}
//弹栈,返回被弹出的元素 
char pop(Stack* stack){
	stack->top--;
	char ch = *(stack->top);
	return ch;
}
//打印栈中数据
void printStack(Stack* stack){
	char* pTop = stack->top;
	while(stack->base != pTop){
		printf("%c ", *(--pTop));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值