java二叉树

网上有关于二叉数的java实现http://blog.csdn.net/skylinesky/article/details/6611442

多数案例都没有键值,有键值的也全是整型。我用java实现了一个可以任何对象为键的二叉数

package Tree;

import java.io.IOException;

public class Tree<I extends Comparable<I> ,V> {

	@SuppressWarnings("rawtypes")
	private Node root;

	@SuppressWarnings("unchecked")
	public Node<I, V> getRoot() {
		return root;
	}

	public void setRoot(Node<I, V> root) {
		this.root = root;
	}

	//向树添加节点
	@SuppressWarnings({ "unchecked" })
	public void add(I index,V value){

		if(root == null){
			root = new Node<I, V>(index,value);
			System.out.println(root);
		}else{
			Node<I, V> current = root;
			Node<I, V> parent = null;
			while(true){
				parent = current;
				int result = index.compareTo(current.getIndex());
				if(result < 0){
					current = current.getLeft();
					if(current == null){
						current = new Node<I, V>(index,value);
						parent.setLeft(current);
						return ;
					}
				}else{
					current = current.getRight();
					if(current == null){
						current = new Node<I, V>(index,value);
						parent.setRight(current);
						return ;
					}
				}
			}
		}

	}

	//查找
	@SuppressWarnings("unchecked")
	public Node<I, V> find(I index){
		Node<I, V> current = root;
		int result = index.compareTo(current.getIndex());
		while(result != 0){
			if(result < 0){
				current = current.getLeft();
			}else {
				current = current.getRight();
			}
			if(current == null){
				return null;
			}
			result = index.compareTo(current.getIndex());
		}

		return current;
	}

	//中序遍历
	public void centerTraversal(Node<I, V> node){
		if(node != null){
			centerTraversal(node.getLeft());
			System.out.println(node);
			centerTraversal(node.getRight());
		}
	}

	//前序遍历
	public void leftTraversal(Node<I, V> node){
		if(node != null){
			System.out.println(node);
			centerTraversal(node.getLeft());
			centerTraversal(node.getRight());
		}
	}

	//后序遍历
	public void rightTraversal(Node<I, V> node){
		if(node != null){
			System.out.println(node);
			centerTraversal(node.getLeft());
			centerTraversal(node.getRight());
		}
	}

	@SuppressWarnings("unchecked")
	public void delete(I index){

		Node<I, V> current = root;
		boolean isLeft = true;
		Node<I, V> parent = null;
		int result = index.compareTo(current.getIndex()); 

		//找到要删除的结点
		while(result != 0){
			parent = current;
			System.out.println(current.getIndex());
			if(result < 0){
				current = current.getLeft();
				isLeft = true;
			}else {
				current = current.getRight();
				isLeft = false;
			}
			if(current == null){
				throw new RuntimeException(" \"" + index + "\" 没有这个节点");
			}
			result = index.compareTo(current.getIndex());
		}

		//这个要删除的节点没有子结点
		if(current.getLeft() == null && current.getRight() == null){
			if(current == root){
				root = null;
			}else{
				if(isLeft){
					parent.setLeft(null);
				}else{
					parent.setLeft(null);
				}
			}
		}else if(current.getRight() == null){
			//只有左子结点

			if(current == root){
				root = root.getLeft();
			}else{
				if(isLeft){
					parent.setLeft(current.getLeft());
				}else{
					parent.setRight(current.getLeft());
				}
			}
		//只有右子结点
		}else if(current.getLeft() == null){
			if(current == root){
				root = root.getRight();
			}else{
				if(isLeft){
					parent.setLeft(current.getRight());
				}else{
					parent.setRight(current.getRight());
				}
			}
		}else{
			//左右子节点都有的情况下,
			Node<I,V> follow = current;
			Node<I,V> followParent = follow;
			Node<I,V> followCurrent = follow.getRight();

			if(followCurrent.getLeft() == null){
				if(isLeft){
					parent.setLeft(current.getRight());
				}else{
					parent.setRight(current.getRight());
				}
				current.getRight().setLeft(current.getLeft());
			}else{
				while(followCurrent != null){
					followParent = follow;
					follow = followCurrent;
					followCurrent = followCurrent.getLeft();
				}
				followParent.setLeft(follow.getRight());
				if(isLeft){
					parent.setLeft(follow);
				}else{
					parent.setRight(follow);
				}
				follow.setLeft(current.getLeft());
				follow.setRight(current.getRight());
			}

		}

	}

	 public static void main(String[] args) throws IOException{
	     Tree<Integer,Double> tree = new Tree<Integer,Double>();
	     tree.add(50, 1.5);
	     tree.add(25, 1.2);
	     tree.add(75, 1.7);
	     tree.add(12, 1.5);
	     tree.add(37, 1.2);
	     tree.add(43, 1.7);
	     tree.add(30, 1.5);
	     tree.add(33, 1.2);
	     tree.add(87, 1.7);
	     tree.add(93, 1.5);
	     tree.add(97, 1.5);

	     tree.leftTraversal(tree.getRoot());
	     System.out.println();

	     tree.delete(30);

	     tree.centerTraversal(tree.getRoot());
	     System.out.println();

	     tree.rightTraversal(tree.getRoot());
	     System.out.println();

	 }
}
package Tree;

public class Node<I extends Comparable<I> ,V> {
	//节点的关键字
	private I index;

	//节点的值
	private V value;

	private Node<I,V> left;

	private Node<I,V> right;

	public Node(){
	}

	public Node(I index,V value){
		this(index,value,null,null);
	}

	public Node(I index,V value,Node<I,V> left,Node<I,V> right){
		this.index = index;
		this.value = value;
		this.left = left;
		this.right = right;
	}

	public I getIndex() {
		return index;
	}

	public void setIndex(I index) {
		this.index = index;
	}

	public V getValue() {
		return value;
	}

	public void setValue(V value) {
		this.value = value;
	}

	public Node<I, V> getLeft() {
		return left;
	}

	public void setLeft(Node<I, V> left) {
		this.left = left;
	}

	public Node<I, V> getRight() {
		return right;
	}

	public void setRight(Node<I, V> right) {
		this.right = right;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(" { left:").append("[index:").append(left!=null?left.index:"null").append(" , value:").append(left!=null?left.value:"null").append("] } ")
				.append(" { self:").append("[index:").append(index).append(", value:").append(value).append("] } ")
				.append(" { right:").append("[index:").append(right!=null?right.index:"null").append(", value:").append(right!=null?right.value:"null").append("] } ");
		return sb.toString();
	}

	public static void main(String args[]){
		Node<String,String> root = new Node<String,String>();
		root.index = "父节点";
		root.value = "啊哈";
		Node<String,String> left = new Node<String,String>();
		left.index = "左节点";
		left.value = "你妹";
		Node<String,String> right = new Node<String,String>();
		right.index = "右节点";
		right.value = "我妹";
		//root.left = left;
		//root.right = right;
		System.out.println(root);
	}

}

java二叉树,布布扣,bubuko.com

时间: 2024-10-17 05:40:16

java二叉树的相关文章

代写程序|java二叉树字典查询(qq 928900200)

This assignment will help you practice and understand better the Binary Tree and Binary Search Tree data structures, their operations and implementations. You are to design a small dictionary using the Binary Search Tree data structure. Each entry ma

java 二叉树的遍历 为什么只给出前序以及后序遍历,不能生成唯一的二叉树

最近在学习java的数据结构与算法知识,看到数据结构 树的遍历的方式.在理解过程中.查看到一篇文章,视野非常有深度,在信息论的角度看待这个问题.在此贴出该文章的链接以及内容. [文章出处]http://www.binarythink.net/2012/12/binary-tree-info-theory/ 我们在学习二叉树的遍历时,都会不可避免的学到二叉树的三种遍历方式,分别是遵循(根-左-右)的前序遍历.遵循(左-根-右)的中序遍历以及遵循(左-右-根)的后序遍历.并且每一个二叉树都可以用这三

java二叉树的实现和遍历

/* * Java实现二叉树 */ public class BinaryTree { int treeNode; BinaryTree leftTree; BinaryTree rightTree; public BinaryTree(int Data) { // TODO Auto-generated constructor stub treeNode=Data; leftTree=null; rightTree=null; } public void insert(BinaryTree n

毕业了-java二叉树层次遍历算法

/*************************************** * 时间:2017年6月23日 * author:lcy * 内容:二叉树的层次遍历 * 需要借助队列这个数据结构,直接import就可以了 * 1.首先将根节点放入队列中. 2.当队列为非空时,循环执行步骤3到步骤5,否则执行6: 3.出队列取得一个结点,访问该结点: 4.若该结点的左子树为非空,则将该结点的左子树入队列: 5.若该结点的右子树为非空,则将该结点的右子树入队列: 6.结束. ***********

Java二叉树的递归,非递归遍历,高度,节点数,叶子节点数

import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class Main { public static class TreeNode<T>{ T data; TreeNode<T> left=null; TreeNode<T> right=null; public TreeNode() {} public TreeNode(T data){ this.d

java二叉树实现代码

public class Tree { private TreeNode root = null; public Tree() { root = new TreeNode(1, "A"); } private class TreeNode { private int key; private String data; private boolean isVisted; private TreeNode leftChild; private TreeNode rightChild; pu

JAVA二叉树的创建以及各种功能的实现

直接上代码了,代码说得很清楚了 package BTree; public class BTree { private Node root; private class Node { private Node lchild; private Node rchild; private int data; public Node(int data) { this.lchild = null; this.rchild = null; this.data = data; } } public BTree

Java 二叉树遍历右视图-LeetCode199

题目如下: 题目给出的例子不太好,容易让人误解成不断顺着右节点访问就好了,但是题目意思并不是这样. 换成通俗的意思:按层遍历二叉树,输出每层的最右端结点. 这就明白时一道二叉树层序遍历的问题,用一个队列来处理,但是问题是怎么来辨别每层的最右端结点,我思考了半天,最后想出的办法是利用一个标记位,例如上面的例子: q代表队列,f代表标记结点,right代表记录的最右端结点 q: 1 flag right:{} q: flag 2 3 遇到标记位所以移动标记位,并将队头弹出的数据存起来如下 q: 2

Java 二叉树遍历

package edu.cumt.jnotnull; import java.util.Stack; public class BinaryTree { protected Node root; public BinaryTree(Node root) { this.root = root; } public Node getRoot() { return root; } /** 构造树 */ public static Node init() { Node a = new Node('A');