skip to main
|
skip to sidebar
2007年8月29日星期三
binary java source code
class Node {
int val;
Node left=null;
Node right=null;
Node (int val) {
this.val=val;
}
}
class Binary {
Node root;
private static String twoBlanks = " ";
static String tab="";
void insert(Node n, int val) {
if (val < n.val ) {
if (n.left == null) // a leaf
n.left=new Node(val);
else
insert(n.left, val);
} else if (val > n.val ) {
if (n.right == null) // a leaf
n.right=new Node(val);
else
insert(n.right, val);
} else {}; // the value is repeated, do nothing
}
public void display(Node n) {
String s = "";
if (n != null) {
System.out.println(n.val + "\n");
tab += twoBlanks; // add two blanks
System.out.print(tab + " left: ");
display(n.left);
tab = tab.substring(2); // remove two blanks
tab += twoBlanks; // add two blanks
System.out.print(tab + " right: ");
display(n.right);
tab = tab.substring(2); // remove two blanks
}
}
}
import java.util.Random;
public class testBinary {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: Add your code here
Node n = new Node(5);
Binary b=new Binary();
//Random r = new Random();
int [] nums = {1,8,3,6,9};
for (int i = 0; i < nums.length; i++)
b.insert(n,nums[i]);
System.out.println("Start to print out ..." + "\n");
b.display(n);
}
}
没有评论:
发表评论
较早的博文
主页
订阅:
博文评论 (Atom)
Information Technology students java .net
博客归档
▼
2007
(4)
▼
八月
(4)
binary java source code
binary-java
vb.net store proc
羅馬數字
我的简介
Richard
查看我的完整个人资料
没有评论:
发表评论