ugrás a tartalomhoz

Kiegyensúlyozott-e az adott bináris fa

morocztamas · 2012. Feb. 22. (Sze), 16.50
Egy bináris fáról, hogyan tudom megállapítani, hogy kiegyensúlyozott-e és aztán azzá tenni?
Eddig a forrás ilyen:
  1. package binfa;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileReader;  
  6. import java.util.Scanner;  
  7.   
  8. public class Binfa {  
  9.   public static void main(String[] args) throws FileNotFoundException {  
  10.     RendezettBinfa rbf2 = new RendezettBinfa();  
  11.     Scanner s = null;  
  12.     try {  
  13.       s = new Scanner (  
  14.               new BufferedReader (  
  15.                       new FileReader ("szamok.txt")));  
  16.       while (s.hasNextInt()) {  
  17.         rbf2.addElem(s.nextInt());  
  18.       }  
  19.     } finally {  
  20.       if (s != null) {  
  21.         s.close();  
  22.       }  
  23.     }  
  24.     System.out.println(rbf2.inorder());  
  25.     System.out.print("Magassága: ");  
  26.     System.out.println(rbf2.height());  
  27.     System.out.println();  
  28.   
  29.     /**/  
  30.       
  31.     RendezettBinfa rbf = new RendezettBinfa();  
  32.     rbf.addElem(4);  
  33.     rbf.addElem(2);  
  34.     rbf.addElem(1);  
  35.     rbf.addElem(3);  
  36.     rbf.addElem(6);  
  37.     rbf.addElem(5);  
  38.     rbf.addElem(7);  
  39.     System.out.println(rbf); // inorder eljárás, mert a gyökér a bal- és a jobbrészfa kiírása között van   
  40.     System.out.print("Inorder: ");  
  41.     System.out.println(rbf.inorder());  
  42.     System.out.print("Postorder: ");  
  43.     System.out.println(rbf.postorder());  
  44.     System.out.print("Magassága: ");  
  45.     System.out.println(rbf.height());  
  46.     System.out.print("Preorder: ");  
  47.     System.out.println(rbf.preorder());  
  48.       
  49.     /* 
  50.     Csucs cs = new Csucs(5); 
  51.     Csucs bcs = new Csucs(6); 
  52.     Csucs jcs = new Csucs(7); 
  53.     cs.addLeft(bcs); 
  54.     cs.addRight(jcs); 
  55.     System.out.println(cs); // *.toString 
  56.     cs.setElem(1); 
  57.     cs.bcs.setElem(2); 
  58.     jcs.setElem(3); 
  59.     System.out.println(cs); 
  60.     // System.out.println(jcs); 
  61.     */  
  62.   }  
  63. }  
  1. package binfa;  
  2.   
  3. public class Csucs {  
  4.   protected int elem;  
  5.   Csucs bcs, jcs;  
  6.     
  7.   public Csucs (int elem) {  
  8.     this.elem = elem;  
  9.     bcs = null;  
  10.     jcs = null;  
  11.   }  
  12.     
  13.   public void addLeft (Csucs cs) {  
  14.     bcs = new Csucs(cs.getElem());  
  15.   }  
  16.     
  17.   public void addRight (Csucs cs) {  
  18.     jcs = new Csucs(cs.getElem());  
  19.   }  
  20.     
  21.   @Override  
  22.   public String toString () {  
  23.     String s = "" + elem;  
  24.     if (bcs != null) {  
  25.       s += ", " + bcs.toString();  
  26.     }  
  27.     if (jcs != null) {  
  28.       s += ", " + jcs.toString();  
  29.     }  
  30.     return s;  
  31.   }  
  32.     
  33.   public void setElem (int elem) {  
  34.     this.elem = elem;  
  35.   }  
  36.     
  37.   public int getElem () {  
  38.     return elem;  
  39.   }  
  40. }  
  1. package binfa;  
  2.   
  3. public class RendezettBinfa {  
  4.   Csucs gy;  
  5.   RendezettBinfa balFa, jobbFa;  
  6.     
  7.   public RendezettBinfa () {  
  8.     gy = null;  
  9.     balFa = null;  
  10.     jobbFa = null;  
  11.   }  
  12.     
  13.   public void addElem (int elem) {  
  14.     if (gy == null) {  
  15.       gy = new Csucs(elem);  
  16.     } else if (gy.getElem() >= elem) {  
  17.       if (balFa == null) {  
  18.         balFa = new RendezettBinfa();  
  19.       }  
  20.       balFa.addElem(elem);  
  21.     } else {  
  22.       if (jobbFa == null) {  
  23.         jobbFa = new RendezettBinfa();  
  24.       }  
  25.       jobbFa.addElem(elem);  
  26.     }  
  27.   }  
  28.     
  29.   public int height () {  
  30.     if (balFa == null && jobbFa == null) {  
  31.       return 0;  
  32.     } else if (balFa == null) {  
  33.       return jobbFa.height() + 1;  
  34.     } else if (jobbFa == null) {  
  35.       return balFa.height() + 1;  
  36.     } else {  
  37.       int b = balFa.height(), j = jobbFa.height();  
  38.       return (b < j ? j : b) + 1;  
  39.     }  
  40.   }  
  41.     
  42.   public String preorder () {  
  43.     String s = "";  
  44.     if (gy != null) {  
  45.       s += gy.getElem() + ", ";  
  46.       if (balFa != null) {  
  47.         s += balFa.preorder(); // Mint a faktoriális függvénynél. n! = (n - 1)! * n  
  48.       }  
  49.       if (jobbFa != null) {  
  50.         s += jobbFa.preorder();  
  51.       }  
  52.     }  
  53.     return s;  
  54.   }  
  55.     
  56.   public String inorder () {  
  57.     String s = "";  
  58.     if (balFa != null) {  
  59.       s += balFa.inorder();  
  60.     }  
  61.     if (gy != null) {  
  62.       s += gy.getElem() + ", ";  
  63.     }  
  64.     if (jobbFa != null) {  
  65.       s += jobbFa.inorder();  
  66.     }  
  67.     return s;  
  68.   }  
  69.     
  70.   public String postorder () {  
  71.     String s = "";  
  72.     if (balFa != null) {  
  73.       s += balFa.postorder();  
  74.     }  
  75.     if (jobbFa != null) {  
  76.       s += jobbFa.postorder();  
  77.     }  
  78.     if (gy != null) {  
  79.       s += gy.getElem() + ", ";  
  80.     }  
  81.     return s;  
  82.   }  
  83.     
  84.   @Override  
  85.   public String toString () { // Inorder  
  86.     String s = "";  
  87.     if (balFa != null) {  
  88.       s += balFa.toString();  
  89.     }  
  90.     if (gy != null) {  
  91.       s += gy.getElem() + ", ";  
  92.     }  
  93.     if (jobbFa != null) {  
  94.       s += jobbFa.toString();  
  95.     }  
  96.     return s;  
  97.   }  
  98. }  
 
1

How to determine if binary

Poetro · 2012. Feb. 22. (Sze), 17.23
2

Fura, nincs java

inf · 2012. Feb. 22. (Sze), 18.26
Fura, nincs java kódszínezőnk? Nálam nem működik... :S
3

Úgy tűnik. A multkor én is

kuka · 2012. Feb. 23. (Cs), 10.24
Úgy tűnik. A multkor én is JavaScript színezést kértem a Java kódhoz. Kipróbáltam, a fentebbi kód is jól mutat JavaScript színezéssel. Lehet ez az a kivétel amelyik erősíti a Java!=JavaScript szabályt.
4

Átszíneztem js-re, elmegy,

inf · 2012. Feb. 23. (Cs), 18.54
Átszíneztem js-re, elmegy, jobb, mint a semmi...
Egyébként valszeg azért hasonló, mert ugyanazok a foglalt szavak...