1 / 13

Trees, Vectors, Iterators

Trees, Vectors, Iterators. ADT. Abstract Data Type (ADT) vs implementation -Soyut Veri Türleri -Uygulamaları. Ağaçlar ( Trees ). Listelerde olduğu gibi ağaçlar da birçok elemandan (Node) oluşur.

Télécharger la présentation

Trees, Vectors, Iterators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Trees, Vectors, Iterators

  2. ADT • Abstract Data Type (ADT) vs implementation-Soyut Veri Türleri -Uygulamaları

  3. Ağaçlar (Trees) • Listelerde olduğu gibi ağaçlar da birçok elemandan (Node) oluşur. • Yaygın bir ağaç şekli ikili ağaçlardır (Binary Trees). Bu ağaçlarda herbir eleman diğer iki elemana referans taşır. public class Agac { Object veri; Agacsol, sag; }

  4. Ağacın tepesi kök, diğer elemanlar dal, null referans taşıyan elemanlar ise yaprak olarak adlandırılır.

  5. Kurucu metod public Agac(Object veri, Agac sol, Agac sag) { this.veri = veri;         this.sol = sol;         this.sag = sag;} • Önce çocuk elemanları oluşturalım: • Agac sol = new Agac (new Integer(2), null, null); Agac sag = new Agac (new Integer(3), null, null); • Kök elemanın oluşturulması: • Agacagac = new Agac (new Integer(1), sol, sag);

  6. Özyineleme metoduyla ağaç elemanlarına ulaşılması     public static int total (Agac agac) {         if (agac == null) return 0;         Integer veri = (Integer) agac.veri;         return veri.intValue() + total (agac.sol) + total (agac.sag);  }

  7. İfade ağaçları (Expression Trees) • 1 + 2 * 3 • Çarpma işleminin toplama işleminden önce yapılacağı bilinmezse bu işlemin sonucu belirsizdir.

  8. Ağaçların yazdırılması • Ağacın kökünden başlamak üzere elemanlarını yazdırma: • public static void print (Agacagac) {         if (agac == null) return; System.out.print (agac + " ");         print (agac.sol);         print (agac.sag);   } + 1 * 2 3

  9. Önce dalların sonra kök elemanın yazdırılması:     public static void printPostorder (Agacagac) {         if (agac == null) return; printPostorder (agac.sol); printPostorder (agac.sag); System.out.print (agac + " ");     } 1 2 3 * +

  10. Önce sol, sonra kök sonra da sağ ağacın yazdırılması:     public static void printInorder (Agacagac) {         if (agac == null) return; printInorder (agac.sol); System.out.print (agac + " "); printInorder (agac.sag);     } 1 + 2 * 3

  11. Vector Class • Vector Sınıfı dinamik olarak boyut değiştirebilen Object dizisi işlevi görür. • Size vektörün tuttuğu eleman sayısını • Capacity vektörün işgal ettiği yeri gösterir • v.add(Object o): eleman ekleme • v.get(index): herhangi bir indexteki elemanı alma. Geriye Object döndürür

  12. Iterators Iterator iterator = v.iterator (); while (iterator.hasNext ()) { System.out.println (iterator.next ()); } ListIterator iter = v.listIterator(); while (iter.hasNext()) { System.out.println((String)iter.next()); }

More Related