1. 程式人生 > >樹的整理與總結

樹的整理與總結

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.待定