Created
March 8, 2023 01:31
-
-
Save mingyang91/26422bd3992116d54d2dc260d5cb0c11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lsbf.fm; | |
import java.util.Optional; | |
public class Tree { | |
public static void main(String[] args) { | |
Node<Integer> root = new Node<>(5); | |
root = new Node<>(5, Optional.of(new Node<>(1)), Optional.of(new Node<>(10))); | |
System.out.println(root.getLeft().get().getValue()); | |
System.out.println(root.getRight().get().getValue()); | |
} | |
} | |
class Node<T> { | |
private T value; | |
private Optional<Node<T>> left; | |
private Optional<Node<T>> right; | |
public Optional<Node<T>> getLeft() { | |
return left; | |
} | |
public Optional<Node<T>> getRight() { | |
return right; | |
} | |
public T getValue() { | |
return value; | |
} | |
public Node(T value) { | |
this.value = value; | |
this.left = Optional.empty(); | |
this.right = Optional.empty(); | |
} | |
public Node(T value, Optional<Node<T>> left, Optional<Node<T>> right) { | |
this.value = value; | |
this.left = left; | |
this.right = right; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment