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));
}
}