Advertisement
Guest User

Лаб34

a guest
Dec 21st, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.72 KB | None | 0 0
  1. Речник---------------------------------------------------------------------------------------------------------------------------
  2. package SLLHashTables;
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class recnik {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner in=new Scanner(System.in);
  10.         System.out.println("Vnesi kolku zborovi kje ima recnikot. . .");
  11.         int n=Integer.parseInt(in.nextLine());     
  12.         SLLHT<String,String> tabela=new SLLHT<String,String>(n);
  13.         for(int i=0;i<n;i++){
  14.         String s;
  15.         s=in.nextLine();
  16.         String[] s1=s.split(" ");
  17.         tabela.insert(s1[0].toLowerCase(), s1[1].toLowerCase());
  18.         }
  19.         System.out.println("Vnesi go tekstot. . .");
  20.         String s2;
  21.         s2=in.nextLine();
  22.         String[] s3=s2.split(" ");
  23.         for(int i=0;i<s3.length;i++){
  24.             s3[i]=s3[i].toLowerCase();
  25.             if(s3[i].charAt(s3[i].length()-1)=='.'||s3[i].charAt(s3[i].length()-1)=='!'||s3[i].charAt(s3[i].length()-1)=='?'||s3[i].charAt(s3[i].length()-1)==',')
  26.                 s3[i]=s3[i].substring(0, s3[i].length()-1);
  27.             if(tabela.find(s3[i].toLowerCase())!=null){
  28.                 System.out.print(tabela.find(s3[i].toLowerCase()).info.value +" ");
  29.             }
  30.             if(tabela.find(s3[i].toLowerCase())==null){
  31.                 System.out.print("? ");
  32.             }
  33.         }
  34.  
  35.        
  36.        
  37.     }
  38.  
  39. }
  40. Висина | Број на јазли | Дедо-внук ----------------------------------------------------------------------------------------------
  41. package binarysearchtrees;
  42.  
  43. import java.lang.reflect.Array;
  44.  
  45.  
  46. public class rekurzijaVisina {
  47.    
  48.     static int rekurzija(BNode<Integer> root){
  49.         int l;
  50.         int d;
  51.         if (root==null) return -1;
  52.         else {
  53.             d= rekurzija(root.right)+1;
  54.             l= rekurzija(root.left)+1;
  55.         }
  56.         if(d>l)return d;
  57.         else return l;
  58.     }
  59.  
  60.     static int jazlirekurzija(BNode<Integer> root){
  61.         if (root==null) return 0;
  62.         else
  63.             return 1+jazlirekurzija(root.right)+jazlirekurzija(root.left);
  64.         }
  65.     static BNode<Integer> binarno(int [] nizica,int poc, int kraj){
  66.         int n=(poc+kraj)/2;
  67.         if (poc==kraj){
  68.         return new BNode<Integer>(nizica[n]);
  69.         }
  70.         else{
  71.             BNode<Integer> pom=new BNode<Integer>(nizica[n]);
  72.             pom.left=binarno(nizica,poc,n-1);
  73.             pom.right=binarno(nizica,n+1,kraj);
  74.             return pom;
  75.         }
  76.  
  77.     }
  78.     static void dedo(BNode<Integer> root){
  79.         if(root.right==null&&root.left==null){
  80.         return;
  81.         }
  82.         else {
  83.             if(root.right!=null){
  84.             if(root.right.right!=null)
  85.                 System.out.println(root.right.right.info+" e vnuk na "+root.info);
  86.             if(root.right.left!=null)
  87.                 System.out.println(root.right.left.info+" e vnuk na "+root.info);
  88.             }
  89.             if(root.left!=null){
  90.             if(root.left.left!=null)
  91.                 System.out.println(root.left.left.info+" e vnuk na "+root.info);
  92.             if(root.left.right!=null)
  93.                 System.out.println(root.left.right.info+" e vnuk na "+root.info);
  94.             }
  95.             if(root.left!=null) dedo(root.left);
  96.             if(root.right!=null) dedo(root.right);
  97.         }
  98.     }
  99.     static int dedo1(BNode<Integer> root){
  100.         int n1=0;
  101.         int n2=0;
  102.         int n3=0;
  103.         int n4=0;
  104.  
  105.         if(root.right==null&&root.left==null){
  106.         return 0;
  107.         }
  108.         else {
  109.             if(root.right!=null){
  110.             if(root.right.right!=null)
  111.                 n1=1;
  112.             if(root.right.left!=null)
  113.                 n2=1;
  114.             }
  115.             if(root.left!=null){
  116.             if(root.left.left!=null)
  117.                 n3=1;
  118.             if(root.left.right!=null)
  119.                 n4=1;
  120.             }
  121.             if(root.right!=null&&root.left!=null)
  122.                 return n1+n2+n3+n4+dedo1(root.left)+dedo1(root.right);
  123.             else if(root.right==null&&root.left!=null)
  124.                 return n1+n2+n3+n4+dedo1(root.left);
  125.             else if(root.right!=null&&root.left==null)
  126.                 return n1+n2+n3+n4+dedo1(root.right);
  127.     }
  128.         return dedo1(root.right)+dedo1(root.left);
  129.     }
  130.  
  131.  
  132.     public static void main(String[] args) {
  133.         int [] a= {1,2,4,6,7,10,15};
  134.         BSTree <Integer> tree = new BSTree<Integer>();      
  135.         tree.root= binarno(a, 0, a.length-1);
  136.         tree.inorder();
  137.         BSTree <Integer> tree1 = new BSTree<Integer>();
  138.         BNode<Integer> b1=new BNode<Integer>(2);
  139.         BNode<Integer> b2=new BNode<Integer>(7);
  140.         BNode<Integer> b3=new BNode<Integer>(5);
  141.         BNode<Integer> b4=new BNode<Integer>(1);
  142.         BNode<Integer> b5=new BNode<Integer>(6);
  143.         BNode<Integer> b6=new BNode<Integer>(9);
  144.         BNode<Integer> b7=new BNode<Integer>(5);
  145.         BNode<Integer> b8=new BNode<Integer>(11);
  146.         BNode<Integer> b9=new BNode<Integer>(4);
  147.         b1.left=b2;
  148.         b1.right=b3;
  149.         b2.left=b4;
  150.         b2.right=b5;
  151.         b5.left=b7;
  152.         b5.right=b8;
  153.         b3.right=b6;
  154.         b6.left=b9;
  155.         tree1.root=b1;
  156.         dedo(b1);
  157.         System.out.println(dedo1(b1));
  158.     BNode<Integer> root=new BNode<Integer>(1);
  159.     BNode<Integer> a1=new BNode<Integer>(1);
  160.     BNode<Integer> a2=new BNode<Integer>(1);
  161.     BNode<Integer> a3=new BNode<Integer>(1);
  162.     BNode<Integer> a4=new BNode<Integer>(1);
  163.     root.left=a1;
  164.     a1.left=a2;
  165.     a1.right=a3;
  166.     a3.left=a4;
  167.     System.out.println(rekurzija(root));
  168.     System.out.println(jazlirekurzija(root));
  169.  
  170.  
  171.     }
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement