数据结构之链表和二叉树

代码示例:

  • 链表实现
//学生类
class Student{
	int id;
	String name;
	int age;
	public Student(int id,String name,int age) {
		this.id=id;
		this.name=name;
		this.age=age;
	}
	
	public boolean equals(Object obj) {
		if(this==obj) {
			return true;
		}
		if(obj instanceof Student) {
			Student temp=(Student)obj;
			if(temp.id!=id) return false;
			if(temp.age!=age) return false;
			if(!temp.name.equals(name)) return false;
			return true;
		}else {
			return false;
		}
	}
}
//链表类
class NodeManage{
	Node root;//根节点
	int currentIndex=0;
	public void add(Student stu) {
		if(null==root) {
			root=new Node(stu);
		}else {
			root.addNode(stu);
		}
	}
	
	public void delete(Student stu) {
		if(null!=root) {
			if(root.data.equals(stu)) {
				root=root.next;
			}else {
				root.delNode(stu);
			}
			
		}
	}
	
	public boolean update(Student oldStu,Student newStu) {
		if(null!=root) {
			if(root.data.equals(oldStu)) {
				root.data=newStu;
				return true;
			}else {
				return root.updateNode(oldStu, newStu);
			}
		}
		return false;
	}
	
	public boolean insert(int index,Student stu) {
		if(index<0) return false;
		if(null!=root) {
			if(index==0) {
				Node temp=new Node(stu);
				temp.next=root;
				root=temp;
				return true;
			}else {
				return root.insertNode(index, stu);
			}
		}
		return false;
	}
	
	public void printAll() {
		if(null!=root) {
			System.out.println("->(id:"+root.data.id
					+",name:"+root.data.name
					+",age:"+root.data.age
					+")");
			root.printNode();
			System.out.println();
		}
	}
	//节点类
	private class Node{
		Student data;//节点存储的数据
		Node next;//指向下一节点
		
		public Node(Student stu) {
			data=stu;
		}
		//节点添加
		public void addNode(Student stu) {
			if(null==this.next) {
				this.next=new Node(stu);
			}else {
				this.next.addNode(stu);
			}
		}
		
		//节点更新
		public boolean updateNode(Student oldStu,Student newStu) {
			if(null!=this.next) {
				if(this.next.data.equals(oldStu)) {
					this.next.data=newStu;
					return true;
				}else {
					return this.next.updateNode(oldStu, newStu);
				}
			}
			return false;
		}
		
		//节点删除
		public void delNode(Student stu) {
			if(this.next!=null) {
				if(this.next.data.equals(stu)) {
					this.next=this.next.next;
				}else {
					this.next.delNode(stu);
				}
			}
			
		}
		
		//节点插入(前插)
		public boolean insertNode(int index,Student stu) {
			currentIndex++;
			if(null!=this.next) {
				if(index==currentIndex) {
					Node temp=new Node(stu);
					temp.next=this.next;
					this.next=temp;
					return true;
				}else {
					return this.next.insertNode(index, stu);
				}
			}
			return false;
		}
		
		//打印节点信息
		public void printNode() {
			if(this.next!=null) {
				System.out.println("->(id:"+this.next.data.id
						+",name:"+this.next.data.name
						+",age:"+this.next.data.age
						+")");
				this.next.printNode();
			}
		}
		
	}
}

public class LinkTest {
	public void main() {
		NodeManage nm=new NodeManage();
		System.out.println("-----add-----");
		nm.add(new Student(1,"小明",20));
		nm.add(new Student(2,"小红",21));
		nm.add(new Student(3,"小芳",22));
		nm.add(new Student(4,"小绪",23));
		nm.add(new Student(5,"小伙",24));
		nm.printAll();
		
		System.out.println("-----update-----");
		boolean res=nm.update(new Student(3,"小芳",22), new Student(6,"小猪",25));
		nm.printAll();
		System.out.print(res);
		
		System.out.println("-----delete-----");
		nm.delete(new Student(6,"小猪",25));
		nm.printAll();
		
		System.out.println("-----insert-----");
		res=nm.insert(2, new Student(3,"小芳",22));
		nm.printAll();
		System.out.print(res);
	}
}

测试结果:

-----add-----
->(id:1,name:小明,age:20)
->(id:2,name:小红,age:21)
->(id:3,name:小芳,age:22)
->(id:4,name:小绪,age:23)
->(id:5,name:小伙,age:24)

-----update-----
->(id:1,name:小明,age:20)
->(id:2,name:小红,age:21)
->(id:6,name:小猪,age:25)
->(id:4,name:小绪,age:23)
->(id:5,name:小伙,age:24)

true-----delete-----
->(id:1,name:小明,age:20)
->(id:2,name:小红,age:21)
->(id:4,name:小绪,age:23)
->(id:5,name:小伙,age:24)

-----insert-----
->(id:1,name:小明,age:20)
->(id:2,name:小红,age:21)
->(id:3,name:小芳,age:22)
->(id:4,name:小绪,age:23)
->(id:5,name:小伙,age:24)

true
  • 二叉树实现
public class BinaryTree {
	private Node root;
	
	public void add(int data) {
		if(null!=root) {
			root.addNode(data);
		}else {
			root=new Node(data);
		}
	}
	public void print() {
		if(null!=root) {
			root.printNode();
		}
	}
	private class Node{
		private int data;
		private Node left;//左子树
		private Node right;//右子树
		
		public Node(int data) {
			this.data=data;
		}
		public void addNode(int data) {
			if(this.data>=data) {
				if(this.left!=null) {//存在左子树
					this.left.addNode(data);
				}else {
					this.left=new Node(data);
				}
			}else {
				if(this.right!=null) {//存在左子树
					this.right.addNode(data);
				}else {
					this.right=new Node(data);
				}
			}
		}
		
		//先序遍历(左,中,右)
		public void printNode() {
			if(this.left!=null) {
				this.left.printNode();
			}
			System.out.print(this.data+"->");
			if(this.right!=null) {
				this.right.printNode();
			}
		}
	}
}

public class binaryTreeTest {
	public static void main(String args[]) {
		BinaryTree t=new BinaryTree();
		//8,3,10,1,6,14,4,7,13
		t.add(8);
		t.add(3);
		t.add(10);
		t.add(1);
		t.add(6);
		t.add(14);
		t.add(4);
		t.add(7);
		t.add(13);
		t.print();
		
	}
}

测试结果:

1->3->4->6->7->8->10->13->14->
发布了46 篇原创文章 · 获赞 90 · 访问量 33万+

猜你喜欢

转载自blog.csdn.net/qq_40077167/article/details/86315303
今日推荐