Implementación del árbol de búsqueda binaria con Java

Las características del árbol de búsqueda binaria:

1. El valor del nodo principal es mayor que el valor del subárbol izquierdo; el valor del nodo principal es menor o igual que el subárbol derecho

La forma de atravesar el árbol:

1. Cruce de primer orden: primero atraviesa el nodo raíz, luego atraviesa el subárbol izquierdo y finalmente atraviesa el subárbol derecho

2. Cruce de orden medio: el orden medio atraviesa el subárbol izquierdo, luego atraviesa el nodo raíz y finalmente atraviesa el subárbol derecho

3. Recorrido posterior al pedido: el pedido posterior atraviesa el subárbol izquierdo, luego el subárbol derecho y finalmente el nodo raíz

Código:

package com.phome.mapdemo.treedemo;

public class BinarySearchTree {
    // 定义一个二叉树节点
    private BinaryTree binaryTree;
    // 二叉树中总共有几个节点
    private int size;

    public int getSize() {
        return size;
    }

    public BinaryTree getBinaryTree() {
        return binaryTree;
    }

    // 插入元素(默认)
    public void add(int data){
        // 如果二叉树的父节点是空的则初始化一个二叉树
        BinaryTree newTree = new BinaryTree(data,null,null,null);
        if (binaryTree == null){
            binaryTree = newTree;
            size++;
            return;
        }
        BinaryTree parent = binaryTree;
        BinaryTree root = null;
        while(true){
            if (parent == null)
                break;
            root = parent;
            if(data < parent.data){
                parent = parent.left;
            }else {
                parent = parent.right;
            }
        }
        if (data < root.data){
            root.left = newTree;
            size++;
        }else {
            root.right = newTree;
            size++;
        }
    }
    // 先序遍历
    public void prePrintTree(BinaryTree binaryTree){
        if(binaryTree != null){
            System.out.print(binaryTree.data+" ");
            prePrintTree(binaryTree.left);
            prePrintTree(binaryTree.right);
        }
    }
    // 中序遍历
    public void inOrderPrintTree(BinaryTree binaryTree){
        if(binaryTree != null){
            inOrderPrintTree(binaryTree.left);
            System.out.print(binaryTree.data + " ");
            inOrderPrintTree(binaryTree.right);
        }
    }
    // 后序遍历
    public void postOrderPrintTree(BinaryTree binaryTree){
        if(binaryTree != null){
            postOrderPrintTree(binaryTree.left);
            postOrderPrintTree(binaryTree.right);
            System.out.print(binaryTree.data + " ");
        }
    }
    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.add(10);
        binarySearchTree.add(9);
        binarySearchTree.add(11);
        binarySearchTree.add(3);
        binarySearchTree.add(5);
        binarySearchTree.add(8);
        binarySearchTree.add(2);
        System.out.println("数的节点:"+binarySearchTree.getSize());
        System.out.println("先序遍历:");
        binarySearchTree.prePrintTree(binarySearchTree.getBinaryTree());
        System.out.println();
        System.out.println("中序遍历:");
        binarySearchTree.inOrderPrintTree(binarySearchTree.getBinaryTree());
        System.out.println();
        System.out.println("后序遍历:");
        binarySearchTree.postOrderPrintTree(binarySearchTree.getBinaryTree());

    }
}


class BinaryTree{
    public int data;
    public BinaryTree parent;
    public BinaryTree left;
    public BinaryTree right;

    public BinaryTree(int data, BinaryTree parent, BinaryTree left, BinaryTree right) {
        this.data = data;
        this.parent = parent;
        this.left = left;
        this.right = right;
    }

    public BinaryTree() {
        this(0,null,null,null);
    }

    @Override
    public String toString() {
       return "data:["+data+"]";
    }
    
}

Resultados del:

数的节点:7
先序遍历:
10 9 3 2 5 8 11 
中序遍历:
2 3 5 8 9 10 11 
后序遍历:
2 8 5 3 9 11 10 

 

Supongo que te gusta

Origin blog.csdn.net/u013804636/article/details/107911991
Recomendado
Clasificación