资讯专栏INFORMATION COLUMN

【Java】二叉树的实现

jerryloveemily / 3124人阅读

摘要:泛型简易实现根节点二叉树的节点节点的值左右孩子节点先序遍历方法用于测试二叉查找树插入二叉树的节点节点的值左右孩子节点根节点插入节点先序遍历方法用于测试先序遍历迭代器

泛型简易实现
public class Tree {
    private Node root;   //根节点

    public Tree(Node root) {
        this.root = root;
    }

    /*二叉树的节点*/
    private static class Node {
        T element;  //节点的值
        Node lchild, rchild; //左右孩子节点

        public Node(T element, Node lchild, Node rchild) {
            this.element = element;
            this.lchild = lchild;
            this.rchild = rchild;
        }   
    }

    /*先序遍历*/
    public void preorder(Node root   ) {
        if(root != null) {
            System.out.println(root.element);
            preorder(root.lchild);
            preorder(root.rchild);
        }
    }

    /*main方法用于测试*/
    public static void main(String[] args) {
        Node lchild = new Node("B", null, null);
        Node rchild = new Node("C", null, null);
        Node root = new Node("A", lchild, rchild);

        Tree tree = new Tree(root);
        tree.preorder(tree.root);

    }
}
二叉查找树 插入
public class Tree> {
    /*二叉树的节点*/
    private static class Node {
        T element;  //节点的值
        Node lchild, rchild; //左右孩子节点

        public Node(T element, Node lchild, Node rchild) {
            this.element = element;
            this.lchild = lchild;
            this.rchild = rchild;
        }   
    }

     Node root = null;   //根节点

    public Tree(Node root) {
        this.root = root;
    }   

    /*插入节点*/
    protected Node insert(Noderoot, NodenewNode) {
        if(root == null) {
            root = newNode;
        } else if (root.element.compareTo(root.element) < 0) {
            root.lchild = insert(root.lchild, newNode);
        } else {
            root.rchild = insert(root.rchild, newNode);
        }
        return root;
    }

    public void insert(T data) {
        Node newNode = new Node(data, null, null);
        root = insert(root, newNode);
    }

    /*先序遍历*/
    public void preorder(Node root   ) {
        if(root != null) {
            System.out.println(root.element);
            preorder(root.lchild);
            preorder(root.rchild);
        }
    }

    /*main方法用于测试*/
    public static void main(String[] args) {
        Tree tree = new Tree(null);
        tree.insert("C");
        tree.insert("B");
        tree.insert("A");
        tree.preorder(tree.root);
    }
}
先序遍历迭代器
    /** Returns a preorder iterator for this tree. */ 
     public Iterator preorderIterator() { 
     return new PreorderIterator(); 
     } 

    /*** inner class for a preorder iterator ***/
    private class PreorderIterator implements Iterator {
        private Node nextNode;

        private PreorderIterator() {
            // The traversal starts with the root node.
            nextNode = root;
        }

        public boolean hasNext() {
            return (nextNode != null);
        }

        public T next() {
            if (nextNode == null)
                throw new NoSuchElementException();

            // Store a copy of the key to be returned.
            T element = nextNode.element;

            // Advance nextNode.
            if (nextNode.lchild != null)
                nextNode = nextNode.lchild;
            else if (nextNode.rchild != null)
                nextNode = nextNode.rchild;
            else {
                // We"ve just visited a leaf node.
                // Go back up the tree until we find a node
                // with a right child that we haven"t seen yet.
                Node parent = nextNode.parent;
                Node child = nextNode;
                while (parent != null
                        && (parent.rchild == child || parent.rchild == null)) {
                    child = parent;
                    parent = parent.parent;
                }

                if (parent == null)
                    nextNode = null; // the traversal is complete
                else
                    nextNode = parent.rchild;
            }

            return element;
        }

        @Override
        public void remove() {
            // TODO Auto-generated method stub  
        }
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64129.html

相关文章

  • Java数据结构与算法——叉树及操作(包括叉树遍历)

    摘要:本篇主要介绍二叉树的概念二叉树的表示二叉树的操作三种遍历方式实现求二叉树的子树求节点的父节点二叉树高度,可能是考试中的,也可能是面试中的。通常二叉树的遍历根据根节点的遍历次序分为先根遍历中根遍历后根遍历。 声明:码字不易,转载请注明出处,欢迎文章下方讨论交流。 前言:Java数据结构与算法专题会不定时更新,欢迎各位读者监督。本篇主要介绍二叉树的概念、二叉树的表示、二叉树的操作(三种遍历...

    muddyway 评论0 收藏0
  • 一文掌握关于Java数据结构所有知识点(欢迎一起完善)

    摘要:是栈,它继承于。满二叉树除了叶结点外每一个结点都有左右子叶且叶子结点都处在最底层的二叉树。没有键值相等的节点。这是数据库选用树的最主要原因。 在我们学习Java的时候,很多人会面临我不知道继续学什么或者面试会问什么的尴尬情况(我本人之前就很迷茫)。所以,我决定通过这个开源平台来帮助一些有需要的人,通过下面的内容,你会掌握系统的Java学习以及面试的相关知识。本来是想通过Gitbook的...

    keithxiaoy 评论0 收藏0
  • 树和树的算法

    摘要:树和树的算法一树树的概念树英语是一种抽象数据类型或是实作这种抽象数据类型的数据结构,用来模拟具有树状结构性质的数据集合。一种时间复杂度额外空间复杂度的二叉树的遍历方式,为二叉树的节点个数。 树和树的算法 一、树 1.1 树的概念 树(英语:tree)是一种抽象数据类型(ADT)或是实作这种抽象数据类型的数据结构,用来模拟具有树状结构性质的数据集合。它是由n(n>=1)个有限节点组成一个...

    RaoMeng 评论0 收藏0
  • 树和树的算法

    摘要:树和树的算法一树树的概念树英语是一种抽象数据类型或是实作这种抽象数据类型的数据结构,用来模拟具有树状结构性质的数据集合。一种时间复杂度额外空间复杂度的二叉树的遍历方式,为二叉树的节点个数。 树和树的算法 一、树 1.1 树的概念 树(英语:tree)是一种抽象数据类型(ADT)或是实作这种抽象数据类型的数据结构,用来模拟具有树状结构性质的数据集合。它是由n(n>=1)个有限节点组成一个...

    PiscesYE 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<