10

到现在为止,我一直在写一个 Node 类

class Node {
        private  value;
        private Node left;
        private Node right;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getLeft() {
            return left;
        }

        public void setLeft(Node left) {
            this.left = left;
        }

        public Node getRight() {
            return right;
        }

        public void setRight(Node right) {
            this.right = right;
        }
    } 

和二叉搜索树为

public class BinarySearchTree {
    private Node root;

    public BinarySearchTree(int value) {
        root = new Node(value);
    }

    public void insert(int value) {
      Node node = new Node(value);
        // insert logic goes here to search and insert
    }
}

现在我想支持 BinarySearchTree 插入任何类型的节点,比如字符串,人

我怎样才能使它通用以容纳任何类型?

4

10 回答 10

17

使用泛型:

class Node<T extends Comparable<T>> {
        private T value;
        ...
}

public class BinarySearchTree<T extends Comparable<T>> {
    private Node<T> root;

    public BinarySearchTree(T value) {
        root = new Node<T>(value);
    }

    public void insert(T value) {
      Node<T> node = new Node<T>(value);
        // insert logic goes here to search and insert
    }
}
于 2012-06-29T14:06:27.840 回答
4

只需将每个NodeBinarySearchTree类设为通用:

class Node<T extends Comparable<T>> {
    private T value;
    private Node<T> left;
    private Node<T> right;

    public Node(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public Node<T> getLeft() {
        return left;
    }

    public void setLeft(Node<T> left) {
        this.left = left;
    }

    public Node<T> getRight() {
        return right;
    }

    public void setRight(Node<T> right) {
        this.right = right;
    }
} 

和:

class BinarySearchTree<T extends Comparable<T>> {
    private Node<T> root;

    public BinarySearchTree(T value) {
        root = new Node<T>(value);
    }

    public void insert(T value) {
      Node<T> node = new Node<T>(value);
        // insert logic goes here to search and insert
    }
}

请注意Comparable稍后在树中强制执行节点排序时需要的扩展约束。感谢 zaske 的建议。

于 2012-06-29T14:08:26.257 回答
2
Please find the BST using Generics, U can find more information on below link 

https://www.cs.cmu.edu/~adamchik/15-121/lectures/Trees/code/BST.java

public class BinarySearchTree< T extends Comparable<T>> {
    Node root;
    class Node {
        T data;
        Node left;
        Node right;

        public Node(T data) {
            this.data = data;
        }
    }

    public boolean isEmpty() {
        return root == null;
    }

    public void insert(T value) {
        if(isEmpty())
            root = new Node(value);
        else
            insert(root, value);
    }

    private void insert(Node node, T value) {

        if(value.compareTo(node.data) < 0) {
            if(node.left == null)
                node.left = new Node(value);
            else
                insert(node.left, value);
        }
        else {
            if(node.right == null)
                node.right = new Node(value);
            else
                insert(node.right, value);
        }
    }

}
于 2016-09-17T17:53:45.090 回答
1

请不要您的代码无法编译。
您在这里有一些挑战 -
A. 将节点定义为通用 -

public class Node<T> {
   private T value;
   //... here goes the rest of your code
}

B. 您的搜索类也应该是通用的,并且签名应该是

public class BinarySearchTree <T extends Comparable<T>> {

   public BinarySearchTree(T value) {
      //Do your logic here
   }

   public void insert(T value)  {
        //Do your logic here
   }
}

这是必需的,以强制您仅提供实现 Comparable 的类型,以便您能够在树中执行搜索。

于 2012-06-29T14:12:30.043 回答
0

你有两个选择:

1)您可以进入泛型/模板。

2)让你的树接受一个类型Object而不是int让用户负责转换。

于 2012-06-29T14:07:42.680 回答
0

我在这里找到了一个实现并发 AVL 树系统的 SnapTreeMap 。

于 2016-05-13T17:18:07.993 回答
0

最近写了这个。希望对你有帮助

public class GenericBST<V extends Comparable<V>> {

private class Node {
    Node left, right;
    V value;
    public Node(V value) {
        this.value = value;
    }
}

private Node root;
private int size;

public int getSize() {
    return size;
}

public GenericBST() {
    this.root = null;
    this.size = 0;
}

public boolean isEmpty() {
    if (root == null) return true;
    else return false;
}

public void insert(V value) {
    size++;
    if (isEmpty()) root = new Node(value);
    else insert(root, value);
}

public boolean contains(V value) {
    return contains(root, value);
}

public void print() {
    print(root);
}

private boolean contains(Node node, V value) {
    if (value.compareTo(node.value) == 0) {
        return true;
    } else if (value.compareTo(node.value) < 0) {
        if (node.left == null) return false;
        else return contains(node.left, value);
    } else {
        if (node.right == null) return false;
        else return contains(node.right, value);
    }
}

private void print(Node node) {
    if (root == null) return;
    System.out.println(node.value);
    if (node.left != null) print(node.left);
    if (node.right != null) print(node.right);
}

private void insert(Node node, V value) {
    if(value.compareTo(node.value) <= 0) {
        if(node.left == null) node.left = new Node(value);
        else insert(node.left, value);
    } else {
        if(node.right == null) node.right = new Node(value);
        else insert(node.right, value);
    }
}

}

于 2022-01-30T15:47:23.863 回答
0
public class TNode<T extends Comparable<T>> {
    T data;
    public TNode<T> left;
    public TNode<T> right;

    public TNode(T data){
        this.data = data;
    }
}


import java.util.ArrayList;
import java.util.List;

public class BinaryTree<T extends Comparable<T>> {
private TNode root;

public TNode getRoot() {
    return this.root;
}

public void add(T data) {
    TNode<T> newNode = new TNode<T>(data);
    if (root == null) {
        root = newNode;
    } else {
        TNode<T> tempNode = root;
        TNode<T> prev = null;
        while (tempNode != null) {
            prev = tempNode;
            if (data.compareTo(tempNode.data) > 0) {
                tempNode = tempNode.right;
            } else {
                tempNode = tempNode.left;
            }
        }
        if (data.compareTo(prev.data) < 0) {
            prev.left = newNode;
        } else {
            prev.right = newNode;
        }

    }
}


public void traverseInOrder(TNode<T> root, List<T> storageList) {
    if (root != null) {
        traverseInOrder(root.left, storageList);
        storageList.add(root.data);
        traverseInOrder(root.right, storageList);
    }
}

public void traversePreOrder(TNode<T> root, List<T> storageList) {
    if (root != null) {
        storageList.add(root.data);
        traversePreOrder(root.left, storageList);
        traversePreOrder(root.right, storageList);
    }
}

public void traversePostOrder(TNode<T> root, List<T> storageList) {
    if (root != null) {
        traversePostOrder(root.left, storageList);
        traversePostOrder(root.right, storageList);
        storageList.add(root.data);
    }
}

public void printList(List<T> list) {
    for (T item : list) {
        System.out.println(item);
    }
}


public static void main(String args[]) {
    BinaryTree<Integer> bTree = new BinaryTree<>();
    bTree.add(50);
    bTree.add(30);
    bTree.add(60);
    bTree.add(25);
    bTree.add(40);
    bTree.add(35);
    bTree.add(70);
    bTree.add(65);

    System.out.println("#### Inorder Traversal ####");
    List<Integer> inOrderList = new ArrayList<>();
    bTree.traverseInOrder(bTree.getRoot(), inOrderList);
    bTree.printList(inOrderList);

    System.out.println("#### Pre Traversal ####");
    List<Integer> preOrderList = new ArrayList<>();
    bTree.traversePreOrder(bTree.getRoot(), preOrderList);
    bTree.printList(preOrderList);


    System.out.println("#### Post Traversal ####");
    List<Integer> postOrderList = new ArrayList<>();
    bTree.traversePostOrder(bTree.getRoot(), postOrderList);
    bTree.printList(postOrderList);


}
于 2018-05-27T04:04:49.623 回答
0

最近写了这个。希望对你有帮助

public class TreeMap<V extends Comparable<V>> {

private class Node {
    Node left, right;
    V value;
    public Node(V value) {
        this.value = value;
    }
}

private Node root;
private int size;

public int getSize() {
    return size;
}

public TreeMap() {
    this.root = null;
    this.size = 0;
}

public boolean isEmpty() {
    if (root == null) return true;
    else return false;
}

public void insert(V value) {
    size++;
    if (isEmpty()) root = new Node(value);
    else insert(root, value);
}

public boolean contains(V value) {
    return contains(root, value);
}

public void print() {
    print(root);
}

private boolean contains(Node node, V value) {
    if (value.compareTo(node.value) == 0) {
        return true;
    } else if (value.compareTo(node.value) < 0) {
        if (node.left == null) return false;
        else return contains(node.left, value);
    } else {
        if (node.right == null) return false;
        else return contains(node.right, value);
    }
}

private void print(Node node) {
    if (root == null) return;
    System.out.println(node.value);
    if (node.left != null) print(node.left);
    if (node.right != null) print(node.right);
}

private void insert(Node node, V value) {
    if(value.compareTo(node.value) <= 0) {
        if(node.left == null) node.left = new Node(value);
        else insert(node.left, value);
    } else {
        if(node.right == null) node.right = new Node(value);
        else insert(node.right, value);
    }
}

}

于 2022-01-30T15:20:13.627 回答
-1

关于第二个问题,您应该使用 Template :

http://www.oracle.com/technetwork/articles/javase/generics-136597.html

关于第一:

http://en.wikipedia.org/wiki/Binary_search_algorithm http://en.wikipedia.org/wiki/Tree_rotation(插入)

也许读起来更快:

http://www.roseindia.net/java/java-get-example/java-binary-tree-code.shtml

好好学习!

于 2012-06-29T14:07:45.577 回答