Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class BST {
- private static Scanner sc;
- static class Node{
- private Node left;
- private Node right;
- private Node parent;
- private Integer valor;
- public Node() {
- left = null;
- right = null;
- parent = null;
- valor = null;
- }
- public Node(Integer v) {
- left = null;
- right = null;
- parent = null;
- valor = v;
- }
- public Node(Integer v,Node l, Node r, Node p) {
- left = l;
- right = r;
- parent = p;
- valor = v;
- }
- public Node getLeft() {return this.left;}
- public Node getRight() {return this.right;}
- public Node getParent() {return this.parent;}
- public Integer getValor() {return this.valor; }
- public void setLeft(Node l) { this.left = l; }
- public void setRight(Node r) { this.right = r; }
- public void setParent(Node p) { this.parent = p; }
- public void setValor(Integer v) {this.valor = v;}
- public boolean empty() { return this.valor == null;}
- public boolean isLeaf() {return this.left.getValor() == null && this.right.getValor() == null;}
- }
- static class Tree{
- private Node root;
- private String ans;
- public Tree() {
- root = new Node();
- }
- public void insert(Integer element) {
- if(element != null)
- insert(root, element);
- }
- private void insert(Node no, int element) {
- if(no.empty()) {
- no.setValor(element);
- no.setLeft(new Node());
- no.setRight(new Node());
- no.getLeft().setParent(no);
- no.getRight().setParent(no);
- }
- else {
- if(element > no.getValor()) insert(no.getRight(), element);
- else if(element < no.getValor()) insert(no.getLeft(), element);
- }
- }
- public int height() {
- return height(root)-1;
- }
- public int height(Node no) {
- if(no.empty()) return 0;
- return Math.max(this.height(no.getLeft()), this.height(no.getRight())) + 1;
- }
- public Integer search(Integer element) {
- return search(root, element).getValor();
- }
- public Node search(Node no, Integer element) {
- if(no != null && !no.empty()) {
- //System.out.print(no.getValor() + ", ");
- this.ans += no.getValor() + ", ";
- if(element > no.getValor() && no.getRight() != null) return search(no.getRight(), element);
- else if(element < no.getValor() && no.getLeft() != null) return search(no.getLeft(), element);
- return no;
- }
- Node painho = no.getParent().getParent();
- Node bambam = no.getParent();
- while(painho != null && Math.abs(painho.getValor()-element) < Math.abs(bambam.getValor()-element)) {
- bambam = painho;
- painho = painho.getParent();
- }
- return bambam;
- }
- public Node maximum() {
- return maximum(root);
- }
- private Node maximum(Node no) {
- ans += no.getValor() + ", ";
- if (no.getRight().empty()) return no;
- return maximum(no.getRight());
- }
- public Node minimum() {
- return minimum(this.root);
- }
- private Node minimum(Node no) {
- //ans += no.getValor() + ", ";
- if (no.getLeft().empty()) return no;
- return minimum(no.getLeft());
- }
- private Node alternativeParent(Node no, int element, boolean tipo){
- Node dad = no.getParent();
- //if(dad != null) ans += dad.getValor() + ", ";
- if((dad == null || dad.getValor().compareTo(element) < 0) && !tipo) return dad;
- if((dad == null || dad.getValor().compareTo(element) > 0) && tipo) return dad;
- return alternativeParent(dad, element, tipo);
- }
- // public Node sucessor(int element) {
- // Node no = search(element);
- // if(no != null && !no.empty()) {
- // //ans += no.getValor() + ", ";
- // if (!no.getRight().empty()) {
- // return minimum(no.getRight());
- // }else {
- // return alternativeParent(no, no.getValor(), true);
- // }
- // }
- // return null;
- // }
- //
- // public String predecessor(int element) {
- // Node no = search(element);
- // ans = "";
- // if(no != null && !no.empty()) {
- // ans += no.getValor() + ", ";
- // if (!no.getLeft().empty()) {
- // maximum(no.getLeft());
- // }else {
- // alternativeParent(no, element, false);
- // }
- // }
- // return "[" + ans.substring(0, ans.length()-2) + "]";
- // }
- //
- // public void remove(int element) {
- // Node no = search(element);
- // remove(no);
- // }
- //
- // private void remove(Node no) {
- // if(no != null && !no.empty()) {
- // if(no.isLeaf()) {
- // replace(no, new Node());
- // }else if(no.getRight().empty()) {
- // replace(no, no.getLeft());
- // }else if(no.getLeft().empty()) {
- // replace(no, no.getRight());
- // }else {
- // Node sucess = sucessor(no.getValor());
- // no.setValor(sucess.getValor());
- // remove(sucess);
- // }
- // }
- // }
- //
- // private void replace(Node no, Node set) {
- // Node dad = no.getParent();
- // if(dad == null) {
- // this.root = set;
- // set.setParent(null);
- // }else {
- // if(dad.getValor() <= no.getValor()) {
- // dad.setRight(set);
- // dad.getRight().setParent(dad);
- // }else {
- // dad.setLeft(set);
- // dad.getLeft().setParent(dad);
- // }
- // }
- // }
- public int[] preOrder() {
- int[] arr = new int[this.size()];
- if(!root.empty()) preOrder(this.root, arr, 0);
- return arr;
- }
- private int preOrder(Node no, int[] arr, int idx) {
- if(!no.empty()) {
- arr[idx++] = no.getValor();
- idx = preOrder(no.getLeft(), arr, idx);
- idx = preOrder(no.getRight(), arr, idx);
- }
- return idx;
- }
- public int size() {
- return size(root);
- }
- private int size(Node no) {
- int result = 0;
- if (!no.empty()) {
- result = 1 + size(no.getLeft()) + size( no.getRight());
- }
- return result;
- }
- public int leaf() {
- return leaf(root);
- }
- private int leaf(Node no) {
- int numFolhas = 0;
- if (no.getValor() == null)
- return 0;
- if (no.isLeaf()) {
- return 1;
- } else {
- numFolhas = leaf(no.getLeft()) + leaf(no.getRight());
- }
- return numFolhas;
- }
- public String bfs() {
- ans = "";
- Queue<Node> q = new LinkedList<>();
- q.add(root);
- while(!q.isEmpty()) {
- Node top = q.poll();
- ans += top.getValor() + " ";
- if(top.getRight() != null && !top.getRight().empty()) q.add(top.getRight());
- if(top.getLeft() != null && !top.getLeft().empty()) q.add(top.getLeft());
- }
- return ans.substring(0, ans.length()-1);
- }
- public int proximo(Integer element) {
- return proximo(element, root).getValor();
- }
- public Node comp(Node l, Node r, int element) {
- if(l != null && !l.empty() && r != null && !r.empty()) {
- if(Math.abs(l.getValor()-element) < Math.abs(r.getValor()-element)) {
- return l;
- }else return r;
- }else if(l != null && !l.empty()) return l;
- else if(r != null && !r.empty()) return r;
- return null;
- }
- public Node proximo(Integer element, Node no) {
- if(no.isLeaf() || no.getValor() == element) return no;
- Node bestParent = proximo(element,comp(no.getLeft(), no.getRight(), element));
- int dif = Math.abs(no.getValor()-element);
- if(dif > Math.abs(bestParent.getValor()-element)) return bestParent;
- else return no;
- }
- }
- public static void main(String[] args) {
- sc = new Scanner(System.in);
- String[] vals = sc.nextLine().split(" ");
- int[] arr = new int[vals.length];
- Tree DST = new Tree();
- for(int i = 0; i < vals.length; i++) {
- arr[i] = Integer.parseInt(vals[i]);
- DST.insert(arr[i]);
- }
- int elem = sc.nextInt();
- System.out.println(Arrays.toString(DST.preOrder()));
- System.out.println(DST.proximo(elem));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment