创建叶子node节点
class Node{
int no;
Node next;
public Node(int no){
this.no=no;
}
}
public class Lbiao {
Node head;
int size;
public void init(){
head=null;
size=0;
}
public boolean isEmpty(){
return head==null; //return size==0;
}
public void insertT(Node n1){
n1.next=head;
head=n1;
size++;
}
public void insertW(Node n1){
if (isEmpty()){insertT(n1);return;}
Node p=head;
while(p.next!=null)
p=p.next;
p.next=n1;
size++;
}
public Node get(int i){
if ((i<1)||(i>size)) {System.out.println("i超过了范围!");return null;}
int j=1;Node p=head;
while(j<i){
p=p.next;
j++;
}
return p;
}
public void delete(int i){
if (!isEmpty()){
if ((i<1)||(i>size)){return ;}
if (i==1){head=head.next;size--;return;}
Node p=head;
int j=1;
while(j<i-1){
p=p.next;
j++;
}
p.next=p.next.next;
size--;
}
else{System.out.println("没有结点用来删除!");}
}
public void printLink(){
Node p;
p=head;
while(p!=null){
System.out.println(p.no);
p=p.next;
}
}
public void insert(int i,Node n1){
if ((i<1)||(i>size+1)){System.out.println("超过范围!"); return;}
if (i==1){
n1.next=head;
head=n1;
size++;
return ;
}
Node p=head;
int j=1;
while(j<i-1){
p=p.next;
j++;
}
n1.next=p.next;
p.next=n1;
size++;
}
public static void main(String[] args) {
Lbiao link=new Lbiao();
link.init();
link.insertT(new Node(10));
link.insertT(new Node(20));
link.insertT(new Node(30));
link.insertT(new Node(40));
link.insertT(new Node(50));
link.printLink();
link.insert(6,new Node(23));
link.printLink();
}
}
建立二叉树
class Tree{
int data;
Tree lchild;
Tree rchild;
}
public class Shu {
Tree []t;
public void createT(int m){
t=new Tree[m+1];
for(int i=1;i<=m;i++)
t[i]=new Tree();
}
public void Zhprintbt(int n){
if (t[n].lchild!=null){
Zhprintbt(2*n);
if (t[n].data!=0)
System.out.print("t["+n+"].data="+t[n].data+"\t");
if (t[n].rchild!=null)
Zhprintbt(2*n+1);
}
else{
if (t[n].data!=0)
System.out.print("t["+n+"].data="+t[n].data+"\t");
if (t[n].rchild!=null)
Zhprintbt(2*n+1);
}
}
public void Qprintbt(int n){
if (t[n].data!=0)
System.out.print("t["+n+"].data="+t[n].data+"\t");
if (t[n].lchild!=null)
Qprintbt(2*n);
if (t[n].rchild!=null)
Qprintbt(2*n+1);
}
public void Hprintbt(int n){
if (t[n].lchild!=null)
Hprintbt(2*n);
if (t[n].rchild!=null)
Hprintbt(2*n+1);
if (t[n].data!=0)
System.out.print("t["+n+"].data="+t[n].data+"\t");
}
public static void main(String[] args) {
Shu s=new Shu();
s.createT(16);
for(int i=1;i<16/2;i++){
s.t[i].lchild=s.t[i*2];
s.t[i].rchild=s.t[i*2+1];
}
s.t[1].data=11;
s.t[2].data=22;
s.t[3].data=33;
s.t[5].data=44;
s.t[6].data=55;
s.t[7].data=66;
s.t[11].data=77;
s.t[14].data=88;
s.Zhprintbt(2);
System.out.println();
s.Qprintbt(1);
System.out.println();
s.Hprintbt(1);
}
}
三元组:只考虑稀疏矩阵中非0的元素,并且存储到一个类(三元组)的数组中。
0 0 0 0 0 1 2 0 0
23 0 0 0 0 0 0 0 0
0 98 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 3 0 0
(0,0,0) (0,0,0) (0,0,0) (0,0,0) (0,0,0) (0,0,0) (0,0,0)
(5,9,6) (0,6,1) (0,7,2) (1,0,23) (2,1,98) (4,0,3) (4,6,3)
import java.util.Random;
class Sanyuanzu{
int row;
int col;
int value;
public Sanyuanzu(int row,int col,int value){
this.row=row;
this.col=col;
this.value=value;
}
}
public class Xsjz {
Random r=new Random();
int [][]jz;
Sanyuanzu []a;
public void createjz(int m,int n){ //产生二维数组
jz=new int[m][n]; //给二维数组jz分配空间,该数组的行是m,列是n
for(int i=0;i<jz.length;i++)
for(int j=0;j<jz[0].length;j++)
jz[i][j]=r.nextInt(100)<=85?0:r.nextInt(99)+1;
}
public void toSan(){
int c=0,k=0; //表示非0元素的个数
for(int i=0;i<jz.length;i++)
for(int j=0;j<jz[0].length;j++)
if (jz[i][j]!=0)
c++;
a=new Sanyuanzu[c+1]; //给数组分配空间
for(int i=0;i<a.length;i++)
a[i]=new Sanyuanzu(0,0,0); //实例化类为对象
a[0].row=jz.length;
a[0].col=jz[0].length;
a[0].value=c;
for(int i=0;i<jz.length;i++)
for(int j=0;j<jz[0].length;j++)
if (jz[i][j]!=0){
k++;
a[k].row=i;
a[k].col=j;
a[k].value=jz[i][j];
}
}
public void prinjz(){
for(int i=0;i<jz.length;i++){
for(int j=0;j<jz[0].length;j++)
System.out.print(jz[i][j]+" ");
System.out.println();
}
}
public void printsan(){
for(int i=0;i<a.length;i++)
System.out.print("("+a[i].row+","+a[i].col+","+a[i].value+") ");
}
public static void main(String[] args) {
Xsjz s=new Xsjz();
s.createjz(5,8);
s.prinjz();
s.toSan();
s.printsan();
}
}