双向链表
public class SxNode {
public Node head;
public Node tail;
public void deleteNode(Node node){
Node temp = this.head;
while (temp != null){
if(temp.value.equals(node.value)){
if(temp == this.tail && temp == this.head ){
this.head = null;
this.tail = null;
}else if(temp.pre == null){
this.head = temp.next;
temp.next.pre = null;
}else if(temp.next == null){
this.tail = temp.pre;
temp.pre.next = null;
}else{
temp.pre.next = temp.next;
temp.next.pre = temp.pre;
}
break;
}
temp = temp.next;
}
}
public void addNode(Node node){
if(this.head == null && this.tail == null){
this.head = node;
this.tail = node;
}else{
Node temp = this.head;
while (temp.next != null){
temp = temp.next;
}
temp.next = node;
node.pre = temp;
this.tail = node;
}
}
}
class Node {
public Node next;
public String value;
public Node(String value) {
this.value = value;
}
public Node pre;
}
public class NodeMain {
public static void main(String[] args) {
Node node1 = new Node("1");
Node node2 = new Node("2");
Node node3 = new Node("3");
Node node4 = new Node("4");
Node node5 = new Node("5");
Node node6 = new Node("6");
SxNode sxNode = new SxNode();
sxNode.addNode(node1);
Node head = sxNode.head;
Node temp = head;
while (temp != null){
System.out.println(temp.value);
temp = temp.next;
}
sxNode.deleteNode(node1);
Node head1 = sxNode.head;
Node temp1 = head1;
while (temp1 != null){
System.out.println(temp1.value);
temp1 = temp1.next;
}
}
}
二叉树
public class TwoTree {
TreeNode root;
public void addNode(TreeNode treeNode){
if(root == null){
this.root = treeNode;
}else {
TreeNode currentNode = this.root;
TreeNode parent ;
for(;;){
parent = currentNode;
if(treeNode.compareTo(currentNode) > 0){
currentNode = currentNode.right;
if (currentNode == null){
parent.right = treeNode;
break;
}
}else {
currentNode = currentNode.left;
if (currentNode == null){
parent.left = treeNode;
break;
}
}
}
}
}
/**
* 中序遍历
*
* @param treeNode
*/
public void aroundOrder(TreeNode treeNode) {
if (treeNode != null) {
aroundOrder(treeNode.left);
System.out.print(" " + treeNode.value + " ");
aroundOrder(treeNode.right);
}
}
/**
* 后序遍历
*
* @param treeNode
*/
public void afterOrder(TreeNode treeNode) {
if (treeNode != null) {
afterOrder(treeNode.left);
afterOrder(treeNode.right);
System.out.print(" " + treeNode.value + " ");
}
}
/**
* 先序遍历
*
* @param treeNode
*/
public void beforeOrder(TreeNode treeNode) {
if (treeNode != null) {
System.out.print(" " + treeNode.value + " ");
beforeOrder(treeNode.left);
beforeOrder(treeNode.right);
}
}
}
class TreeNode implements Comparable<TreeNode> {
public TreeNode left;
public String value;
public TreeNode(String value) {
this.value = value;
}
public TreeNode right;
@Override
public int compareTo(TreeNode o) {
return this.value.compareTo(o.value);
}
}
public class TwoTreeMain {
public static void main(String[] args) {
TwoTree twoTree = new TwoTree();
twoTree.addNode(new TreeNode("e"));
twoTree.addNode(new TreeNode("b"));
twoTree.addNode(new TreeNode("d"));
twoTree.addNode(new TreeNode("a"));
twoTree.addNode(new TreeNode("c"));
twoTree.aroundOrder(twoTree.root);
System.out.println("\n");
twoTree.afterOrder(twoTree.root);
System.out.println("\n");
twoTree.beforeOrder(twoTree.root);
}
}