//堆栈实现的代码
package traverse;
public class Stack {
Object[] data;//数据
int maxSize;//栈的最大容量
int top;//顶点
public Stack(int maxSize){
this.maxSize=maxSize;
data=new Object[maxSize];
top=-1;
}
public int getSize(){
return maxSize;
}
public int getElementCount(){
return top;
}
public boolean isEmpty(){
return top==-1;
}
public boolean isFull(){
return top+1==maxSize;
}
public boolean push(Object data){
if(isFull()){
System.out.println("栈已满!");
return false;
}
this.data[++top]=data;
return true;
}
public Object pop()throws Exception{
if(isEmpty())
{
throw new Exception("栈已空!");
}
return this.data[top--];
}
public Object peek(){
return this.data[getElementCount()];
}
}
二叉树实现代码:
package traverse;
public class BinaryTree {
private Node root;
private static class Node {
Node left;
Node right;
int data;
Node(int newData) {
left = null;
right = null;
data = newData;
}
}
/**
* 创建一个空的二叉树
*/
public BinaryTree() {
root = null;
}
/**
* 递归的插入数值
* data 要插入的数值
*/
public void insert(int data) {
root = insert(root, data);
}
/**
* 将数值插入到二叉树中,比当前结点小或等于当前结点的插在当前结点的左侧,比当前结点大的数插在当前结点的右侧,每次从根结点开始递归比较
* node 当前的结点,就是根结点,只是每次根结点的左右子孙更新
* data 要插入的数值
* 新排好的二叉树
*/
private Node insert(Node node, int data) {
if (node == null) {
node = new Node(data);
} else {
if (data <= node.data) {
node.left = insert(node.left, data);
} else {
node.right = insert(node.right, data);
}
}
return (node);
}
/**
* 将数值输入构建二叉树
* data 要输入的数值
*/
public void buildTree(int[] data) {
for (int i = 0; i < data.length; i++) {
insert(data[i]);
}
}
/**
* 递归打印出二叉树
*/
public void printTree() {
printTree(root);
System.out.println();
}
/**
* 从根结点开始遍历,从树的最高层叶子结点开始输出,从左至右
* node 当前的结点
*/
private void printTree(Node node) {
if (node == null)
return;
// left, node itself, right
printTree(node.left);
System.out.print(node.data + " ");
printTree(node.right);
}
}
哈希表代码:
package traverse;
public class HashTable{
private String[] name; //关键字
private int sum; //容量
public HashTable(){ //初始化,初始容量是10个
name = new String[10];
sum = 0;
}
public int hash1(String s){ //哈希函数
return Math.abs(s.hashCode())%name.length;
}
public int hash2(String s){ //处理冲突的哈希函数
int result = Math.abs(s.hashCode())%(name.length-1);
System.out.println(s+"--"+result);
if(result%2==0){
return result + 1;
}
return result;
}
public boolean contains(String s){ //哈希表里面是否包含字符串s
int start = hash1(s);
int i = start;
while (name[i] != null){
if(name[i].equals(s)){
return true;
}
i = (i + hash2(s))%name.length;
if(i == start){
return false;
}
}
return false;
}
public void add(String s){
if(sum>=name.length/2){
this.rehash();
}
int start = hash1(s);
int i = start;
while(name[i] != null){
if(s.equals(name[i])){
return;
}
i = (i + hash2(s))%name.length;
if(i == start){
return;
}
}
name[i] = s;
sum ++;
}
public void rehash(){ //扩建一个哈希表为原表的两倍,把原来的哈希表添加到新表中
HashTable ht = new HashTable();
ht.name = new String[this.name.length * 2];
for(int i = 0; i < this.name.length; i ++){
if((this.name[i] != null)){
ht.add(this.name[i]);
}
}
this.name = ht.name;
this.sum = ht.sum;
}
public void remove(String s){ //删除某个元素
if(this.contains(s)){
int i = this.getValue(s);
this.name[i] = null;
}
}
public int getValue(String s){ //得到s在哈希表中的位置
int start = this.hash1(s);
int i = start;
while(this.name[i] != null){
if(this.name[i].equals(s)){
return i;
}
i = (i + this.hash2(s))%this.name.length;
if(i == start){
return -1;
}
}
return -1;
}
public void print(){ //输出哈希表中所有元素
for(int i = 0; i < name.length; i ++){
System.out.println(i+":"+name[i]);
}
}
public int size(){ //哈希表存储元素的个数
return this.sum;
}
public int length(){ //哈希表的长度
return this.name.length;
}
}
主函数遍历:
package traverse;
import traverse.BinaryTree;
import traverse.Stack;
import traverse.HashTable;
public class traverse {
public static void main(String[] args) throws Exception {
BinaryTree tree=new BinaryTree();
int[] data = { 2, 8, 7, 4 ,9,3,1,6,7,5};
tree.buildTree(data);
tree.printTree();
HashTable table = new HashTable();
table.add("one");
table.add("two");
table.add("three");
table.add("four");
table.add("fve");
table.add("six");
System.out.println(table.contains("two"));
table.remove("six");
System.out.println(table.contains("six"));
table.print();
Stack stack=new Stack(100);
stack.push(new String("1"));
stack.push(new String("2"));
stack.push(new String("3"));
stack.push(new String("4"));
stack.push(new String("5"));
while(!stack.isEmpty())
{
System.out.print(stack.peek());
stack.pop();
}
}
}