Java對二叉搜尋樹進行插入、尋找、遍曆、最大值和最小值的操作

來源:互聯網
上載者:User

標籤:二叉搜尋樹

1、首先,需要一個節點對象的類。這些對象包含資料,資料代表格儲存體的內容,而且還有指向節點的兩個子節點的引用

class Node {public int iData;public double dData;public Node leftChild;public Node rightChild;public void displayNode() {System.out.print("{");System.out.print(iData);System.out.print(",");System.out.print(dData);System.out.print("}");}}

2、插入一個節點

從根開始尋找一個相應的節點,它將是新節點的父節點。當父節點找到了,新節點就可以串連到它的左子節點或右子節點處,這取決於新節點的值是比父節點的值大還是小。

下面是insert()方法代碼:

public void insert(int id, double dd) {Node newNode = new Node();newNode.iData = id;newNode.dData = dd;if(root == null)root = newNode;else {Node current = root;Node parent;while(true) {parent = current;if(id < current.iData) {current = current.leftChild;if(current == null) {parent.leftChild = newNode;return;}} else {current = current.rightChild;if(current == null) {parent.rightChild = newNode;return;}}} // end while} // end else}

這裡用一個新的變數parent(current的父節點),來儲存遇到的最後一個不是null的節點。必須這樣做,因為current在尋找的過程中會變成null,才能發現它查過的上一個節點沒有一個對應的子節點。如果不儲存parent,就會失去插入新節點的位置。


3、尋找一個節點

public Node find(int key) {// 假設樹非空Node current = root;while(current.iData != key) {if(key < current.iData)current = current.leftChild;elsecurrent = current.rightChild;if(current == null)return null;}return current;}

找到節點:如果while迴圈不滿足條件,從迴圈的末端退出來,current的iData欄位和key相等,這就找到了該節點。

找不到節點:如果current等於null,在尋找序列中找不到下一個子節點,到達序列的末端而沒有找到要找的節點,表明了它不存在,返回nulll來指出這個情況。


4、遍曆樹(前序走訪,中序遍曆,後序遍曆)

/** * 前序走訪 * @param localRoot */public void preOrder(Node localRoot) {if(localRoot != null) {System.out.print(localRoot.iData+" ");preOrder(localRoot.leftChild);preOrder(localRoot.rightChild);}}/** * 中序遍曆 * @param localRoot */public void inOrder(Node localRoot) {if(localRoot != null) {preOrder(localRoot.leftChild);System.out.print(localRoot.iData+" ");preOrder(localRoot.rightChild);}}/** * 後序遍曆 * @param localRoot */public void postOrder(Node localRoot) {if(localRoot != null) {preOrder(localRoot.leftChild);preOrder(localRoot.rightChild);System.out.print(localRoot.iData+" ");}}

遍曆樹的最簡單方法使用遞迴的方法。用遞迴的方法遍曆整棵樹要用一個節點作為參數。初始化這個節點是根。例如中序遍曆只需要做三件事:

1)、調用自身來遍曆節點的左子樹

2)、訪問這個節點

3)、調用自身來遍曆節點的右子樹。


5、尋找最大值和最小值

/** * 求樹中的最小值 * @return */public Node minimum() {Node current;current = root;Node last = null;while(current != null) {last = current;current = current.leftChild;}return last;}/** * 求樹中的最大值 * @return */public Node maxmum() {Node current;current = root;Node last = null;while(current != null) {last = current;current = current.rightChild;}return last;}


以下是完整測試代碼:

package binTree;class Node {public int iData;public double dData;public Node leftChild;public Node rightChild;public void displayNode() {System.out.print("{");System.out.print(iData);System.out.print(",");System.out.print(dData);System.out.print("}");}}class Tree {private Node root;public Tree() {root = null;}/** * 尋找節點 * @param key * @return */public Node find(int key) {// 假設樹非空Node current = root;while(current.iData != key) {if(key < current.iData)current = current.leftChild;elsecurrent = current.rightChild;if(current == null)return null;}return current;}/** * 插入節點 * @param id * @param dd */public void insert(int id, double dd) {Node newNode = new Node();newNode.iData = id;newNode.dData = dd;if(root == null)root = newNode;else {Node current = root;Node parent;while(true) {parent = current;if(id < current.iData) {current = current.leftChild;if(current == null) {parent.leftChild = newNode;return;}} else {current = current.rightChild;if(current == null) {parent.rightChild = newNode;return;}}} // end while} // end else}/** * 前序走訪 * @param localRoot */public void preOrder(Node localRoot) {if(localRoot != null) {System.out.print(localRoot.iData+" ");preOrder(localRoot.leftChild);preOrder(localRoot.rightChild);}}/** * 中序遍曆 * @param localRoot */public void inOrder(Node localRoot) {if(localRoot != null) {preOrder(localRoot.leftChild);System.out.print(localRoot.iData+" ");preOrder(localRoot.rightChild);}}/** * 後序遍曆 * @param localRoot */public void postOrder(Node localRoot) {if(localRoot != null) {preOrder(localRoot.leftChild);preOrder(localRoot.rightChild);System.out.print(localRoot.iData+" ");}}/** * 求樹中的最小值 * @return */public Node minimum() {Node current;current = root;Node last = null;while(current != null) {last = current;current = current.leftChild;}return last;}/** * 求樹中的最大值 * @return */public Node maxmum() {Node current;current = root;Node last = null;while(current != null) {last = current;current = current.rightChild;}return last;}}public class TreeApp {public static void main(String[] args) {Tree theTree = new Tree();/** *                50 *               /   *              25  75 *             / \     *            12 37   87 *               / \     *              30 43   93 *               \         *                33      97 */theTree.insert(50, 1.5);theTree.insert(25, 1.2);theTree.insert(75, 1.7);theTree.insert(12, 1.5);theTree.insert(37, 1.2);theTree.insert(43, 1.7);theTree.insert(30, 1.5);theTree.insert(33, 1.2);theTree.insert(87, 1.7);theTree.insert(93, 1.5);theTree.insert(97, 1.5);System.out.println("插入完畢~");//找到root節點Node nodeRoot = theTree.find(50);// 中序遍曆theTree.inOrder(nodeRoot);System.out.println();// 求最小值System.out.println("mini:"+ theTree.minimum().iData);// 求最大值System.out.println("max:"+ theTree.maxmum().iData);}}


Java對二叉搜尋樹進行插入、尋找、遍曆、最大值和最小值的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.