树的模板.

var code = "e1e9ecba-71a4-4954-8419-e6e71c6123d4"

树:

存储:

struct node {
	int father, child[Maxn], len;
} Tree[Maxn];

查找根节点:

for(int i = 1;i <= n; ++i) if(Tree[i].father == 0) Root_Id = i;

二叉树:

存储:

struct node {
	int father, lchild, rchild;
} BTree[Maxn];

遍历:

先序遍历:

void preorder(int root) {
	if(root) {
		printf("%d ", root);
	 	preorder(BTree[root].lchild);
	 	preorder(BTree[root].rchild);
	}
}

中序遍历:

void inorder(int root) {
	if(root) {
		inorder(BTree[root].lchild);
		printf("%d ", root);
		inorder(BTree[root].rchild);
	}
}

后序遍历:

void postorder(int root) {
	if(root) {
		postorder(BTree[root].lchild);
		postorder(BTree[root].rchild);
		printf("%d ", root);
	}
}
posted @ 2022-05-28 11:31  LCat90  阅读(5)  评论(0)    收藏  举报  来源