/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Tree;
/**
*
* @author think
*/
public class BinaryTree
{
public static void main(String[] args) {
BTree bt = new BTree();
bt.add(6);
bt.add(4);
bt.add(7);
bt.add(9);
bt.print();
}
}
class Node
{
int value;
Node left;
Node right;
Node(int value)
{
this.value = value;
left = null;
right = null;
}
}
class BTree
{
private Node root;
public BTree()
{
this.root = null;
}
public Node getRoot()
{
return root;
}
public void add(int value)
{
addRecursion(this.root, value);
}
private void addRecursion(Node current, int value)
{
if(current == null)
{
current = new Node(value);
System.out.println("add: " + current.value);
}
else
{
if(value < current.value)
{
addRecursion(current.left, value);
}
else if(value > current.value)
{
addRecursion(current.right, value);
}
}
return;
}
public void print()
{
PreoderTravesal(this.root);
}
// preoder recursiion
private void PreoderTravesal(Node root)
{
while(root != null)
{
System.out.println("node: " + root.value);
PreoderTravesal(root.left);
PreoderTravesal(root.right);
}
}
}
sao mình không duyt được dcaya nhỉ, lỗi tàn hình.
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?