/*Linked List Lab warmup shell for Senior research course. */ // This example is from // http://www.glenmccl.com/tip_005.htm import javax.swing.JOptionPane; import java.util.Scanner; import java.io.*; public class BinaryTree { public static void printInorder(BinaryNode tree) { if (tree != null) { printInorder(tree.left); System.out.printf("%3d", tree.data); printInorder(tree.right); } } public static BinaryNode insertTree(BinaryNode head, BinaryNode node) { if (head == null) { head = node; } else { BinaryNode p = head; BinaryNode back=p; boolean toRight = false; boolean toLeft = false; while ( p != null) { back = p; if (node.data < p.data) { p = p.left; if (p==null) toLeft = true; } else { p = p.right; if (p==null) toRight = true; } } if (toLeft) back.left = node; else back.right = node; } return head; } public static void main(String[] args) { BinaryNode head = null; int[] vals = {5, 2, 8, 1, 3, 6, 15, 13, 4}; String[] words = {"mary","had","a","little","lamb"}; // int[] vals = {5, 2, 1, 3, 4}; // add some entries to list for (int i = 0; i < vals.length; i++) { // BinaryNode temp = new BinaryNode(vals[i]); BinaryNode temp = new BinaryNode((int)(Math.random()*20)); // System.out.print(" data=" + temp.data); head=insertTree(head, temp); // System.out.println("Head data=" + head.data); } System.out.println(); printInorder(head); System.out.println(); } }