每天練習的java基礎題目

每天練習的java基礎題目

1、 鏈表操作:
增加數據
取得鏈表長度
判斷空鏈表
內容查詢
根據索引取得數據
修改鏈表數據
刪除鏈表數據
對象數組轉換

2、 工廠設計模式 | 代理設計模式

3、 equals方法

4、 二叉樹

代碼:
Link.java

// 鏈表操作
public class Link {
    private class Node {
        private Object data;
        private Node next;
        public Node(Object data) {
            this.data = data;
        }
        // addNode
        public void addNode(Node newNode) {
            if (this.next == null)
                this.next = newNode;
            else 
                this.next.addNode(newNode);         

        }

        // containsNode
        public boolean containsNode(Object data) {
            if (data.equals(this.data)) 
                return true;
            if (this.next != null)
                return this.next.containsNode(data);
            return false;
        }

        // getNode
        public Object getNode(int index) {
            if (Link.this.foot ++ == index)
                return this.data;
            return this.next.getNode(index);
        }

        // setNode
        public void setNode(int index, Object data) {
            if (Link.this.foot ++ == index)
                this.data = data;
            else
                this.next.setNode(index, data);
        }

        // removeNode
        public void removeNode(Node previous, Object data) {
            if (data.equals(this.data))
                previous.next = this.next;
            else
                this.next.removeNode(this, data);
        }

        // toArrayNode
        public void toArrayNode() {
            Link.this.retArray[Link.this.foot ++] = this.data;
            if (this.next != null)
                this.next.toArrayNode();
        }
    }
    private Node root;
    private int count = 0;
    private int foot = 0;
    private Object[] retArray;

    // 增加數據
    public void add(Object data) {
        Node newNode = new Node(data);
        if (this.root == null)
            this.root = newNode;
        else 
            this.root.addNode(newNode);
        this.count ++;
    }

    // 取得鏈表長度
    public int size() {
        return this.count;
    }

    // 判斷空鏈表
    public boolean isEmpty() {
        return this.count == 0;
    }

    // 內容查詢
    public boolean contains(Object data) {
        if (this.root == null || data == null)
            return false;

        return this.root.containsNode(data);

    }

    // 根據索引取得數據
    public Object get(int index) {
        if (this.root == null || index > this.count - 1 || index < 0)
            return null;
        this.foot = 0;
        return this.root.getNode(index);
    }

    // 修改鏈表數據
    public void set(int index, Object data) {
        if (this.root == null || index > this.count - 1 || index < 0)
            return;
        this.foot = 0;
        this.root.setNode(index, data);
    }

    // 刪除鏈表數據
    public void remove(Object data) {
        if (this.contains(data)) {
            if (data.equals(this.root.data))
                this.root = this.root.next;
            else 
                this.root.next.removeNode(this.root, data);
            this.count --;
        }
    }

    // 對象數組轉換
    public Object[] toArray() {
        if (this.root == null)
            return null;
        this.foot = 0;
        this.retArray = new Object[this.count];
        this.root.toArrayNode();
        return this.retArray;
    }

    public static void main(String[] args) {
        Link link = new Link();
        link.add("111");
        link.add("222");
        link.add("333");

        //System.out.println("size = " + link.size());
        //System.out.println("isEmpty = " + link.isEmpty());
        //System.out.println("contains = " + link.contains("111"));
        //System.out.println("contains = " + link.contains("444"));
        //System.out.println("get = " + link.get(0));
        //System.out.println("get = " + link.get(4));
        //link.set(0, "fuck");
        //System.out.println("get = " + link.get(0));

        //link.remove("111");
        //System.out.println("get = " + link.get(0));

        Object[] array = link.toArray();
        for (int x = 0 ; x < array.length ; x ++) {
            if (array[x] instanceof String) 
                System.out.println("x = " + x + "; value = " + array[x]);
        }   
    }

}

Factory.java

// 工廠設計模式
public class Factory {
    public static Fruit getInstance(String className) {
        if ("apple".equals(className))
            return new Apple();
        if ("orange".equals(className))
            return new Orange();
        return null;
    }

    public static void main(String[] args) {
        Fruit fruit = Factory.getInstance("apple");
        fruit.eat();

        fruit = Factory.getInstance("orange");
        fruit.eat();
    }
}

interface Fruit {
    public void eat();
}
class Apple implements Fruit {
    public void eat() {
        System.out.println("apple ... eat");
    }
}

class Orange implements Fruit {
    public void eat() {
        System.out.println("orange ... eat");
    }
}

Proxy.java

// 代理設計模式
public class Proxy implements Subject {
    private Subject subject;
    public Proxy(Subject subject) {
        this.subject = subject;
    }
    private void prepare() {
        System.out.println("Proxy ... prepare");
    }
    public void make() {
        this.prepare();
        this.subject.make();
        this.destroy();
    }
    private void destroy() {
        System.out.println("Proxy ... destroy");
    }
    public static void main(String[] args) {
        Subject subject = new Proxy(new RealSubject());
        subject.make();
    }
}

interface Subject {
    public void make();
}

class RealSubject implements Subject {
    public void make() {
        System.out.println("RealSubject ... make");
    }
}

Student.java 的equals

// equals方法
public class Student {
    private String name;
    private int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public boolean equals(Object object) {
        if (this == object)
            return true;
        if (object == null)
            return false;
        if (!(object instanceof Student))
            return false;
        Student s = (Student) object;
        if (this.name.equals(s.name) && this.age == s.age)
            return true;
        return false;
    }

    public static void main(String[] args) {
        Student s1 = new Student("zhangsan", 10);
        Student s2 = new Student("lisi", 10);
        System.out.println(s1.equals(s2));
    }
}

Main.java

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/*
 * 二叉樹
 * 增加  public String insert(int value)
 * 查找 public Node find(int value)
 * 八種遍歷方式: 
 *  前序遞歸遍歷 public void preOrderTraverse()
 *  前序棧遍歷     public void preOrderByStack()
 *  中序遞歸遍歷 public void inOrderTraverse()
 *  中序棧遍歷     public void inOrderByStack()
 *  後序遞歸遍歷 public void postOrderTraverse()
 *  後序棧遍歷     public void postOrderByStack()
 *  廣度遍歷         public void gdOrder()
 *  深度遍歷:迭代public void sdOrder()
 * 獲取樹的深度 public int getHeight()
 * 獲取樹節點的個數 public int size()
 * 刪除節點 public boolean remove(int value)
 * 
 */
class Node {
    int value;
    Node left;
    Node right;
    Node (int value) {
        this.value = value;
    }

    public void display() {
        System.out.println(this.value);
    }

    @Override
    public String toString() {
        return String.valueOf(this.value);
    }
}

class Binary {
    private Node root;
    Binary(int value) {
        this.root = new Node(value);
        this.root.left = null;
        this.root.right = null;
    }

    // insert
    public String insert(int value) {
        String error = null;
        Node node = new Node(value);
        Node current = this.root;
        Node parent = null;
        while (true) {
            if (value == current.value) {
                error = "The Binary Tree Has The Same Value;";
                break;
            } else if (value < current.value) {
                parent = current;
                current = current.left;
                if (current == null) {
                    parent.left = node;
                    break;
                }
            } else if (value > current.value) {
                parent = current;
                current = current.right;
                if (current == null) {
                    parent.right = node;
                    break;
                }
            }
        }
        return error;
    }

    // find
    public Node find(int value) {
        Node current = this.root;
        while (current != null) {
            if (value == current.value) return current;
            else if (value < current.value) current = current.left;
            else current = current.right;
        }
        return null;
    }

    // Eight Order
    public void preOrderTraverse() {
        System.out.println("前序遞歸遍歷");
        preOrderTraverse(this.root);
        System.out.println();
    }
    private void preOrderTraverse(Node node) {
        if (node == null) return ;
        node.display();
        preOrderTraverse(node.left);
        preOrderTraverse(node.right);
    }

    public void preOrderByStack() {
        System.out.println("前序棧遍歷");
        Stack<Node> stack = new Stack<Node>();
        Node current = this.root;
        while (current != null || !stack.isEmpty()) {
            while (current != null) {
                current.display();
                stack.push(current);
                current = current.left;
            }

            if (!stack.isEmpty()) {
                current = stack.pop();
                current = current.right;
            }
        }
        System.out.println();
    }

    public void inOrderTraverse() {
        System.out.println("中序遞歸遍歷");
        inOrderTraverse(this.root);
        System.out.println();
    }
    private void inOrderTraverse(Node node) {
        if (node == null) return ;
        inOrderTraverse(node.left);
        node.display();
        inOrderTraverse(node.right);
    }

    public void inOrderByStack() {
        System.out.println("中序棧遍歷");
        Stack<Node> stack = new Stack<Node>();
        Node current = this.root;
        while (current != null || !stack.isEmpty()) {
            while (current != null) {
                stack.push(current);
                current = current.left;
            }

            if(!stack.isEmpty()) {
                current = stack.pop();
                current.display();
                current = current.right;
            }
        }
        System.out.println();
    }

    public void postOrderTraverse () {
        System.out.println("後序遞歸遍歷");
        postOrderTraverse(this.root);
        System.out.println();
    }
    private void postOrderTraverse(Node node) {
        if(node == null) return ;
        postOrderTraverse(node.left);
        postOrderTraverse(node.right);
        node.display();
    }

    public void postOrderByStack() {
        System.out.println("後序棧遍歷");
        Stack<Node> stack = new Stack<Node>();
        Node current = this.root;
        Node preNode = null;
        while (current != null || !stack.isEmpty()) {
            while (current != null) {
                stack.push(current);
                current = current.left;
            }

            if(!stack.isEmpty()) {
                current = stack.peek().right;
                if (current == null || preNode == current) {
                    current = stack.pop();
                    current.display();
                    preNode = current;
                    current = null;
                }
            }
        }

        System.out.println();
    }

    public void gdOrder() {
        System.out.println("廣度遍歷: 從上到下");
        LinkedList<Node> list = new LinkedList<Node>();
        list.offer(this.root);
        Node current = null;
        while (list.size() > 0) {
            current = list.poll();
            current.display();
            if (current.left != null) list.offer(current.left);
            if (current.right != null) list.offer(current.right);
        }
        System.out.println();
    }

    public void sdOrder() {
        System.out.println("深度遍歷: traverse");
        List<List<Node>> rst = new ArrayList<List<Node>>();
        List<Node> list = new ArrayList<Node>();
        sdOrder(this.root, rst, list);
        System.out.println(rst);
    }
    private void sdOrder(Node node, List<List<Node>> rst, List<Node> list) {
        if (node == null) return ;
        if (node.left == null && node.right == null) {
            list.add(node);
            rst.add(new ArrayList<>(list));
            list.remove(list.size() - 1);
        }
        list.add(node);
        sdOrder(node.left, rst, list);
        sdOrder(node.right, rst, list);
        list.remove(list.size() - 1);
    }

    public int getHeight() {
        System.out.println("樹的深度:");
        return getHeight(this.root);
    }
    private int getHeight(Node node) {
        if (node == null) return 0;
        int i = getHeight(node.left);
        int j = getHeight(node.right);
        return (i < j) ? (j + 1) : (i + 1);
    }

    public int size() {
        System.out.println("樹節點的個數:");
        return size(this.root);
    }
    private int size(Node node) {
        if(node == null) return 0;
        int i = size(node.left);
        int j = size(node.right);
        return i + j + 1;
    }

    public boolean remove(int value) {
        System.out.println("刪除:" + value);
        Node current = this.root;
        Node parent = null;
        boolean isLeft = true;
        while (true) {
            if (current == null) return false;
            else if (value == current.value) break;
            else if (value < current.value) {
                parent = current;
                current = current.left;
                isLeft = true;
            } else {
                parent = current;
                current = current.right;
                isLeft = false;
            }
        }

        // four condition
        // 1. The delNode has zero child
        if (current.left == null && current.right == null) {
            if (current == this.root) this.root = null;
            else {
                if (isLeft) parent.left = null;
                else parent.right = null;
            }
        }
        // 2. The delNode has one child, and is left Child
        else if (current.right == null) {
            if (current == this.root) this.root = current.left;
            else {
                if (isLeft) parent.left = current.left;
                else parent.right = current.left;
            }
        }
        // 3. The delNode has one child, and is right child
        else if (current.left == null) {
            if (current == this.root) this.root = current.right;
            else {
                if (isLeft) parent.left = current.right;
                else  parent.right = current.right;
            }
        }
        // 4. The delNode has two child , and must get successor node
        else {
            Node successor = getSuccessor(current);
            if (current == this.root) this.root = successor;
            else {
                if (isLeft) parent.left = successor;
                else parent.right = successor;
            }
        }

        System.out.println();
        return true;
    }

    private Node getSuccessor(Node delNode) {
        Node successor = delNode;
        Node parent = null;
        Node current = delNode.right;
        while (current != null) {
            parent = successor;
            successor = current;
            current = current.left;
        }

        if (successor != delNode.right) {
            parent.left = successor.right;
            successor.right = delNode.right;
        }
        successor.left = delNode.left;
        return successor;
    }
}

public class Main {
    public static void main(String[] args) {
        Binary btree = new Binary(10);
        btree.insert(5);
        btree.insert(15);
        btree.insert(0);
        btree.insert(9);
        btree.insert(11);
        btree.insert(20);
        //System.out.println(btree.insert(20));
//      System.out.println(btree.find(20));
//      System.out.println(btree.find(21));
//      btree.preOrderTraverse();
//      btree.preOrderByStack();
//      btree.inOrderTraverse();
//      btree.inOrderByStack();
//      btree.postOrderTraverse();
//      btree.postOrderByStack();
//      btree.gdOrder();
//      btree.sdOrder();
//      System.out.println(btree.getHeight());
//      System.out.println(btree.size());
//      btree.remove(20);
//      btree.remove(5);
        btree.remove(10);
        btree.preOrderTraverse();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章