二叉树

作者: 请叫我平爷 | 来源:发表于2022-03-21 14:07 被阅读0次

    Node节点

    public class Node {
        int data;   //节点数据
        Node left; //左子节点
        Node right; //右子节点
        public Node(int data){
            this.data = data;
        }
    }
    

    实现
    BinaryTree

    package com.example.demo.day07.binary;
    
    import com.example.demo.day07.binary.Node;
    import com.example.demo.day07.binary.Tree;
    
    public class BinaryTreeNode implements Tree {
        
        private Node root;
    
        @Override
        public Node find(int key) {
            Node current = root;
            while (current!=null){
                if (current.data>key){
                    current = current.left;
                }
                else if (current.data<key){
                    current=current.right;
                }
                else {
                    return current;
                }
            }
            return null;
        }
    
        @Override
        public boolean insert(int data) {
            Node node = new Node(data);
            Node current = root;
            while (current!=null){
                if (current.data>data){
                    current = current.left;
                }
                else if (current.data<data){
                    current = current.right;
                }
                else {
                    current = current;
                }
            }
            current = node;
            return true;
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:二叉树

          本文链接:https://www.haomeiwen.com/subject/psbzdrtx.html