Este artigo descreve as operações comuns de árvores binárias implementadas por Java. Compartilhe -o para sua referência, como segue:
importar java.util.arraydeque; importar java.util.queue; importar java.util.stack; // A criação de árvores binárias, recursivo sequência hierárquica não recursiva travessia de nó nó nó nó {int elemento; Nó esquerdo; Nó à direita; public node () {} public node (int elemento) {this.element = element; }} // Árvore de classe Bininartrepublic {// Criar árvore da matriz public static node critTree (int [] dados, int i) {if (i> = data.length || data [i] == -1) return null; Nó temp = novo nó (dados [i]); temp.left = criative (dados, i * 2 + 1); temp.right = criative (dados, i * 2 + 2); retornar temp; } // pré-encomenda pré-encomenda recursiva public estática Void Pre (Temp) {if (temp == null) retornar; System.out.print (temp.Element + ""); pré (temp.left); pré (temp.right); } // Mid-Order Traversal Recursive Public Static Void MID (TEMP) {if (temp == null) retornar; meio (temp.left); System.out.print (temp.Element + ""); meio (temp.right); } // ÚLTIMA POST-PRONTRAVERSAL Recursive Recursive Public Static Void Last (nó temp) {if (temp == null) retornar; último (temp.left); último (temp.right); System.out.print (temp.Element + ""); } // pré-1 pré-encomenda não-recursiva public estática public estática Void Pre1 (Temp do nó) {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 (). Right; }}} // Travessal de encomenda da MID1 Public estático não-Recursivo Void Mid1 (Temp do nó) {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.right; }}} // Last1 Pós-ordem post-encerrsal não-recursivo estático público estático last1 (temp nó) {Stack <Node> Stack = new Stack <> (); Pilha <Node> pilha2 = 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 (). Element + ""); } // sequência de camada ceng public estática de sequência de ceng (temp nó) {if (temp == null) retornar; Fila <Node> fila = new ArrayDeque <> (); fila.offer (temp); while (! System.out.print (temp.Element + ""); if (temp.left! = null) fila.offer (temp.left); if (temp.right! = null) fila.offer (temp.right); }} // Demo público estático void main (String [] args) {int [] Array = {1, 2, 3, 4, 5, 6, 7, -1, -1, 10, -1, -1, 13}; Nó árvore = criador (matriz, 0); System.out.println ("Wulin.com Resultados dos testes:"); pré (árvore); System.out.println (); pre1 (árvore); System.out.println (); meio (árvore); System.out.println (); Mid1 (árvore); System.out.println (); Mid1 (árvore); System.out.println (); Última (árvore); System.out.println (); last1 (árvore); System.out.println (); ceng (árvore); }}Resultados em execução:
Para obter mais informações sobre os algoritmos Java, os leitores interessados neste site podem visualizar os tópicos: "Estrutura de dados Java e tutorial de algoritmo", "Resumo das dicas de nó da operação Java Dom", "Resumo de dicas de operação de Java e Operação de Java" e "Resumo de Java cache" Tips "TIPS"
Espero que este artigo seja útil para a programação Java de todos.