Artikel ini menjelaskan operasi umum pohon biner yang diterapkan oleh Java. Bagikan untuk referensi Anda, sebagai berikut:
impor java.util.arraydeque; impor java.util.queue; impor java.util.stack; // pembuatan pohon biner, rekursif non-rekursif traversal hirarkis traversal di depan, menengah dan belakang // node node node node {elemen; Simpul kiri; Node benar; node publik () {} node publik (elemen int) {this.element = elemen; }} // BinaryTreepublic Class Tree {// Creat Tree Dari Array Public Static Node Creattree (int [] Data, int i) {if (i> = data.length || data [i] == -1) return null; Node temp = node baru (data [i]); temp. left = creattree (data, i * 2 + 1); temp.right = creattree (data, i * 2 + 2); kembalikan suhu; } // pre-order traversal traversal public static void pre (node temp) {if (temp == null) return; System.out.print (temp.element + ""); pra (temp. left); pre (temp.right); } // mid-order traversal public static void mid (node temp) {if (temp == null) return; mid (temp. left); System.out.print (temp.element + ""); Mid (Temp.Right); } // Post-order terakhir traversal public static void last (node temp) {if (temp == null) return; terakhir (temp. left); terakhir (Temp.Right); System.out.print (temp.element + ""); } // pre1 pre-order traversal non-resursif statis void pre1 (node temp) {stack <node> stack = new stack <> (); while (temp! = null ||! stack.isempty ()) {while (temp! = null) {stack.push (temp); System.out.print (temp.element + ""); temp = temp.left; } if (! stack.isempty ()) {temp = stack.pop (). kanan; }}} // traversal in-order dari mid1 mid1 non-rekursif static void mid1 (node temp) {stack <node> stack = new stack <> (); while (temp! = null ||! stack.isempty ()) {while (temp! = null) {stack.push (temp); temp = temp.left; } if (! stack.isempty ()) {temp = stack.pop (); System.out.print (temp.element + ""); Temp. }}} // last1 pasca-order traversal non-rekursif statis statis last1 (node temp) {stack <node> stack = new stack <> (); Stack <node> stack2 = stack baru <> (); while (temp! = null ||! stack.isempty ()) {while (temp! = null) {stack.push (temp); stack2.push (temp); temp = temp.right; } if (! stack.isempty ()) {temp = stack.pop (). kiri; }} while (! stack2.isempty ()) system.out.print (stack2.pop (). elemen + ""); } // urutan lapisan ceng traversal public static void ceng (node temp) {if (temp == null) return; Antrian <node> antrian = arraydeque baru <> (); antrian.offer (temp); while (! queue.isempty ()) {temp = queue.poll (); System.out.print (temp.element + ""); if (temp. left! = null) antrian.offer (temp. if (temp.right! = null) antrian.offer (temp.right); }} // demo public static void main (string [] args) {int [] array = {1, 2, 3, 4, 5, 6, 7, -1, -1, 10, -1, -1, 13}; Node tree = creattree (array, 0); System.out.println ("Hasil tes wulin.com:"); pra (pohon); System.out.println (); pre1 (pohon); System.out.println (); mid (pohon); System.out.println (); mid1 (pohon); System.out.println (); mid1 (pohon); System.out.println (); terakhir (pohon); System.out.println (); last1 (pohon); System.out.println (); Ceng (pohon); }}Hasil Menjalankan:
Untuk informasi lebih lanjut tentang algoritma java, pembaca yang tertarik dengan situs ini dapat melihat topik: "struktur data java dan tutorial algoritma", "ringkasan tips node dom java", "ringkasan file operasi java dan direktori" dan "ringkasan tip operasi java cache" tips java "tips java" Tips "Java Cache Tips"
Saya harap artikel ini akan membantu pemrograman Java semua orang.