节点结构如下:
class fptree{
int data;
int support;
fptree leftchild;
fptree rightchild;
fptree(int d,int s){
data=d;
support=s;
leftchild=null;
rightchild=null;
}下面是树中要插入的数据,每一个数字是一个节点,每一个整数数组的数据是整体(对于{6,4}来说,4是6的孩子,6是根节点的孩子),若数字重复(如{6,4}和{6,16,3}中6重复),则数字的support加1。
array.add(new int[]{6,4});
array.add(new int[]{2,3});
array.add(new int[]{6,16,3});
array.add(new int[]{3,6,13,1,3});
array.add(new int[]{3,13,4});
其中根节点是NULL,拜托了!!!!
class fptree{
int data;
int support;
fptree leftchild;
fptree rightchild;
fptree(int d,int s){
data=d;
support=s;
leftchild=null;
rightchild=null;
}下面是树中要插入的数据,每一个数字是一个节点,每一个整数数组的数据是整体(对于{6,4}来说,4是6的孩子,6是根节点的孩子),若数字重复(如{6,4}和{6,16,3}中6重复),则数字的support加1。
array.add(new int[]{6,4});
array.add(new int[]{2,3});
array.add(new int[]{6,16,3});
array.add(new int[]{3,6,13,1,3});
array.add(new int[]{3,13,4});
其中根节点是NULL,拜托了!!!!
public int key;
public Node left;
public Node right;
public Node(int key){
this.key = key;
}
public String toString(){
return "my key is "+key;
}
}
叶子节点
public Node root;
public Node findNode(Node node){
Node current = root ;
while (current.key != node.key) {
if (current.key > node.key) {
current = current.left;
} else {
current = current.right;
}
if (current == null) {
return null;
}
}
return current;
}
public void insertNode(Node node){
if (root == null) {
root = node;
} else {
Node current = root;
Node parent ;
while (true) {
parent = current;
if (current.key > node.key) {
current = current.left;
if (current == null) {
parent.left = node;
return ;
}
} else {
current = current.right;
if (current == null) {
parent.right = node;
return ;
}
}
}
}
} public void inOrder(Node current){
if (current != null) {
inOrder(current.left);
System.out.println(current);
inOrder(current.right);
}
}
public void preOrder(Node current){
if (current != null) {
System.out.println(current);
preOrder(current.left);
preOrder(current.right);
}
}
public void backOrder(Node current){
if (current != null) {
backOrder(current.left);
backOrder(current.right);
System.out.println(current);
}
} public Node minNode(){
Node current = root;
while (current.left != null) {
current = current.left;
}
return current;
}
public Node maxNode(){
Node current = root;
while (current.right != null) {
current = current.right;
}
return current;
} public boolean deleteNode(Node node){
Node current = root;
Node parent = root ;
boolean isLeft = true;
while (current.key != node.key) {
parent = current;
if (current.key > node.key) {
isLeft = true;
current = current.left;
} else {
isLeft = false;
current = current.right;
}
if (current == null) {
return false;
}
}
if (current.left == null && current.right == null) { //叶子
if (current.key == root.key) {
root = null;
} else if (isLeft) {
parent.left = null;
} else {
parent.right = null;
}
} else if (current.right == null) { //只有左节点
if (current.key == root.key) {
root = current.left;
} else if (isLeft) {
parent.left = current.left;
} else {
parent.right = current.left;
}
} else if (current.left == null) { //只有左节点
if (current.key == root.key) {
root = current.right;
} else if (isLeft) {
parent.left = current.right;
} else {
parent.right = current.right;
}
} //删除左右节点都有的节点
return true;
}
}
树,删除还没有完成。不好意思。
Tree tree = new Tree();
tree.insertNode(new Node(12));
tree.insertNode(new Node(6));
tree.insertNode(new Node(24));
tree.insertNode(new Node(2));
tree.insertNode(new Node(7));
tree.insertNode(new Node(18));
tree.insertNode(new Node(28));
tree.insertNode(new Node(30));
tree.inOrder(tree.root);
Node find = new Node(11);
System.out.println(tree.findNode(find));
Node find2 = new Node(18);
System.out.println(tree.findNode(find2));
Node mix = tree.minNode();
System.out.println(mix);
Node max = tree.maxNode();
System.out.println(max);
}}
应用
结构如:
null
6:2 2:2
4:1 16:1 3:2
3:1 5:1
没画线,就这么样的树,节点包括数据和数据的支持度