Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1790125
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-08-27 11:26:46

abstract Class ListItem

点击(此处)折叠或打开

  1. package abstract_exp;

  2. public abstract class ListItem{
  3.     protected ListItem rightLink;
  4.     protected ListItem leftLink;
  5.     protected Object value;

  6.     public ListItem(Object value) {
  7.         this.value = value;
  8.     }

  9.     abstract ListItem next();
  10.     abstract ListItem setNext(ListItem item);
  11.     abstract ListItem prev();
  12.     abstract ListItem setPrev(ListItem item);
  13.     abstract int compareTo(ListItem other);

  14.     public Object getValue(){
  15.         return value;
  16.     }

  17.     public void setValue(Object value){
  18.         this.value = value;
  19.     }
  20. }
下面是interface 

点击(此处)折叠或打开

  1. public interface NodeList {
  2.     ListItem getRoot();
  3.     boolean addItem(ListItem newItem);
  4.     boolean removeItem(ListItem item);
  5.     void traverse(ListItem root);

  6. }


Node 

点击(此处)折叠或打开

  1. public class Node extends ListItem {
  2.     public Node(Object value) {
  3.         super(value);
  4.     }

  5.     @Override
  6.     ListItem next() {
  7.         return this.rightLink;
  8.     }

  9.     @Override
  10.     ListItem setNext(ListItem item) {
  11.         this.rightLink = item;
  12.         return this.rightLink;
  13.     }

  14.     @Override
  15.     ListItem prev() {
  16.         return leftLink;
  17.     }

  18.     @Override
  19.     ListItem setPrev(ListItem item) {
  20.         this.leftLink = item;
  21.         return this.leftLink;
  22.     }

  23.     @Override
  24.     int compareTo(ListItem other) {
  25.         if(other != null){
  26.             return ((String) super.getValue()).compareTo(
  27.                     (String) other.getValue());
  28.         }else{
  29.             return -1;
  30.         }
  31.     }
  32. }
MyLinkedList

点击(此处)折叠或打开

  1. public class MyLinkList implements NodeList {
  2.     private ListItem root = null;

  3.     public MyLinkList(ListItem root) {
  4.         this.root = root;
  5.     }

  6.     @Override
  7.     public ListItem getRoot() {
  8.         return this.root;
  9.     }

  10.     @Override
  11.     public boolean addItem(ListItem newItem) {
  12.         if (this.root == null) {
  13.             //The list was empty,
  14.             this.root = newItem;
  15.             return true;
  16.         }
  17.         ListItem currentItem = this.root;
  18.         while (currentItem != null) {
  19.             int comparison = (currentItem.compareTo(newItem));
  20.             if (comparison < 0) {
  21.                 //new item is greater, move right if possible
  22.                 if (currentItem.next() != null) {
  23.                     currentItem = currentItem.next();
  24.                 } else {
  25.                     //There is no next, so insert at end of list
  26.                     currentItem.setNext(newItem);
  27.                     newItem.setPrev(currentItem);
  28.                     return true;
  29.                 }
  30.             } else if (comparison > 0) {
  31.                 //new Item is less, insert before
  32.                 if (currentItem.prev() != null) {
  33.                     currentItem.prev().setNext(newItem);
  34.                     newItem.setPrev(currentItem.prev());
  35.                     newItem.setNext(currentItem);
  36.                     currentItem.setPrev(newItem);
  37.                 } else {
  38.                     //the node with a prev is the root
  39.                     newItem.setNext(this.root);
  40.                     this.root.setPrev(newItem);
  41.                     this.root = newItem;
  42.                 }
  43.                 return true;
  44.             } else {
  45.                 //equal
  46.                 System.out.println(newItem.getValue() + " is already present at the list");
  47.                 return false;
  48.             }
  49.         }
  50.         return false;
  51.     }

  52.     @Override
  53.     public boolean removeItem(ListItem item) {
  54.         if(item != null){
  55.             System.out.println("Deleting item "+item.getValue());
  56.         }
  57.         ListItem currentItem = this.root;
  58.         while(currentItem != null){
  59.             int comparison = currentItem.compareTo(item);
  60.             if(comparison == 0) {
  61.                 //found the item to delete
  62.                 if(currentItem == this.root){
  63.                     this.root = currentItem.next();
  64.                 }else{
  65.                     currentItem.prev().setNext(currentItem.next());
  66.                     if(currentItem.next() != null){
  67.                         currentItem.next().setPrev(currentItem.prev());
  68.                     }
  69.                 }
  70.                 return true;
  71.             }else if(comparison < 0){
  72.                 currentItem = currentItem.next();
  73.             }else{
  74.                 //comparison > 0
  75.                 //we are at an item greater than the one to be deleted
  76.                 //so the item is not in the list
  77.                 return false;
  78.             }
  79.         }
  80.         //we have reached the end of the list
  81.         //without finding the item to delete
  82.         return false;
  83.     }

  84.     @Override
  85.     public void traverse(ListItem root) {
  86.         if (root == null) {
  87.             System.out.println("The list is empty");
  88.         } else {
  89.             while (root != null) {
  90.                 System.out.println(root.getValue());
  91.                 root = root.next();
  92.             }
  93.         }
  94.     }
  95. }
SearchTree

点击(此处)折叠或打开

  1. public class SearchTree implements NodeList {
  2.     private ListItem root = null;

  3.     //constructor
  4.     public SearchTree(ListItem root){
  5.         this.root = root;
  6.     }

  7.     @Override
  8.     public ListItem getRoot() {
  9.         return this.root;
  10.     }

  11.     @Override
  12.     public boolean addItem(ListItem newItem) {
  13.         if(this.root == null){
  14.             //the tree was empty, so our item becomes the
  15.             //head of the tree
  16.             this.root = newItem;
  17.             return true;
  18.         }
  19.         //other waise, starting comparing from the head of the tree
  20.         ListItem currentItem = this.root;
  21.         while(currentItem != null){
  22.             int comparison = (currentItem.compareTo(newItem));
  23.             if(comparison < 0){
  24.                 //newItem is greater ,move right if possible
  25.                 if(currentItem.next() != null){
  26.                     currentItem=currentItem.next();
  27.                 }else{
  28.                     //there is no node to the right, so add at this point
  29.                     currentItem.setNext(newItem);
  30.                     return true;
  31.                 }
  32.             }else if(comparison > 0){
  33.                 //newItem is less, move left is possible
  34.                 if(currentItem.prev() != null){
  35.                     currentItem = currentItem.prev();
  36.                 }else{
  37.                     //there is no node to the left, so add at this point
  38.                     currentItem.setPrev(newItem);
  39.                     return true;
  40.                 }
  41.             }else{
  42.                 //equal, so do not add
  43.                 System.out.println(newItem.getValue() +" is already existed in ");
  44.                 return false;

  45.             }
  46.         }
  47.         //we can not actually get here, but java complains if there's no return
  48.         return false;
  49.     }

  50.     @Override
  51.     public boolean removeItem(ListItem item) {
  52.         if(item !=null){
  53.             System.out.println("Deleting item "+item.getValue());
  54.         }

  55.         ListItem currentItem = this.root;
  56.         ListItem parentItem = currentItem;

  57.         while(currentItem != null){
  58.             int comparison = (currentItem.compareTo(item));
  59.             if(comparison < 0){
  60.                 parentItem = currentItem;
  61.                 currentItem = currentItem.next();
  62.             }else if(comparison > 0){
  63.                 parentItem = currentItem;
  64.                 currentItem = currentItem.prev();
  65.             }else{
  66.                 //equal, we've found the item so remove it
  67.                 performRemove(currentItem, parentItem);
  68.                 return true;
  69.             }
  70.         }
  71.         return false;
  72.     }

  73.     private void performRemove(ListItem item, ListItem parent){
  74.         //remove item from the tree
  75.         if(item.next() == null){
  76.             //no right tree, so make parent point to left ree(which may be null)
  77.             if(parent.next() == item){
  78.                 //item is rigt child of its parent
  79.                 parent.setNext(item.prev());
  80.             }else if(parent.prev() == item){
  81.                 //item is left child of its parent
  82.                 parent.setPrev(item.prev());
  83.             }else{
  84.                 //parent must be item, which means we were looking at the root of the tree
  85.                 this.root = item.prev();
  86.             }
  87.         }else if(item.prev()== null){
  88.             //no left tree, so make parent point to right tree(which may be null)
  89.             if(parent.next()==item){
  90.                 //item is right child of its parent
  91.                 parent.setNext(item.next());
  92.             }else if(parent.prev() == item){
  93.                 //item is left child of its parent
  94.                 parent.setPrev(item.next());
  95.             }else{
  96.                 //again, we are deleting the root
  97.                 this.root = item.next();
  98.             }
  99.         }else{
  100.             //neither left nor right are null, deletion is now a lot
  101.             //trickier , From the right sub-tree, find the smallest value
  102.             //ie: the leftmost
  103.             ListItem current=item.next();
  104.             ListItem leftMostParent = item;
  105.             while(current.prev() != null){
  106.                 leftMostParent = current;
  107.                 current = current.prev();
  108.             }

  109.             //now put the smallest value into our node to be deleted
  110.             item.setValue(current.getValue());
  111.             //and delete the smallest
  112.             if(leftMostParent == item){
  113.                 //there was no left most node, so current points to the smallest
  114.                 //node (the one that must now be delete)
  115.                 item.setNext(current.next());
  116.             }else{
  117.                 //set the smallest node's parent to point to
  118.                 //the smallest node's right child (which may be null)
  119.                 leftMostParent.setPrev(current.next());
  120.             }
  121.         }
  122.     }

  123.     @Override
  124.     public void traverse(ListItem root) {
  125.         if( null){
  126.             traverse(root.prev());
  127.             System.out.println(root.getValue());
  128.             traverse(root.next());
  129.         }
  130.     }
  131. }

阅读(1109) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~