树的整理与总结

本文深入探讨了二叉树和一般树的多种遍历方法,包括先序、中序、后序和层次遍历,并详细介绍了如何通过先序与中序、中序与后序遍历来构建二叉树。此外,还讲解了二叉排序树的构建,树的高度、叶子节点的计算,以及节点值的交换。最后,文章提供了基于宽度优先搜索和深度优先搜索的一般树操作示例。
摘要由CSDN通过智能技术生成

1.二叉树——四种遍历(下标)

#define MAXN 100

struct node
{
	int data;
	int lchild = -1;
	int rchild = -1;
}tree[MAXN];

void preorder(int root)
{
        if (root == -1)
		return;
	对tree[root].data的操作;
	preorder(tree[root].lchild);
	preorder(tree[root].rchild);

}

void inorder(int root)
{
        if (root == -1)
		return;
	inorder(tree[root].lchild);
	对tree[root].data的操作;
	inorder(tree[root].rchild);
}

void postorder(int root)
{
        if (root == -1)
		return;
	postorder(tree[root].lchild);
	postorder(tree[root].rchild);
	对tree[root].data的操作;
}

void levelorder()
{
	queue<node> q;
	q.push(tree[0]);
	while (!q.empty())
	{
        	node t = q.front();
		q.pop();
		if (t.lchild != -1)
			q.push(tree[t.lchild]);
		if (t.rchild != -1)
        		q.push(tree[t.rchild]);
		对t.data的操作;
	}
}

2.二叉树——四种遍历(指针)

struct node
{
	int data;
	node* lchild = NULL;
	node* rchild = NULL;
};

node* root = NULL;

void preorder(node* root)
{
        if (!root)
        {
        	root->data的操作;
        	preorder(root->lchild);
        	preorder(root->rchild);
        }
}

void inorder(node* root)
{
        if (!root)
        {
        	inorder(root->lchild);
                root->data的操作;
        	inorder(root->rchild);
        }
}

void postorder(node* root)
{
        if (!root)
        {
        	postorder(root->lchild);
        	postorder(root->rchild);
                root->data的操作;
        }
}

void levelorder(node* tree)
{
	if(tree)
	{
        	queue<node> q;
		q.push(*tree);
		while (!q.empty())
		{
			node t = q.front();
			q.pop();
			if (t.lchild)
				q.push(*t.lchild);
			if (t.rchild)
				q.push(*t.rchild);
			对t.data的操作;
		}
	}
}

3.二叉树——按照先序遍历和中序遍历建树 

struct node
{
	int data;
	node* lchild;
	node* rchild;
};

int len, pre[MAX], in[MAX];

node* PreInCreateTree(int *pre, int *in, int len)
{
	if (len == 0)
		return NULL;
	int i = 0;
	while (pre[0] != in[i])
		i++;
	node* t = (node*)malloc(sizeof(node));
	t->data = pre[0];
	t->lchild = PreInCreateTree(pre + 1, in, i);
	t->rchild = PreInCreateTree(pre + i + 1, in + i + 1, len - i - 1);
	return t;
}

4.二叉树——按照中序遍历和后序遍历建树

struct node
{
	int data;
	node* lchild;
	node* rchild;
};

int len, in[MAX], post[MAX];

node* InPostCreateTree(int *in, int *post, int len)
{
	if (len <= 0)
		return NULL;
	int i = len - 1;
	while (i >= 0 && post[len - 1] != in[i])
		i--;
	node* t = (node*)malloc(sizeof(node));
	t->data = post[len - 1];
	t->lchild = InPostCreateTree(in, post, i);
	t->rchild = InPostCreateTree(in + i + 1, post + i, len - i - 1);
	return t;
}

5.二叉树——求高度

 


6.二叉树——求叶子树

 


7.二叉树——交换左右子树

struct node
{
	int data;
	node* lchild;
	node* rchild;
};

node* tree = NULL;

void change(node* tree)
{
	if (tree)
	{
		change(tree->lchild);
        	change(tree->rchild);
        	node* t = tree->lchild;
		tree->lchild = tree->rchild;
		tree->rchild = t;
	}
}

8.二叉排序树——建树

另:二叉排序树的中序遍历为这组数的非递减序

struct node
{
	int data;
	node* lchild;
	node* rchild;
};

void insert(node* &tree, int num)
{
	if (!tree)
	{
		tree=(node *)malloc(sizeof(node));
		tree->data = num;
		tree->lchild = NULL;
		tree->rchild = NULL;
	}
	else
	{
		if (num < tree->data)
			insert(tree->lchild, num);
		else
			insert(tree->rchild, num);
	}
}

9.一般树——bfs(如求高度、求叶子或对每一层要做操作等)

struct node
{
	int val;
	vector<int> next;
};

vector<node> tree;
int root;

void bfs()  //求每一层的叶子数
{
        queue<int> q;
        q.push(root);
        int level = 0;
	while (!q.empty())
	{
		int size = q.size();
		int cnt = 0;
                level++;
		while(size--)
		{
			int t = q.front();
			q.pop();
			if (tree[t].next.size() == 0)
				cnt++;
			else
			{
				for (int i = 0; i < tree[t].next.size(); i++)
					q.push(tree[t].next[i]);
			}
        	}
		cout << level << " " << cnt << endl;
	}
}

10.一般树——dfs(如求路径权值、求树的直径等)

#define MAXN 100

struct node
{
	int val;
	vector<int> next;
};

vector<node> tree;
int path[MAXN] = {0};

void dfs(int root)  //求根到叶子权值和为s的路径
{
	if (sum == s)  
	{
		if (tree[root].next.size())
			return;
		for (int i = 0; i < k - 1; i++)
			cout << path[i] << " ";
		cout << path[k - 1] << endl;
		return;
	}
	if (sum > s)
		return;
	for (int i = 0; i < tree[root].next.size(); i++)
	{
		int t = tree[root].next[i];
		path[k++] = tree[t].val;
		sum += tree[t].val;
		dfs(t);
		sum-= tree[t].val;
		k--;
	}
}
#define MAXN 1000

vector<vector<int>> g;
vector<int> v;
int vis[MAXN], maxd = 0;

void dfs(int x, int depth)  //求树的直径
{
	vis[x] = 1;
	if (depth > maxd)
	{
		maxd = depth;
		v.clear();
		v.push_back(x);
	}
	else if (depth == maxd)
		v.push_back(x);
	for (int i = 0; i < g[x].size(); i++)
	{
		if (!vis[g[x][i]])
			dfs(g[x][i], depth + 1);
	}
}

int main()
{
	int n;
	cin >> n;
	g.resize(n);
	for (int i = 1; i < n; i++)
	{
		int from, to;
		cin >> from >> to;
		g[from].push_back(to);
		g[to].push_back(from);
	}
	memset(vis, 0, sizeof(vis));
	dfs(0, 1);
	maxd = 0;
	memset(vis, 0, sizeof(vis));
	dfs(v[0], 1);
	cout << maxd << endl;
	return 0;
}

11.待定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值