Java examples for Data Structure:Tree
A simple implementation of a binary search tree (unbalanced).
import java.util.ArrayDeque; public class BinaryTree { TreeNode root;/*from ww w . j a va 2s .c o m*/ public void insert(int value) { if (root == null) { root = new TreeNode(); root.data = value; } else { insertNode(value, root); } } private void insertNode(int value, TreeNode current) { if (value < current.data) { if (current.left == null) { current.left = new TreeNode(); current.left.data = value; } else { insertNode(value, current.left); } } else { if (current.right == null) { current.right = new TreeNode(); current.right.data = value; } else { insertNode(value, current.right); } } } public boolean contains(int value) { if (findNode(value) != null) { return true; } else { return false; } } private boolean containsRecursion(int value, TreeNode current) { if (current == null) { return false; } else if (current.data == value) { return true; } else { if (value > current.data) { return containsRecursion(value, current.right); } else { return containsRecursion(value, current.left); } } } public TreeNode findNode(int value) { return findNodeRecursion(value, root); } private TreeNode findNodeRecursion(int value, TreeNode current) { if (current == null) { return null; } else if (current.data == value) { return current; } else { if (value > current.data) { return findNodeRecursion(value, current.right); } else { return findNodeRecursion(value, current.left); } } } public TreeNode findParent(int value) { return findParentRecursion(value, root); } private TreeNode findParentRecursion(int value, TreeNode current) { if (root.data == value) { return null; } if (value < current.data) { if (current.left == null) { return null; } else if (current.left.data == value) { return current; } else { return findParentRecursion(value, current.left); } } else { if (current.right == null) { return null; } else if (current.right.data == value) { return current; } else { return findParentRecursion(value, current.right); } } } public boolean remove(int value) { TreeNode nodeToRemove = findNode(value); if (nodeToRemove == null) { return false; } TreeNode parent = findParent(value); if (root.left == null && root.right == null) { root = null; } else if (nodeToRemove.left == null && nodeToRemove.right == null) { if (value < parent.data) { parent.left = null; } else { parent.right = null; } } else if (nodeToRemove.left == null) { if (value < parent.data) { parent.left = nodeToRemove.right; } else { parent.right = nodeToRemove.right; } } else if (nodeToRemove.right == null) { if (value < parent.data) { parent.left = nodeToRemove.left; } else { parent.right = nodeToRemove.left; } } else { TreeNode largestValue = nodeToRemove.left; while (largestValue.right != null) { largestValue = largestValue.right; } TreeNode parentOfLargest = findParent(largestValue.data); if (parentOfLargest.left == largestValue) { if (largestValue.left == null) { parentOfLargest.left = null; } else { parentOfLargest.left = largestValue.left; } } else { parentOfLargest.right = null; } nodeToRemove.data = largestValue.data; } return true; } public TreeNode findMin() { if (root == null) { return null; } return findMinRecursion(root); } private TreeNode findMinRecursion(TreeNode current) { if (current.left == null) { return current; } else { return findMinRecursion(current.left); } } public TreeNode findMax() { if (root == null) { return null; } return findMaxRecursion(root); } private TreeNode findMaxRecursion(TreeNode current) { if (current.right == null) { return current; } else { return findMaxRecursion(current.right); } } public void traversePreorder() { traversePreorderHelper(root); } private void traversePreorderHelper(TreeNode current) { if (current != null) { System.out.println(current.data); traversePreorderHelper(current.left); traversePreorderHelper(current.right); } } public void traversePostorder() { traversePostorderHelper(root); } private void traversePostorderHelper(TreeNode current) { if (current != null) { traversePostorderHelper(current.left); traversePostorderHelper(current.right); System.out.println(current.data); } } public void traverseInorder() { traverseInorderHelper(root); } private void traverseInorderHelper(TreeNode current) { if (current != null) { traverseInorderHelper(current.left); System.out.println(current.data); traverseInorderHelper(current.right); } } public void traverseBreadth() { traverseBreadthHelper(root); } private void traverseBreadthHelper(TreeNode current) { ArrayDeque<TreeNode> deq = new ArrayDeque<TreeNode>(); while (current != null) { System.out.println(current.data); if (current.left != null) { deq.add(current.left); } if (current.right != null) { deq.add(current.right); } if (!deq.isEmpty()) { current = deq.remove(); } else { current = null; } } } public static void main(String[] args) { BinaryTree bt = new BinaryTree(); bt.insert(42); bt.insert(13); bt.insert(666); bt.insert(10); bt.insert(11); bt.insert(7); bt.insert(9); bt.insert(4); System.out.println("1: " + bt.contains(1)); System.out.println("10: " + bt.contains(10)); System.out.println("42: " + bt.contains(42)); System.out.println("13: " + bt.contains(13)); System.out.println("11: " + bt.contains(11)); System.out.println("7: " + bt.contains(7)); System.out.println("9: " + bt.contains(9)); System.out.println("4: " + bt.contains(4)); System.out.println("666: " + bt.contains(666)); System.out.println("123456: " + bt.contains(123456)); System.out.println(); System.out.println(bt.findParent(42)); System.out.println(bt.findParent(13).data); System.out.println(bt.findParent(10).data); System.out.println(bt.findParent(123456)); System.out.println(); System.out.println(bt.findNode(1)); System.out.println(bt.findNode(42)); System.out.println(bt.findNode(666)); System.out.println(); System.out.println("Preorder Traverse:"); bt.traversePreorder(); System.out.println(); System.out.println("Postorder Traverse:"); bt.traversePostorder(); System.out.println(); System.out.println("Inorder Traverse:"); bt.traverseInorder(); System.out.println(); System.out.println("Breadth First Traverse:"); bt.traverseBreadth(); System.out.println(); System.out.println("Min: " + bt.findMin().data); System.out.println("Max: " + bt.findMax().data); System.out.println(); } } class TreeNode { int data; TreeNode left; TreeNode right; }