zoukankan      html  css  js  c++  java
  • 博客作业04--树

    1.学习总结

    2.PTA实验作业

    题目1: 先序输出叶结点

    设计思路

    for 先序遍历这棵树
         当节点的左孩子和右孩子为空时,输出节点值
         递归浏览左孩子,递归浏览右孩子。
         当节点为空时退出。
    end 
    

    代码截图

    PTA提交列表说明

    一开始只有部分正确是因为当节点为空时,需退出这个函数。

    题目2:还原二叉树

    设计思路

    for  先用先序与中序遍历构造出这棵二叉树
          通过递归调用来计算树的高度
          找出最大的高度值 
    end
    

    代码截图

    PTA提交列表说明

    本题一开始在构建二叉树的时候一直找不到规律,后面通过查阅资料,知道了他的规律
    递归求高度还是比较容易理解的

    题目3: jmu-ds-表达式树

    设计思路

    代码截图

    PTA提交列表说明

    3.截图本周题目集的PTA最后排名

    3.1 PTA排名

    3.2 我的得分:1.5分

    4. 阅读代码

    #include<bits/stdc++.h>
    using namespace std;
    typedef struct node{
    	char data;
    	struct node *lchild,*rchild;
    }BTNode;
    typedef struct node * BTree;
    void CreateBTree(BTree &BT,string str);
    int GetWPL(BTree BT,int n);
    int main(){
    	int n=0,WPL=0;
    	BTree BT;
    	string str;
    	cin>>str;
    	CreateBTree(BT,str);
    	WPL=GetWPL(BT,n);
    	cout<<WPL;
    	return 0;
    }
    void CreateBTree(BTree &BT,string str){
    	queue<BTree>q;
    	BTree T;
    	int i=0;
    	if(str[1]=='#'||str[1]=='') BT=NULL;
    	else {
    		BT=new BTNode;
    		BT->data=str[1];
    		BT->lchild=BT->rchild=NULL;
    		q.push(BT);
    	}
    	i=2;
    	while(!q.empty()){
    		T=q.front();
    		q.pop();
    		if(T==NULL) ;
    		else if(str[i]=='#') {
    			T->lchild=NULL;
    			q.push(NULL);
    		}
    		else if(T!=NULL){
    			T->lchild=new BTNode;
    			T->lchild->data=str[i];
    			T->lchild->lchild=T->lchild->rchild=NULL;
    			q.push(T->lchild);
    		}
    		i++;
    		if(str[i]=='') break;
    		if(T==NULL) ;
    		else if(str[i]=='#') {
    			T->rchild=NULL;
    			q.push(NULL);
    		}
    		else if(T!=NULL){
    			T->rchild=new BTNode;
    			T->rchild->data=str[i];
    			T->rchild->lchild=T->rchild->rchild=NULL;
    			q.push(T->rchild);
    		}
    		i++;
    		if(str[i]=='') break;
    	}
    }
    int GetWPL(BTree BT,int n){
    	static int WPL=0;
    	if(BT!=NULL){
    		if(BT->lchild==NULL&&BT->rchild==NULL){
    			WPL=WPL+(BT->data-'0')*n;
    		}
    		GetWPL(BT->lchild,n+1);
    		GetWPL(BT->rchild,n+1);
    	}
    	else return WPL;
    }
    

    在上机的时候不会做,树不会建是最为致命的,通过百度之后,知道这一题怎么做了

    5. 代码Git提交记录截图

  • 相关阅读:
    pygame--颜色变化
    pyQt绘图
    pyqt布局管理器
    java执行shell/cmd命令
    word公式编辑器公式
    pygame绘制文本
    2.add two number
    eltwise层
    crop层
    fcn
  • 原文地址:https://www.cnblogs.com/gongshunde/p/8995712.html
Copyright © 2011-2022 走看看