The path between the binary tree from the root node to the node pointed by r and the output algorithm [C/C++]


foreword

Save your own understanding of the path from the root node of the binary tree to the node pointed to by r and output the algorithm.

I'm lazy here, I don't have the api function to adjust my own stack, I use the stack in stl. But it doesn't hurt, what matters is the thought.

For the algorithm of the path between the root node of the binary tree and the node pointed to by r, we must first understand that the node pointed to by r is found first, and then the stack is continuously popped out. This is a bit similar to outputting the child node first and then outputting the parent node, so the The model of the algorithm is the non-recursive post-order traversal of the binary tree.

Although stl is used here, it is mainly based on C/C++.


train of thought

1. First recursively build a tree

//先序递归建树,以'.'表示NULL
void PreOrderCreateTree(BiTree* bt) {
    
    
	ElemType temp;
	cin >> temp;
	if (temp == '.') {
    
    
		(*bt) == NULL;
	}
	else {
    
    
		Node* s = (Node*)malloc(sizeof(Node));
		s->data = temp;
		s->lchild = s->rchild = NULL;
		*bt = s;
		PreOrderCreateTree(&(*bt)->lchild);
		PreOrderCreateTree(&(*bt)->rchild);
	}
	return;
}

2. Write an algorithm for searching by value for r nodes

//按值查找二叉树,返回其节点。
void SearchTree(BiTree bt, ElemType key, Node** p) {
    
    
	if (bt == NULL) {
    
    
		return;
	}
	else {
    
    
		//如果找到key,那么就修改p指针,否则p指针一直为NULL;
		if (bt->data == key) {
    
    
			*p = bt;
		}
		else {
    
    
			SearchTree(bt->lchild, key, p);
			SearchTree(bt->rchild, key, p);
		}
	}
}

3. Path output algorithm

The template is traversed in the non-recursive post-order of the binary tree. When the popped element is an r node, the empty stack is popped.

if (tmp == p) {
    
    
	while (!S.empty()) {
    
    
		BiTreeNode* pop = S.top();
		printf("%c ", pop->data);
		S.pop();
	}
	return;
}
else {
    
    
	pre = cur;
	S.pop();
	cur = NULL;
}

4. Test results

insert image description here

The complete code is as follows

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<stack>
#define ElemType char
using namespace std;

//二叉树的存储结构
typedef struct Node {
    
    
	ElemType data;
	struct Node* lchild;
	struct Node* rchild;
}Node, * BiTree;

//先序递归建树,以'.'表示NULL
void PreOrderCreateTree(BiTree* bt) {
    
    
	ElemType temp;
	cin >> temp;
	if (temp == '.') {
    
    
		(*bt) == NULL;
	}
	else {
    
    
		Node* s = (Node*)malloc(sizeof(Node));
		s->data = temp;
		s->lchild = s->rchild = NULL;
		*bt = s;
		PreOrderCreateTree(&(*bt)->lchild);
		PreOrderCreateTree(&(*bt)->rchild);
	}
	return;
}

//按值查找二叉树,返回其节点。
void SearchTree(BiTree bt, ElemType key, Node** p) {
    
    
	if (bt == NULL) {
    
    
		return;
	}
	else {
    
    
		//如果找到key,那么就修改p指针,否则p指针一直为NULL;
		if (bt->data == key) {
    
    
			*p = bt;
		}
		else {
    
    
			SearchTree(bt->lchild, key, p);
			SearchTree(bt->rchild, key, p);
		}
	}
}

//二叉树从根节点到r所指节点之间的路径并输出算法
void Path(BiTree bt, Node* r) {
    
    
	if (bt == NULL) {
    
    
		return;
	}
	Node* node = bt;
	Node* pre = NULL;
	stack<Node*>S;
	while (node != NULL || !S.empty()) {
    
    
		if (node != NULL) {
    
    
			S.push(node);
			node = node->lchild;
		}
		else {
    
    
			Node* top = S.top();
			if (top->rchild == NULL || top->rchild == pre) {
    
    
				//如果找到r指针,那么直接弹栈直至栈空即可。否则按正常弹栈。
				if (top == r) {
    
    
					while (!S.empty()) {
    
    
						Node* tmp = S.top();
						printf("%c ", tmp->data);
						S.pop();
					}
					return;
				}
				else {
    
    
					pre = top;
					S.pop();
					node = NULL;
				}
			}
			else {
    
    
				node = top->rchild;
			}
		}
	}
}

void test() {
    
    
	BiTree bt = NULL;
	PreOrderCreateTree(&bt);
	Node* p = NULL;
	SearchTree(bt, 'E', &p);
	Path(bt, p);
	return;
}

int main() {
    
    
	test();/*测试用例 ABD..E..CF... */
	//输出E B A
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45400167/article/details/125798880