二叉树的基础问题

先给定一个二叉树的图作为样例:

二叉树的基础问题_第1张图片

图中下边没有给出的表示为空,图画的不好,将就着看吧。

下面的代码中分别给出了二叉树的前中后的递归遍历和非递归遍历,最后还给出层次遍历。

#include<iostream>
#include<deque>
#include<stack>
using namespace std;
#define NIL '#'
typedef struct tree_node{
	char data;
	struct tree_node *lchild,*rchild;
}BiTNode,*BiTree;
void create_tree(BiTree *T) {
	char ch; 
	scanf("%c", &ch);
	if(ch == NIL){
		*T = NULL;
	} else {
		*T = (BiTNode*)malloc(sizeof(BiTNode));
		(*T)->data = ch;
		create_tree(&(*T)->lchild);
		create_tree(&(*T)->rchild);
	}
}
void pre_order_traverse_recursive(BiTree T) { //前根递归遍历
	if(T) {
		cout << T->data << " ";
		pre_order_traverse_recursive(T->lchild);
		pre_order_traverse_recursive(T->rchild);
	}
}

void pre_order_traverse_iterative(BiTree T) {  //前根非递归遍历
	stack<BiTNode *> m_stack;
	m_stack.push(T);
	BiTNode *p;
	while(!m_stack.empty()) {
		while((p = m_stack.top()) != NULL) {
			cout << p->data << " ";
			m_stack.push(p->lchild);
		}
		
		m_stack.pop();

		if(!m_stack.empty()) {
			p = m_stack.top();
			m_stack.pop();
			m_stack.push(p->rchild);
		}
	}
}
void in_order_traverse_recursive(BiTree T) { //中根递归遍历
	if(T) {
		in_order_traverse_recursive(T->lchild);
		cout << T->data << " ";
		in_order_traverse_recursive(T->rchild);
	}
}
void in_order_traverse_iterative(BiTree T) { //中根非递归遍历
	stack<BiTNode*> m_stack;
	m_stack.push(T);
	BiTNode *p;

	while(!m_stack.empty()) {
		while((p = m_stack.top()) != NULL) 
			m_stack.push(p->lchild);

		m_stack.pop();

		if(!m_stack.empty()) {
			p = m_stack.top();
		    m_stack.pop();
			cout << p->data << " ";
			m_stack.push(p->rchild);
		}
	}
}

void post_order_traverse_recursive(BiTree T) { //后根递归遍历
	if(T) {
		post_order_traverse_recursive(T->lchild);
		post_order_traverse_recursive(T->rchild);
		cout << T->data << " ";
	}
}

void post_order_traverse_iterative(BiTree T) { //后根非递归遍历
	stack<BiTNode*> m_stack;
	BiTNode *p;
	BiTNode *r;
	p = T;
	while(p || !m_stack.empty()) {
		if(p) {
			m_stack.push(p);
			p = p->lchild;
		} else {
			p = m_stack.top();
			if(p->rchild && p->rchild != r) {
				p = p->rchild;
				m_stack.push(p);
				p = p->lchild;
			} else {
				p = m_stack.top();
				m_stack.pop();
				cout << p->data << " ";
				r = p;
				p = NULL;
			}
		}
	}
}

void horizontal_traverse(BiTree T) {
	deque<BiTNode *> m_deque;
	m_deque.push_back(T);
	while(!m_deque.empty()) {
		BiTNode *temp = m_deque.front();
		m_deque.pop_front();
		cout << temp->data << " ";
		if(temp->lchild)
			m_deque.push_back(temp->lchild);
		if(temp->rchild)
			m_deque.push_back(temp->rchild);
	}
}
void main() {
	BiTree T = NULL;
	create_tree(&T);

	//先根遍历
	cout << "pre_order_traverse_recursive:";
	pre_order_traverse_recursive(T);
	cout << endl;

	cout << "pre_order_traverse_iterative:";
	pre_order_traverse_iterative(T);
	cout << endl;

	//中根遍历
	cout << "in_order_traverse_recursive:";
	in_order_traverse_recursive(T);
	cout << endl;

	cout << "in_order_traverse_iterative:";
	in_order_traverse_iterative(T);
	cout << endl;

	//后根遍历
	cout << "post_order_traverse_iterative:";
	post_order_traverse_recursive(T);
	cout << endl;

	cout << "post_order_traverse_iterative:";
	post_order_traverse_iterative(T);
	cout << endl;
	// 水平遍历
	cout <<  "horizontal traverse:";
	horizontal_traverse(T);
	cout << endl;
	
}


你可能感兴趣的:(二叉树的基础问题)