`
zhaohaolin
  • 浏览: 984849 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Java数据结构和算法--树

阅读更多

(1)二叉树 

Java代码  收藏代码
  1. package ChapterEight;  
  2.   
  3. class Tree {  
  4.     class Node {  
  5.         public long value;  
  6.   
  7.         public Node leftChild;  
  8.   
  9.         public Node rightChild;  
  10.   
  11.         public Node(long value) {  
  12.             this.value = value;  
  13.             leftChild = null;  
  14.             rightChild = null;  
  15.         }  
  16.     }  
  17.   
  18.     public Node root;  
  19.   
  20.     public Tree() {  
  21.         root = null;  
  22.     }  
  23.   
  24.     // 向树中插入一个节点  
  25.     public void insert(long value) {  
  26.         Node newNode = new Node(value);  
  27.         // 树是空的  
  28.         if (root == null)  
  29.             root = newNode;  
  30.         else {  
  31.             Node current = root;  
  32.             Node parentNode;  
  33.             while (true) {  
  34.                 parentNode = current;  
  35.                 if (value < current.value) {  
  36.                     current = current.leftChild;  
  37.                     // 要插入的节点为左孩子节点  
  38.                     if (current == null) {  
  39.                         parentNode.leftChild = newNode;  
  40.                         return;  
  41.                     }  
  42.                 } else {  
  43.                     // 要插入的节点为右孩子节点  
  44.                     current = current.rightChild;  
  45.                     if (current == null) {  
  46.                         parentNode.rightChild = newNode;  
  47.                         return;  
  48.                     }  
  49.                 }  
  50.             }  
  51.         }  
  52.     }  
  53.   
  54.     // 先续遍历树中的所有节点  
  55.     public void preOrder(Node currentRoot) {  
  56.         if (currentRoot != null) {  
  57.             System.out.print(currentRoot.value + " ");  
  58.             preOrder(currentRoot.leftChild);  
  59.             preOrder(currentRoot.rightChild);  
  60.         }  
  61.     }  
  62.   
  63.     // 中续遍历树中的所有节点  
  64.     public void inOrder(Node currentNode) {  
  65.         if (currentNode != null) {  
  66.             inOrder(currentNode.leftChild);  
  67.             System.out.print(currentNode.value + " ");  
  68.             inOrder(currentNode.rightChild);  
  69.         }  
  70.     }  
  71.   
  72.     // 后续遍历树中的所有节点  
  73.     public void postOrder(Node currentNode) {  
  74.         if (currentNode != null) {  
  75.             postOrder(currentNode.leftChild);  
  76.             postOrder(currentNode.rightChild);  
  77.             System.out.print(currentNode.value + " ");  
  78.         }  
  79.     }  
  80.   
  81.     public void traverse(int traverseType) {  
  82.         switch (traverseType) {  
  83.         case 1:  
  84.             preOrder(root);  
  85.             break;  
  86.         case 2:  
  87.             inOrder(root);  
  88.             break;  
  89.         case 3:  
  90.             postOrder(root);  
  91.             break;  
  92.         default:  
  93.             break;  
  94.         }  
  95.     }  
  96.   
  97.     // 依据树节点的值删除树中的一个节点  
  98.     public boolean delete(int value) {  
  99.         // 遍历树过程中的当前节点  
  100.         Node current = root;  
  101.         // 要删除节点的父节点  
  102.         Node parent = root;  
  103.         // 记录树的节点为左孩子节点或右孩子节点  
  104.         boolean isLeftChild = true;  
  105.         while (current.value != value) {  
  106.             parent = current;  
  107.             // 要删除的节点在当前节点的左子树里  
  108.             if (value < current.value) {  
  109.                 isLeftChild = true;  
  110.                 current = current.leftChild;  
  111.             }  
  112.             // 要删除的节点在当前节点的右子树里  
  113.             else {  
  114.                 isLeftChild = false;  
  115.                 current = current.rightChild;  
  116.             }  
  117.             // 在树中没有找到要删除的节点  
  118.             if (current == null)  
  119.                 return false;  
  120.         }  
  121.         // 要删除的节点为叶子节点  
  122.         if (current.leftChild == null && current.rightChild == null) {  
  123.             // 要删除的节点为根节点  
  124.             if (current == root)  
  125.                 root = null;  
  126.             // 要删除的节点为左孩子节点  
  127.             else if (isLeftChild)  
  128.                 parent.leftChild = null;  
  129.             // 要删除的节点为右孩子节点  
  130.             else  
  131.                 parent.rightChild = null;  
  132.         }  
  133.         // 要删除的节点有左孩子节点,没有右孩子节点  
  134.         else if (current.rightChild == null) {  
  135.             // 要删除的节点为根节点  
  136. font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; bord
    分享到:
    评论

相关推荐

Global site tag (gtag.js) - Google Analytics