当前位置:   article > 正文

#创建树及先序&中序创建树_创建treenode

创建treenode
struct TreeNode {
	char data;
	TreeNode* lchild;
	TreeNode* rchild;
};
//#号法创建树 先序创建
TreeNode* createTree() {
	TreeNode* node = NULL;
	TreeNode* pL = NULL;
	TreeNode* pR = NULL;
	char h;
	scanf("%c", &h);
	cout << "h:  " << h << endl;
	if (h == '#') {
		return NULL;
	}
	else {
		node = new TreeNode;
		memset(node, 0, sizeof(TreeNode));
		node->data = h;
		pL = createTree();
		if (pL != NULL) {
			node->lchild = pL;
			cout << "pL data: " << pL->data << endl;;
		}
		else {
			node->lchild = NULL;
			cout << "pL data: NULL" <<  endl;;
		}
		pR = createTree();
		if (pR != NULL) {
			node->rchild = pR;
			cout << "pR data: " << pR->data << endl;;
		}
		else {
			node->rchild = NULL;
			cout << "pR data: NULL" <<  endl;;
		}
	}
	return node;
}
//先序创建树 用后序释放
void deleteTree(TreeNode* node) {
	if (node == NULL) {
		return ;
	}
	if (node->lchild != NULL) {
		deleteTree(node->lchild);
		node->lchild = NULL;
	}
	if (node->rchild != NULL) {
		deleteTree(node->rchild);
		node->rchild = NULL;
	}
	if (node != NULL) {
		deleteTree(node);
		node == NULL;
	}
}
void preMethood(TreeNode* root) {
	if (root == NULL) {
		return;
	}
	cout << " preMethood: " << root->data << " ";
	preMethood(root->lchild);
	preMethood(root->rchild);
}
/*
先序遍历和中序遍历生成树
1.通过先序遍历找到根节点A,再通过中序遍历的位置找出左子树,右子树
2.在A的左子树中,找左子树的根节点(在先序遍历中找),转1
3.在A的右子树中,找右子树的根节点(在先序遍历中找),转1
先序遍历:ADEBCF
中序遍历:DEACFB
*/
TreeNode* createTreeByPreAndMed(char* pre, char* med, int length) {
	TreeNode* node = new TreeNode;
	node->data = pre[0];
	node->lchild = NULL;
	node->rchild = NULL;
	cout << "crtdata: " << node->data << endl;
	if (length == 0) {
		return NULL;
	}
	if (length == 1) {
		return node;
	}
	char data[2] = { pre[0],'\0' };
	char* finddata = strstr(med, data);
	int len = finddata - med;
	cout << "findleft:  " << pre + 1 << endl;
	node->lchild = createTreeByPreAndMed(pre + 1, med, len);
	cout << " findright: " << pre + len + 1 << endl;
	node->rchild = createTreeByPreAndMed(pre + len + 1, med + len + 1, length - len - 1);
	cout << "crtdata: " << node->data << " lchild: " << node->lchild->data << " rchild: " << node->rchild->data << endl;
	return node;
}
int main() {
	char* str1 = "124895367";
	char* str2 = "849251637";
	createTreeByPreAndMed(str1, str2, strlen(str1));
	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/596058
推荐阅读
  

闽ICP备14008679号