forked from sublime1809/HuffmanTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.java
73 lines (60 loc) · 1.31 KB
/
BinaryTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.*;
import java.util.Scanner;
public class BinaryTree<E> {
protected Node<E> root;
protected int age;
public BinaryTree(){
root = null;
}
protected BinaryTree(Node<E> root){
this.root = root;
}
public BinaryTree(E data, BinaryTree<E> leftTree, BinaryTree<E> rightTree, int age){
this.age = age;
root = new Node<E>(data);
if(leftTree != null)
root.left = leftTree.root;
else
root.left = null;
if(rightTree != null)
root.right = rightTree.root;
else
root.right = null;
}
public BinaryTree<E> getLeftSubtree(){
if(root != null && root.left != null)
return new BinaryTree<E>(root.left);
else
return null;
}
public BinaryTree<E> getRightSubtree(){
if(root != null && root.right != null)
return new BinaryTree<E>(root.right);
else
return null;
}
public E getData(){
if(root != null)
return root.data;
else
return null;
}
public boolean isLeaf(){
return (root.left == null && root.right == null);
}
// Node
/** Node class */
protected class Node<E>{
E data;
Node<E> left = null;
Node<E> right = null;
public Node(E data, Node<E> L, Node<E> R){
this.data = data;
left = L;
right = R;
}
public Node(E data){
this.data = data;
}
}
}