【 트리 】 이미 알고 있는 두 갈래 나무의 앞 순서와 중간 순서를 반복하여 뒷 순서를 구하고, 중간 순서와 뒤 순서를 반복하여 앞 순서를 구하다.

1319 단어
#include<iostream>
using namespace std;
// , 
void binary_tree_postorder(char* preorder,char* inorder,int length){
	if (length == 0)
		return;
	char root = *preorder;
	int index = 0;
	for (; index < length; index++){
		if (inorder[index] == root)
			break;
	}
	binary_tree_postorder(preorder + 1, inorder, index);
	binary_tree_postorder(preorder + index + 1, inorder + index + 1, length - index - 1);
	cout << root << " ";
}
// , 
void binary_tree_preorder(char* inorder, char* postorder, int length){
	if (length == 0)
		return;
	char root = postorder[length-1];
	cout << root << " ";
	int index = 0;
	for (; index < length; index++){
		if (inorder[index] == root)
			break;
	}
	binary_tree_preorder(inorder, postorder, index);
	binary_tree_preorder(inorder + index + 1, postorder + index, length - index - 1);
}
int main(){
	char* preorder = "ABDHKECFIGJ";
	char* inorder = "HKDBEAIFCGJ";
	char* postorder = "KHDEBIFJGCA";

	binary_tree_postorder(preorder, inorder, 11);
	cout << endl;
	binary_tree_preorder(inorder, postorder, 11);
	cout << endl;

	return 0;
}

참조:
http://blog.csdn.net/feliciafay/article/details/6816871
http://blog.csdn.net/feliciafay/article/details/6817851

좋은 웹페이지 즐겨찾기