手写二叉树

1.定义节点类

package com.lyf.binarytree;

public class Node<T extends Comparable<T>> {
	private Node<T> left;
	private T value;
	private Node<T> right;
	public Node() {
		super();
	}
	
	public Node(T value) {
		super();
		this.value = value;
	}

	public Node<T> getLeft() {
		return left;
	}
	public void setLeft(Node<T> left) {
		this.left = left;
	}
	public T getValue() {
		return value;
	}
	public void setValue(T value) {
		this.value = value;
	}
	public Node<T> getRight() {
		return right;
	}
	public void setRight(Node<T> right) {
		this.right = right;
	}

	@Override
	public String toString() {
		return "Node [value=" + value + "]";
	}
	
}

2.定义二叉树:

package com.lyf.binarytree;

import java.util.ArrayList;
import java.util.List;

public class BinaryTree<T extends Comparable<T>> {
	private Node<T> root;

	
	public BinaryTree() {
		super();
	}
	
	
	public BinaryTree(T value) {
		super();
		this.root = new Node<T>(value);
	}

	public Node<T> getRoot() {
		return root;
	}


	public void setRoot(Node<T> root) {
		this.root = root;
	}
	
	public T add(T value) {
		if(null==root) {
			root = new Node<T>(value);
			return value;
		}else {
			Node<T> current = root;
			while(true) {
				if(current.getValue().compareTo(value)>0) {
					//小于临时节点
					if(null!=current.getLeft()) {
						current = current.getLeft();
					}else {
						Node<T> node = new Node<T>(value);
						current.setLeft(node);
                        return value;
					}
				}else if(current.getValue().compareTo(value)<0) {
					//大于临时节点
					if(null!=current.getRight()) {
						current = current.getRight();
					}else {
						Node<T> node = new Node<T>(value);
						current.setRight(node);
                        return value;
					}
				}else {
					//等于临时节点
					return null;
				}
			}
		}
	}
	public Node<T> get(T value){
		if(null==root) {
			return null;
		}else {
			Node<T> current = root;
			while(true) {
				if(current.getValue().compareTo(value)>0) {
					//小于临时节点
					if(null!=current.getLeft()) {
						current = current.getLeft();
					}else {
						return null;
					}
				}else if(current.getValue().compareTo(value)<0) {
					//大于临时节点
					if(null!=current.getRight()) {
						current = current.getRight();
					}else {
						return null;
					}
				}else {
					//等于临时节点
					return current;
				}
			}
		}
	}
	
	public void forEach(Node<T> node) {
		if(null!=node) {
			if(null!=node.getLeft()) {
				forEach(node);
			}
			System.out.println(node.getValue());
			if(null!=node.getRight()) {
				forEach(node);
			}
		}
	}
	public void forEach2(Node<T> node,List<T> list) {
		if(null!=node) {
			if(null!=node.getLeft()) {
				forEach2(node.getLeft(),list);
			}
			list.add(node.getValue());
			if(null!=node.getRight()) {
				forEach2(node.getRight(),list);
			}
		}
	}


	@Override
	public String toString() {
		List<T> list = new ArrayList<T>();
		forEach2(root,list);
		return list.toString();
	}
	

}

3.实体类:

package com.lyf.binarytree;

public class Student implements Comparable<Student> {

	private int age;
	private String name;
	
	public Student(int age, String name) {
		super();
		this.age = age;
		this.name = name;
	}

	public Student() {
		super();
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int compareTo(Student o) {
		if(this.getAge()>o.getAge()) {
			return 1;
		}
		if(this.getAge()<o.getAge()) {
			return -1;
		}
		return this.getName().compareTo(o.getName());
	}

	@Override
	public String toString() {
		return "Student [age=" + age + ", name=" + name + "]";
	}

}

4.测试类:

package com.lyf.binarytree;

public class Test {

	public static void main(String[] args) {
		
		BinaryTree<Student> node = new BinaryTree<Student>();
		Random random = new Random();
		for(int i=0;i<10;i++) {
			Student student = new Student(random.nextInt(10), ""+random.nextInt(10)+i);
			node.add(student);
		}
		System.out.println(node.toString());
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值