zoukankan      html  css  js  c++  java
  • 【数据结构第三次考试(树)】On PTA

    判断题

    1-1
    关于树和二叉树
    二叉树是度为 2 的树。(2分)

    F
    

    1-2
    二叉树通常有顺序存储结构和链式存储结构。 (2分)

    T
    

    1-3
    一棵有124个结点的完全二叉树,其叶结点个数是确定的。 (2分)

    T
    

    1-4
    某二叉树的后序和中序遍历序列正好一样,则该二叉树中的任何结点一定都无右孩子。 (2分)

    T
    

    1-5
    堆排序是稳定的。 (2分)

    F
    

    1-6
    哈夫曼编码是一种最优的前缀码。对一个给定的字符集及其字符频率,其哈夫曼编码不一定是唯一的,但是每个字符的哈夫曼码的长度一定是唯一的。(2分)

    F
    

    1-7
    对一棵二叉排序树按前序方法遍历得出的结点序列是从小到大的序列。 (2分)

    F
    

    1-8
    对AVL树中的任一结点,其左子树的高度一定比其右子树的高度要高。 (2分)

    F
    

    选择题

    2-1
    一棵二叉树中,双分支结点数为15,单分支结点数为30,则叶子结点数为()个。 (3分)
    1. 15
    2. 16
    3. 17
    4. 47

    2
    

    2-2
    先序遍历图示二叉树的结果为 (3分)

    1.	A,B,C,D,H,E,I,F,G
    2.	A,B,D,H,I,E,C,F,G
    3.	H,D,I,B,E,A,F,C,G
    4.	H,I,D,B,E,F,G,A,C
    
    2
    

    2-3
    已知一棵完全二叉树的第9层(设根为第1层)有100个叶结点,则该完全二叉树的结点个数最多是: (3分)
    1. 311
    2. 823
    3. 1847
    4. 无法确定

    2
    

    2-4
    以二叉链表作为二叉树的存储结构,在具有 n 个结点的二叉链表中(n>0),空链域的个数为 __ (3分)
    1. n+1
    2. n
    3. n−1
    4. 无法确定

    1
    

    2-5
    对N个记录进行堆排序,最坏的情况下时间复杂度是: (3分)
    1. O(logN)
    2. O(N)
    3. O(NlogN)
    4. O(N^2)

    2
    

    2-6
    一组记录为(46,79,56,38,40,84)则利用堆排序的方法建立的初始大根堆为( ) (3分)
    1. 79,46,56,38,40,84
    2. 84,79,56,38,40,46
    3. 84,79,56,46,40,38
    4. 84,56,79,40,46,38

    3
    

    2-7
    如果二叉树的后序遍历结果是FDEBGCA,中序遍历结果是FDBEACG,那么该二叉树的前序遍历结果是什么? (3分)
    1. ABCDEFG
    2. ABDFEGC
    3. ABDFECG
    4. ABDEFCG

    2
    

    2-8
    将{ 32, 2, 15, 65, 28, 10 }依次插入初始为空的二叉排序树,则该树的中序遍历结果是。 (3分)
    1. 2, 10, 15, 28, 32, 65
    2. 32, 2, 10, 15, 28, 65
    3. 10, 28, 15, 2, 65, 32
    4. 32, 2, 15, 10, 28, 65

    3
    

    2-9
    引入二叉线索树的目的是( )。 (3分)
    1. 加快查找结点的前驱或后继的速度
    2. 为了能在二叉树中方便的进行插入与删除
    3. 为了能方便的找到双亲
    4. 使二叉树的遍历结果唯一

    1
    

    2-10
    设森林T中有4棵树,第一、二、三、四棵树的结点个数分别是n1,n2,n3,n4,那么当把森林T转换成一棵二叉树后,且根结点的左子树上有( )个结点。 (3分)
    1. n1-1
    2. n1
    3. n1+n2+n3
    4. n2+n3+n4

    1
    

    2-11
    根据使用频率为5个字符设计的哈夫曼编码不可能是( )。 (3分)
    1. 111,110,10,01,00
    2. 000,001,010,011,1
    3. 100,11,10,1,0
    4. 001,000,01,11,10

    4
    

    2-12
    已知权值集合为{5,7,2,3,6,1,4},计算带权路径长度WPL()。 (3分)
    1. 73
    2. 74
    3. 75
    4. 76

    2
    

    程序填空题

    5-1 计算二叉树的深度

    
    ```cpp
    #include<iostream>
    using namespace std;
    
    typedef struct BiNode
    {				
    	char data;					
    	struct BiNode *lchild,*rchild;	
    }BiTNode,*BiTree;
    
    
    void CreateBiTree(BiTree &T)
    {	
    	char ch;
    	cin >> ch;
    	if(ch=='#')  T=NULL;			
    	else{							
    		T=new BiTNode;
    		T->data=ch;					
    		CreateBiTree(T->lchild);	
    		CreateBiTree(T->rchild);	
    	}								
    }								
    
    int Depth(BiTree T)
    { 
    	int m,n;
    	if(!T(4)) return 0;        
    	else 
    	{							
    		m=Depth(T->lchild)(4);			
    		n=Depth(T->rchild)(4);			
    		if(m>n) return(m+1);		
    		else return (n+1);
    	}
    }
    
    int main()
    {
    	BiTree tree;
    	CreateBiTree(tree);
    	cout<<Depth(tree);
    	return 0;
    
    }
    

    5-2 实现搜索二叉树

    #include<iostream>
    using namespace std;
    
    typedef struct ElemType{	
    	int key;
    }ElemType;
    
    typedef struct BSTNode{
    	ElemType data;
    	BSTNode *lchild,*rchild;
    }BSTNode,*BSTree;
    
    int flag=1;
    
    BSTree SearchBST(BSTree T,char key) {
      if(T==null || key==T->key(4)) return T;
      else if (key<T->data.key)  SearchBST(T->lchild, key)(4);
      else SearchBST(T->rchild, key)(4);
    }
    
    void InsertBST(BSTree &T,ElemType e )//实现细节隐藏
    
    void CreateBST(BSTree &T ) {
      int i=1,n;
      cin >> n;
      T=NULL;
      ElemType e;
      while(i<=n){
        cin>>e.key;  	
        InsertBST(T, e);
    	i++;
      }            
    }
    
    int main()
    {
    	BSTree T;
    	CreateBST(T);
    	int key;
    	cin>>key;
    	BSTree result=SearchBST(T,key);
    	if(result)
    	{cout<<"find!";}
    	else
    	{cout<<"not find!";}
    	return 0;
    }
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char a[1000];
    int ant = 0,c = 0;
    typedef struct Node
    {
        char Data;
        struct Node *Left,*Right;
    }Node;
    Node *CreatNode()
    {
        Node *p = (Node *)malloc(sizeof(Node));
        p -> Left = p -> Right = NULL;
        return p;
    }
    Node *CreatTree()
    {
        if(a[ant] == '#')
        {
            ant ++;
            return NULL;
        }
        Node *head = CreatNode();
        head -> Data = a[ant ++];
        head -> Left = CreatTree();
        head -> Right = CreatTree();
        if(head -> Left == NULL && head -> Right == NULL)c ++;
        return head;
    }
    void InOrder(Node *T)
    {
        if(T == NULL)return;
        InOrder(T -> Left);
        putchar(T -> Data);
        InOrder(T -> Right);
    }
    int main()
    {
        scanf("%s",a);
        Node *head = CreatTree();
        InOrder(head);
        putchar('
    ');
        putchar('0'+c);
    }
    
  • 相关阅读:
    node实现将打包后的文件转压缩包
    Git/SVN忽略node_modules文件
    node实现发送邮件
    node搜索文件夹下的指定内容
    node批量修改文件文本内容
    微信小程序上线发布需要做的事情
    两件事 Jquery.form 锁
    .NET MVC 提交表单出现检测到有潜在危险的Request.Form值
    第一次使用TinyMCE
    第一次使用Entity Framework 的CodeFirst
  • 原文地址:https://www.cnblogs.com/SiriusZHT/p/14310799.html
Copyright © 2011-2022 走看看