普通二叉树java实现

package com.concurrency.song.example.tree;

import java.util.LinkedList;
import java.util.List;

/**
 * @date 2019/6/5 10:07
 *
 * 二叉树
 */
public class BinTreeTraverse2 {
    private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    private static List<Node> nodeList = null;

    /**
     * 内部类,节点信息
     * */
    private static class Node{
        Node leftChild;
        Node rightChild;
        int data;

        Node(int newData){
            leftChild = null;
            rightChild = null;
            data = newData;
        }
    }

    public void createBinTree(){
        nodeList = new LinkedList<Node>();
        //将一个数组的值依次转换为Node的节点
        for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++){
            nodeList.add(new Node(array[nodeIndex]));
        }
        // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
        for (int parentIndex = 0;parentIndex < array.length / 2 -1;parentIndex++){
            //左孩子
            nodeList.get(parentIndex).leftChild = nodeList.get(parentIndex * 2 + 1);
            //右孩子
            nodeList.get(parentIndex).rightChild = nodeList.get(parentIndex * 2 + 2);
        }
        //最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
        int lastParentIndex = array.length / 2 - 1;
        // 左孩子
        nodeList.get(lastParentIndex).leftChild = nodeList
                .get(lastParentIndex * 2 + 1);
        // 右孩子,如果数组的长度为奇数才建立右孩子
        if (array.length % 2 == 1) {
            nodeList.get(lastParentIndex).rightChild = nodeList
                    .get(lastParentIndex * 2 + 2);
        }
    }

    /**
     * 先序遍历
     *
     * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
     *
     * @param node
     *   遍历的节点
     */
    public static void preOrderTraverse(Node node) {
        if (node == null)
            return;
        System.out.print(node.data + " ");
        preOrderTraverse(node.leftChild);
        preOrderTraverse(node.rightChild);
    }

    /**
     * 中序遍历
     *
     * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
     *
     * @param node
     *            遍历的节点
     */
    public static void inOrderTraverse(Node node) {
        if (node == null)
            return;
        inOrderTraverse(node.leftChild);
        System.out.print(node.data + " ");
        inOrderTraverse(node.rightChild);
    }

    /**
     * 后序遍历
     *
     * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
     *
     * @param node
     *            遍历的节点
     */
    public static void postOrderTraverse(Node node) {
        if (node == null)
            return;
        postOrderTraverse(node.leftChild);
        postOrderTraverse(node.rightChild);
        System.out.print(node.data + " ");
    }

    public static void main(String[] args) {
        BinTreeTraverse2 binTree = new BinTreeTraverse2();
        binTree.createBinTree();
        // nodeList中第0个索引处的值即为根节点
        Node root = nodeList.get(0);

        System.out.println("先序遍历:");
        preOrderTraverse(root);
        System.out.println();

        System.out.println("中序遍历:");
        inOrderTraverse(root);
        System.out.println();

        System.out.println("后序遍历:");
        postOrderTraverse(root);
    }

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java代码实现构造一颗三叉链表表示的二叉树,以及在二叉树中查找一个节点的示例代码: ```java class TreeNode { int val; TreeNode left; TreeNode right; TreeNode parent; public TreeNode(int val) { this.val = val; this.left = null; this.right = null; this.parent = null; } } class BinaryTree { private TreeNode root; public BinaryTree() { this.root = null; } public void insert(int val) { TreeNode newNode = new TreeNode(val); if (root == null) { root = newNode; } else { TreeNode current = root; TreeNode parent; while (true) { parent = current; if (val < current.val) { current = current.left; if (current == null) { parent.left = newNode; newNode.parent = parent; return; } } else { current = current.right; if (current == null) { parent.right = newNode; newNode.parent = parent; return; } } } } } public TreeNode search(int val) { TreeNode current = root; while (current != null && current.val != val) { if (val < current.val) { current = current.left; } else { current = current.right; } } return current; } } ``` 在这段代码中,我们定义了一个`TreeNode`类,表示二叉树中的一个节点。节点包含了一个`val`属性,表示节点的值,以及`left`、`right`和`parent`属性,分别表示节点的左子节点、右子节点和父节点。我们还定义了一个`BinaryTree`类,表示三叉链表表示的二叉树。该类包含了一个`root`属性,表示二叉树的根节点。 在`BinaryTree`类中,我们实现了一个`insert`方法,用于向二叉树中插入一个节点。该方法的实现普通二叉树插入方法类似,只不过我们需要在插入节点时同时设置节点的父节点。我们还实现了一个`search`方法,用于在二叉树中查找一个节点。该方法的实现也与普通二叉树查找方法类似。 以下是使用示例代码: ```java public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.insert(5); tree.insert(3); tree.insert(7); tree.insert(1); tree.insert(9); TreeNode node = tree.search(7); if (node != null) { System.out.println("Found node: " + node.val); } else { System.out.println("Node not found."); } } ``` 在这个示例中,我们创建了一颗二叉树,并向其中插入了一些节点。然后,我们调用`search`方法查找值为7的节点。如果找到了该节点,则输出节点的值;否则输出`Node not found.`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值