论坛首页 编程语言技术论坛

二叉排序树使用List 实现(JAVA)

浏览 1269 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-01-04  

使用JAVA中的List构建二叉排序树。其中,iterator方法不知道怎么样写,请大伙支援?代码如下:

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

public class BinaryTreeList<E> extends AbstractSingleLinkedList<E> implements List<E> ,java.io.Serializable {

	private static final long serialVersionUID = -5672690815694732101L;
	private Entry<E>	root;
	private int size ;
	public BinaryTreeList() {
		this.root = null;
	}
	
	public boolean add(E e) {
		Entry<E> newEntry = new Entry<E>(e,null,null);
		if (this.root == null) {
			this.root = newEntry;
		} else {
			Comparable<? super E> k = (Comparable<? super E>) e;
			Entry<E> currentEntry = this.root;
			Entry<E> parentEntry = null;
			int result = 0;
			while (currentEntry != null) {
				result = k.compareTo(currentEntry.data);
				parentEntry = currentEntry;
				if (result > 0) {
					currentEntry = currentEntry.rigthChild;
				} else {
					currentEntry = currentEntry.leftChild;
				}
			}
			if (result > 0) {
				parentEntry.rigthChild = newEntry;
			} else {
				parentEntry.leftChild = newEntry;
			}
			
		}
		size ++;
		return true;
	}
	
	public Entry<E> getRoot() {
		return this.root;
	}
	
	/**
	 * 使用递归遍历tree
	 * @param tree
	 * @param result
	 * @return
	 */
	private void preOrder(Entry<E> tree,List<E> result) {
		if (tree != null) {
			preOrder(tree.leftChild,result);
			result.add(tree.data);
			preOrder(tree.rigthChild,result);
		}
	}
	
	public List<E> showTree() {
		List<E> result = new ArrayList<E>(size);
		preOrder(getRoot(),result);
		return result;
	}
	
	@Override
	public Iterator<E> singleListIterator() {
		/**
		 * 这个地方怎么样实现呢 ?
		 * 就是怎么样把递归写成循环?
		 */
		return new Iterator<E>() {
			private Entry<E> current = root;
			@Override
			public boolean hasNext() {
				return false;
			}

			@Override
			public E next() {
				return null;
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException("can not remove");
			}
			
		};
	}

	@Override
	public E get(int index) {
		throw new UnsupportedOperationException("can not get");
	}

	@Override
	public int size() {
		return size;
	}

	public static class Entry<E>{
		E data;
		Entry<E> leftChild;
		Entry<E> rigthChild;
		
		public Entry(E e,Entry<E> leftChild,Entry<E> rigthChild) {
			this.data = e;
			this.leftChild = leftChild;
			this.rigthChild = rigthChild;
		}
	}
}

 测试类如下:

import junit.framework.TestCase;

import org.junit.Test;


public class BinaryTreeListTest extends TestCase {

	@Test
	public void testAdd() {
		BinaryTreeList<Integer> list = new BinaryTreeList<Integer>();
		list.add(381);
		list.add(12);
		list.add(410);
		list.add(9);
		list.add(40);
		list.add(394);
		list.add(540);
		list.add(35);
		list.add(190);
		list.add(476);
		list.add(760);
		list.add(146);
		list.add(445);
		list.add(600);
		list.add(800);
		//preOrder(list.getRoot());
		for (Integer val : list.showTree()) {
			System.out.print(val);
			System.out.print('\t');
		}
	}
	/*
	public static void preOrder(BinaryTreeList.Entry<Integer> tree) {
		if (tree != null) {
			preOrder(tree.leftChild);
			System.out.print(tree.data);
			System.out.print('\t');
			preOrder(tree.rigthChild);
		}
	}
	*/
}

 输出结果:

9	12	35	40	146	190	381	394	410	445	476	540	600	760	800	

 不足之处:BinaryTreeList 的对象不能使用foreach遍历出结果,必须先调用 list.showTree() 生成一个arrayList。求教:iterator怎么实现呢?

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics