2021秋季《数据结构》_第五章书面作业

测试数据见附录

题目

建立

  1. 前序+附加两个标志位

    BiNODE* buildTree_twoTags()
    {
    	int n; cin >> n;
    	if (n == 0) return NULL;
    	BiNODE* root = new BiNODE;
    	// root = NULL;
    	BiNODE* p = root;
    	BiNODE** Stack=new BiNODE*[n];
    
    	int top = 0;
    	
    	for (int i = 0; i < n; i++)
    	{
    		int ltag = 0, rtag = 0;
    		cin >> ltag >> p->data >> rtag;
    		if (rtag == 0)  // 有右子树
    			Stack[++top] = p;
    		else p->rchild = NULL;
    		
    		BiNODE* q = new BiNODE;
    		if (ltag == 0)  // 有左子树
    			p->lchild = q;
    		else
    		{
    			p->lchild = NULL;
    			p = Stack[top--];  // 更新p为p的双亲结点
    			p->rchild = q;
    		}
    
    		p = q;  // 移动指针
    	}
    	int ltag = 0, rtag = 0;
    	// 读入最后一个结点
    	cin >> ltag >> p->data >> rtag;
    	p->lchild = NULL;
    	p->rchild = NULL;
    	return root;
    }
    
  2. 前序序列+中序序列

    // 递归调用
    BiNODE* buildTree_pre_in_re(int* pre, int head1, int tail1, 
    				int* in, int head2, int tail2)
    {
    	if (tail2 < head2)
    		return NULL;
    	//cout << head1 << ' ' << tail1 << ' ' << head2 << ' ' << tail2 << endl;
    
    	int i = head2;  // 在中序遍历中找根节点
    	for (; i <= tail2; i++)
    	{
    		if (in[i] == pre[head1])
    			break;
    	}
    	BiNODE* root = new BiNODE;
    	root->data = pre[head1];
    	//cout << root->data << endl;
    	root->lchild = buildTree_pre_in_re(pre, head1+1, head1 + i - head2, in, head2, i - 1);
    	root->rchild = buildTree_pre_in_re(pre, head1 + i - head2+1, tail1, in, i + 1, tail2);
    	return root;
    }
    
    // 前序序列+中序序列
    BiNODE* buildTree_pre_in()
    {
    	int n;  cin >> n;
    	if (n == 0) return NULL;
    	int* pre = new int[n];
    	int* in = new int[n];
    	for (int i = 0; i < n; i++)
    	{
    		cin >> pre[i];
    	}
    	for (int i = 0; i < n; i++)
    	{
    		cin >> in[i];
    	}
    
    	BiNODE* root = buildTree_pre_in_re(pre, 0, n-1, in, 0, n-1);
    	return root;
    }
    
  3. 后序序列+中序序列

    BiNODE* buildTree_post_in_re(int* post, int head1, int tail1,
    	int* in, int head2, int tail2)
    {
    	if (tail2 < head2)
    		return NULL;
    	int i = head2;
    	for (; i <= tail2; i++)
    	{
    		if (in[i] == post[tail1])
    			break;
    	}
    	BiNODE* root = new BiNODE;
    	root->data = post[tail1];
    	root->lchild = buildTree_post_in_re(post, head1, head1+i-head2-1, in, head2, i - 1);
    	root->rchild = buildTree_post_in_re(post, head1 + i - head2, tail1-1, in, i + 1, tail2);
    	return root;
    
    }
    
    BiNODE* buildTree_post_in()
    {
    	int n;  cin >> n;
    	if (n == 0) return NULL;
    	int* post = new int[n];
    	int* in = new int[n];
    	for (int i = 0; i < n; i++)
    	{
    		cin >> post[i];
    	}
    	for (int i = 0; i < n; i++)
    	{
    		cin >> in[i];
    	}
    	BiNODE* root = buildTree_post_in_re(post, 0, n - 1, in, 0, n - 1);
    	return root;
    }
    

遍历

  1. 非递归前序遍历

    void preorder(BiNODE* root)
    {
    	if (!root)
    		return;
    
    	BiNODE* q[MAXN];
    	q[0] = root;
    	int top = 1;
    	while (top > 0)
    	{
    		BiNODE* t = q[--top];
    		cout << t->data << endl;
    		// 从右向左压入栈中
    		if (t->rchild)
    			q[top++] = t->rchild;
    		if (t->lchild)
    			q[top++] = t->lchild;
    	}
    }
    
  2. 递归前序遍历

    void preorderRecursive(BiNODE* root)
    {
    	if (root)
    	{
    		cout << root->data << endl;
    		preorderRecursive(root->lchild);
    		preorderRecursive(root->rchild);
    	}
    }
    
  3. 非递归中序遍历

    void inorder(BiNODE* root)
    {
    	if (!root) return;
    	BiNODE* t = root;
    	SNODE* top = NULL;
    
    	while (t||top)
    	{
    		while (t)  // 子树根节点连续入栈,沿左孩子向下
    		{
    			SNODE* p = new SNODE;
    			p->addr = t;
    			p->link = top;
    			top = p;
    			t = t->lchild;  // 遍历到最后一个左孩子为止
    		}
    		if (top)
    		{
    			t = top->addr;
    			cout << t->data << endl;
    			SNODE* p = top;
    			top = top->link;
    			delete p;  // pop栈顶
    			//if(top) cout << "top=" << top->addr->data << endl;
    			t = t->rchild;  // 处理右子树
    			//if (t) cout << "t=" << t->data << endl;
    		}
    	}
    }
    
  4. 递归中序遍历

    void inorderRecursive(BiNODE* root)
    {
    	if (root)
    	{
    		inorderRecursive(root->lchild);
    		cout << root->data << endl;
    		inorderRecursive(root->rchild);
    	}
    }
    
  5. 非递归后序遍历

    void postorder(BiNODE* root)
    {
    	BiNODE* s[MAXN];
    	int mark[MAXN], top = -1;
    	if (root == NULL) return;
    	s[++top] = root;
    	mark[top] = 0;
    	while (top >= 0) {
    		if (mark[top] == 0) 
    		{
    			BiNODE* p = s[top];
    			mark[top] = 1;
    			if (p->rchild != NULL) 
    			{
    				s[++top] = p->rchild;
    				mark[top] = 0;
    			}
    			if (p->lchild != NULL) 
    			{
    				s[++top] = p->lchild;
    				mark[top] = 0;
    			}
    
    		}
    		if (mark[top] == 1)
    			cout << s[top--]->data << endl;
    	}
    }
    
  6. 递归后序遍历

    void postorderRecursive(BiNODE* root)
    {
    	if (root)
    	{
    		postorderRecursive(root->lchild);
    		postorderRecursive(root->rchild);
    		cout << root->data << endl;
    	}
    }
    
  7. 层次遍历

    void layerOrder(BiNODE* root)
    {
    	if (!root) return;
    	BiNODE* q[MAXN];
    	int head = 0, tail = 1;
    	q[head] = root;
    	while (head<tail)
    	{
    		BiNODE* p = q[head++];
    		cout << p->data << endl;
    		if (p->lchild)
    			q[tail++] = p->lchild;
    		if (p->rchild)
    			q[tail++] = p->rchild;
    	}
    }
    

应用

  1. 求给定的二叉树的结点的个数

    int getNodeNum(BiNODE* root)
    {
    	if (!root) return 0;
    	return 1 + getNodeNum(root->lchild) + getNodeNum(root->rchild);
    }
    
  2. 求给定的二叉树的高度

    // 记仅有根节点的树高度为0
    int getTreeHeight(BiNODE* root)
    {
    	if (!root) return -1;
    	int hl = getTreeHeight(root->lchild);
    	int hr = getTreeHeight(root->rchild);
    	int hc = hl > hr ? hl : hr;
    	return 1 + hc;
    }
    
  3. 判断给定的一棵二叉树是否是满树

    bool isFull(BiNODE* root)
    {
    	if (!root)
    		return true;
    	return isFull(root->lchild) && isFull(root->rchild) && getTreeHeight(root->lchild) == getTreeHeight(root->rchild);
    }
    
  4. 判断给定的一个二叉树是否是完全二叉树

    bool isComplete(BiNODE* root)
    {
    	if (!root)
    		return true;
    	BiNODE* q[MAXN];
    	int head = 0, tail = 1;
    	bool restLeaf = false;
    	q[head] = root;
    	while (head<tail)
    	{
    		BiNODE* p = q[head++];
    		BiNODE* left = p->lchild;
    		BiNODE* right = p->rchild;
    		if (restLeaf && (left || right) ||
    			!left && right)
    			return false;
    		if (left)
    			q[tail++] = left;
    		if (right)
    			q[tail++] = right;
    		if (!left || !right)
    			restLeaf = true;
    	}
    	return true;
    }
    
  5. 如果二叉树的结点的值不允许重复,且是可比较大小的。判断一棵二叉树是否满足以下条件:左子树上的所有结点的值都小于根结点的值,右子树上的所有结点的值都大于根结点的值。

    int former = -1;
    // 判断是否二叉搜索树
    bool isBinarySearchTree(BiNODE* root)
    {
    	if (!root)
    		return true;
    	if (!isBinarySearchTree(root->lchild))
    		return false;
    	if (root->data < former)
    		return false;
    	former = root->data;
    	if (!isBinarySearchTree(root->rchild))
    		return false;
    	return true;
    }
    

附录

前序+附加两个标志位:
测试数据:
第一组 //只有根结点
1
1 2 1
第二组://每个结点上只有一个子结点
5
0 1 1
0 2 1
1 3 0
0 4 1
1 5 1
第三组: //完全二叉树
6
0 3 0
0 2 0
1 1 1
1 6 1
0 4 1
0 5 0
第四组://满树
7
0 1 0
0 2 0
1 4 1
1 5 1
0 3 0
1 6 1
1 7 1
第五组: //空树
0

前序+中序:在buildTree_pre_in()中对左右子树分别调用函数Build_pre_in_re(int pre[],int l,int r,int mid[])递归建树
测试数据:
第一组: //空树
0
第二组://只有根结点
1
2
2
第三组://每个结点上只有一个子结点
5
1 2 3 4 5
3 5 4 2 1
第四组://完全二叉树
6
3 2 1 6 4 5
1 2 6 3 5 4
第五组: //满树
7
1 2 4 5 3 6 7
4 2 5 1 6 3 7

后序+中序:在buildTree_post_in()中对左右子树分别调用Build_post_in_re(int post[],int l,int r,int mid[])函数递归建树
测试数据与上面相同,只不过用后序输入
第一组: //空树
0
第二组://只有根结点
1
2
2
第三组://每个结点上只有一个子结点
5
5 4 3 2 1
3 5 4 2 1
第四组://完全二叉树
6
1 6 2 5 4 3
1 2 6 3 5 4
第五组: //满树
7
4 5 2 6 7 3 1
4 2 5 1 6 3 7

第二部分: 遍历.cpp,我的main函数中采用了第一部分中的前序+中序建树
测试数据与第一部分相同:
第一组: //空树
0
第二组://只有根结点
1
2
2
第三组://每个结点上只有一个子结点
5
1 2 3 4 5
3 5 4 2 1
第四组://完全二叉树
6
3 2 1 6 4 5
1 2 6 3 5 4
第五组: //满树
7
1 2 4 5 3 6 7
4 2 5 1 6 3 7

第三部分: 应用.cpp中同样采用前序+中序的方式建树,输出五个函数的判断
第一组: //空树
0
第二组://只有根结点
1
2
2
第三组://每个结点上只有一个子结点
5
1 2 3 4 5
3 5 4 2 1
第四组://完全二叉树,但不是满树和二叉线索树
6
3 2 1 6 4 5
1 2 6 3 5 4
第五组: //满树,也是完全二叉树,但不是二叉线索树
7
1 2 4 5 3 6 7
4 2 5 1 6 3 7
第六组://既是完全二叉树又是二叉搜索树但不是满树
8
5 3 2 1 4 7 6 8
1 2 3 4 5 6 7 8
1.两个串相等的充要条件是( )。A.串长度相等B.串长度任意 C.串中各位置字符任意 D.串中各位置字符均对应相等 2.对称矩阵的压缩存储:以行序为主序存储下三角中的元素,包括对角线上的元素。二维下标为( i, j ),存储空间的一维下标为k,给出k与 i, j (i<j)的关系k=( ) (1<= i, j <= n , 0<= k < n*(n+1)/2)。 A.i*(i-1)/2+j-1 B.i*(i+1)/2+j C.j*(j-1)/2+i-1 D.j*(j+1)/2+i 3.二维数组A[7][8]以列序为主序的存储,计算数组元素A[5][3] 的一维存储空间下标 k=( )。 A.38 B.43 C.26 D.29 4.已知一维数组A采用顺序存储结构,每个元素占用4个存储单元,第9个元素的地址为144,则第一个元素的地址是( )。A.108 B.180 C.176 D.112 5. 下面( )不属于特殊矩阵。 A.对角矩阵 B. 三角矩阵C. 稀疏矩阵 D. 对称矩阵 6. 假设二维数组M[1..3, 1..3]无论采用行优先还是列优先存储,其基地址相同,那么在两种存储方式下有相同地址的元素有( )个。 A. 3 B. 2 C. 1 D. 0 7. 若Tail(L)非空,Tail(Tail(L))为空,则非空广义表L的长度是( )。(其中Tail表示取非空广义表的表尾) A. 3 B. 2 C. 1 D. 0 8.串的长度是( )。 A.串中不同字母的个数 B.串中不同字符的个数C.串中所含字符的个数,且大于0 D.串中所含字符的个数 9.已知广义表(( ),(a), (b, c, (d), ((d, f)))),则以下说法正确的是( )。A.表长为3,表头为空表,表尾为((a), (b, c, (d), ((d, f))))B.表长为3,表头为空表,表尾为(b, c, (d), ((d, f)))C.表长为4,表头为空表,表尾为((d, f)) D.表长为3,表头为(()),表尾为((a), (b, c, (d), ((d, f))))10.广义表A=(a,b,c,(d,(e,f))),则Head(Tail(Tail(Tail(A))))的值为( )。(Head与Tail分别是取表头和表尾的函数) A.(d,(e,f)) B.d C.f D.(e,f)二、填空题(每空 2 分,共 8 分)。 1.一个广义表为 F = (a, (a, b), d, e, (i, j), k),则该广义表的长度为________________。GetHead(GetTail(F))= _______________。 2.一个n*n的对称矩阵,如果以行或列为主序压缩存放入内存,则需要 个存储单元。 3.有稀疏矩阵如下: 0 0 5 7 0 0 -3 0 0 0 4 0 0 2 0 它的三元组存储形式为: 。 三、综合题(共 22 分)。 1.(共8分)稀疏矩阵如下图所示,描述其三元组的存储表示,以及转置后的三元组表示。 0 -3 0 0 0 4 0 6 0 0 0 0 0 0 7 0 15 0 8 0 转置前(4分): 转置后(4分): 2. (共14分)稀疏矩阵M的三元组表如下,请填写M的转置矩阵T的三元组表,并按要求完成算法。 (1)写出M矩阵转置后的三元组存储(6分): M的三元组表: T的三元组表: i j e 2 1 3 3 2 4 4 2 5 4 3 5 5 1 6 5 3 6 i j e (2)如下提供了矩阵采用三元组存储时查找指定行号(m)和列号(n)元素值的算法框架,将代码补充完整(每空2分,共8分)。 typedefstruct{ inti,j; ElemType e; }Triple; typedefstruct{ Triple data[MAXSIZE+1]; //data[0]未用 intmu,nu,tu; //矩阵的行数,列数和非零元的个数 }TSMatrix; voidFind_TSMatrix(TSMatrix M, int m, int n, ElemType&e) //M为要查找的稀疏矩阵三元组存储,m为要查找的元素的行号,n为列号,e为查找后得到的值。 { for ( i=1 ; i<=M.tu ;i++) if( && ) { e=M.data[i].e; ; } if( ) e=0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值