Cet article décrit les opérations communes des arbres binaires mis en œuvre par Java. Partagez-le pour votre référence, comme suit:
Importer java.util.arraydeque; import java.util.queue; import java.util.stack; // la création d'arbres binaires, la piste de traverse de traverse de traversée de traversée de traversée de traversée, intermédiaire et arrière // nœud nœud {int; Nœud à gauche; Node à droite; Node public () {} public Node (int élément) {this.element = élément; }} // BinaryTreepublic Class Tree {// Créer une arborescence à partir du Array Public Static Node CreatTree (int [] data, int i) {if (i> = data.length || data [i] == -1) return null; Node temp = nouveau nœud (data [i]); Temp.Left = CreatTree (données, i * 2 + 1); Temp.Right = CreatTree (données, i * 2 + 2); Tempère de retour; } // Pré-commande Pré-commande Traversal Recursif public static void pre (nœud temp) {if (temp == null) return; System.out.print (temp.Element + ""); pre (temp.); pre (temp.); } // Mid-Order Traversal récursif public static void mid (nœud temp) {if (temp == null) return; MID (Temp.Left); System.out.print (temp.Element + ""); Mid (temp.); } // Dernière post-ordre RECURSAL RECursif public static void Last (Node temp) {if (temp == null) return; dernier (temp.); dernier (temp.); System.out.print (temp.Element + ""); } // Pre1 Précommande Transfert Public non réécite STATIQUE VOID PRE1 (TEMP NODE) {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 (). droite; }}} // Traversion dans l'ordre de Mid1 non rérécise publique 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 Post-Order Traversal Non-Recursif public static void Last1 (Node temp) {Stack <Node> Stack = new Stack <> (); Stack <Node> Stack2 = new Stack <> (); while (temp! = null ||! stack.isempty ()) {while (temp! = null) {stack.push (temp); stack2.push (temp); temp = temp.Right; } if (! stack.isempty ()) {temp = stack.pop (). Left; }} while (! stack2.isempty ()) System.out.print (stack2.pop (). élément + ""); } // Ceng Layer Séquence Traversal public static void Ceng (nœud temp) {if (temp == null) return; File d'attente <Node> queue = new ArrayDeque <> (); file d'attente.offer (temp); while (! queue.isempty ()) {temp = queue.poll (); System.out.print (temp.Element + ""); if (temp.left! = null) file d'attente.offer (temp.left); if (temp. }} // 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 ("Wulin.com Résultats du test:"); pre (arbre); System.out.println (); pre1 (arbre); System.out.println (); Mid (arbre); System.out.println (); MID1 (arbre); System.out.println (); MID1 (arbre); System.out.println (); dernier (arbre); System.out.println (); Last1 (arbre); System.out.println (); Ceng (arbre); }}Résultats en cours:
Pour plus d'informations sur les algorithmes Java, les lecteurs qui sont intéressés par ce site peuvent afficher les sujets: "Structure de données Java et tutoriel d'algorithme", "Résumé des conseils de nœud de Dom Operation Java", "Résumé du fichier Java et des conseils d'opération de répertoire" et "Résumé des conseils d'opération Java Cache"
J'espère que cet article sera utile à la programmation Java de tous.