1. Write a code implementing a binary tree. 2. Write a function that finds if a number is in a binary tree. 3. Given an array, shuffle it without using a random function.
Anoniem
import java.util.LinkedList; import java.util.Queue; public class BST { Node root; private Node addRecursive(Node current, int value) { if (current == null) { return new Node(value); } if (current.value value) { current.left = addRecursive(current.left, value); } return current; } public void add(int value) { root = addRecursive(root, value); } public boolean containsNode(int value) { Node current = root; while (current != null) { if (current.value value) { current = current.left; } else { return true; } } return false; } private Node deleteRecursive(Node current, int value) { if (current == null) { return null; } if (current.value == value) { if (current.left == null && current.right == null) { return null; } if (current.right == null) { return current.left; } if (current.left == null) { return current.right; } int smallestValue = findSmallestValue(current.right); current.value = smallestValue; current.right = deleteRecursive(current.right, smallestValue); return current; } if (value queue = new LinkedList(); queue.add(this.root); while (!queue.isEmpty()) { Node temp = queue.poll(); System.out.print(" " + temp.value); if (temp.left != null) { queue.add(temp.left); } if (temp.right != null) { queue.add(temp.right); } } } class Node { int value; Node right; Node left; public Node(int value) { this.value = value; right = null; left = null; } } }