• Java-数据结构之二叉树练习


    本来这个随笔应该在4月17号就应该发出来的。不巧的是,那天晚上收到了offer,然后接下去两天为入职到处跑,20号入职后一直忙,直到最近几天才有时间看看书。然而20多天前就看完的了二叉树,然后17号那天正在按照二叉树的定义自己写一个完整的二叉树。本来那天晚上就能完成的,这么一个打断,导致现在想接上去之前的思路继续写二叉树有点难了。所以二叉树里面的删除部分以及Iterator部分直接是看着书写的......

    下面是MyBinaryTree.java

    //MyBinaryTree.java
    package
    struct; import java.util.ArrayList; import java.util.Iterator; public class MyBinaryTree<E extends Comparable<E>> { private TreeNode<E> root; private int treeSize=0; public MyBinaryTree() {} public MyBinaryTree(E[] o) { for(int i=0;i<o.length;i++) { insert(o[i]); } } private class TreeNode<E extends Comparable<E>> { public E element; public TreeNode<E> leftChild; public TreeNode<E> rightChild; public TreeNode() {} public TreeNode(E o) { this.element = o; } } //寻找一个元素 public boolean search(E o) { TreeNode<E> current; if(root == null) return false; else { current=root; while(true) { if(current.element.compareTo(o) == 0) //相等 { return true; } else if(current.element.compareTo(o) > 0) //current.element大 { if(current.leftChild!=null) current = current.leftChild; else return false; } else if(current.element.compareTo(o) < 0) //o大 { if(current.rightChild!=null) current = current.rightChild; else return false; } else return false; } } } //插入一个元素 public boolean insert(E o) { TreeNode<E> current; if(root==null) { root = new TreeNode<E>(o); treeSize++; return true; } else { current=root; while(true) { if(current.element.compareTo(o) > 0) //current.element大 { if(current.leftChild!=null) current = current.leftChild; else { current.leftChild = new TreeNode<E>(o); treeSize++; return true; } } else if(current.element.compareTo(o) < 0) //o大 { if(current.rightChild!=null) current = current.rightChild; else { current.rightChild = new TreeNode<E>(o); treeSize++; return true; } } else { return false; } } } } //删除一个元素 public boolean delete(E o) { TreeNode<E> current=root; TreeNode<E> parent=null; //current==root==null返回false if(current==null) return false; //寻找一个相等的元素 while(current.element.compareTo(o)!=0) { if(current.element.compareTo(o) > 0) { if(current.leftChild == null) return false; else { parent = current; current = current.leftChild; } } else { if(current.rightChild == null) return false; else { parent = current; current = current.rightChild; } } } //找到一个相等的元素之后,左右子树均为空 if(current.leftChild==null && current.rightChild==null) { current = null; treeSize--; return true; } else if(current.leftChild==null)//左子树为空 { if(parent==null) root=current.rightChild; else { if(parent.element.compareTo(o)>0) parent.leftChild=current.rightChild; else parent.rightChild=current.rightChild; } treeSize--; return true; } else { //右子树为空,或者左右子树均不为空 TreeNode<E> parentOfBiggest = current; TreeNode<E> theBiggest = current.leftChild; while(theBiggest.rightChild!=null) { parentOfBiggest=theBiggest; theBiggest = theBiggest.rightChild; } current.element = theBiggest.element; if(parentOfBiggest.rightChild == theBiggest) parentOfBiggest.rightChild = theBiggest.leftChild; else parentOfBiggest.leftChild = theBiggest.leftChild; treeSize--; return true; } } //中序 public void inorder() { TreeNode<E> current; current = root; if(current!=null) { inorder(current.leftChild); System.out.print(current.element+" "); inorder(current.rightChild); System.out.println(); } } private void inorder(TreeNode<E> current) { if(current!=null) { inorder(current.leftChild); System.out.print(current.element+" "); inorder(current.rightChild); } } //前序 public void preorder() { TreeNode<E> current; current = root; if(current!=null) { System.out.print(current.element+" "); preorder(current.leftChild); preorder(current.rightChild); System.out.println(); } } private void preorder(TreeNode<E> current) { if(current!=null) { System.out.print(current.element+" "); preorder(current.leftChild); preorder(current.rightChild); } } //后序 public void postorder() { TreeNode<E> current; current = root; if(current!=null) { postorder(current.leftChild); postorder(current.rightChild); System.out.print(current.element+" "); System.out.println(); } } private void postorder(TreeNode<E> current) { if(current!=null) { postorder(current.leftChild); postorder(current.rightChild); System.out.print(current.element+" "); } } //get树的大小 public int getSize() { return treeSize; } //是否为空 public boolean isEmpty() { return treeSize==0; } //iterator public Iterator iterator() { return inorderIterator(); } //清空 public void clear() { root=null; treeSize=0; } public Iterator inorderIterator() { return new InorderIterator(); } class InorderIterator implements Iterator { private ArrayList<E> list = new ArrayList<E>(); private int current = 0; public InorderIterator() { inorder(); } private void inorder() { inorder(root); } private void inorder(TreeNode<E> root) { if(null == root) return; inorder(root.leftChild); list.add(root.element); inorder(root.rightChild); } @Override public boolean hasNext() { if(current < list.size()) return true; return false; } @Override public Object next() { return list.get(current++); } public void remove() { delete(list.get(current)); list.clear(); inorder(); } } }

    下面是MyBinaryTreeTest.java

    //MyBinaryTreeTest.java
    package
    struct; public class MyBinaryTreeTest { public static void main(String[] args) { MyBinaryTree<Integer> mbt = new MyBinaryTree<Integer>(); mbt.insert(12); mbt.insert(34); mbt.insert(6); mbt.insert(24); mbt.insert(4); mbt.insert(346); mbt.insert(40); mbt.insert(346); System.out.println("mbt size: "+mbt.getSize()); if(!mbt.search(346)) System.out.println("did not contains!"); else System.out.println("have this number!"); System.out.print("mbt inorder: "); mbt.inorder(); System.out.print("mbt preorder: "); mbt.preorder(); System.out.print("mbt postorder: "); mbt.postorder(); Integer[] a = new Integer[]{56,26,24,89,1,100,7,55,43,1}; MyBinaryTree<Integer> mbt2 = new MyBinaryTree<Integer>(a); System.out.print("mbt2 inorder: "); mbt2.inorder(); System.out.print("mbt2 preorder: "); mbt2.preorder(); System.out.print("mbt2 postorder: "); mbt2.postorder(); System.out.println("mbt2 size: "+mbt2.getSize()); mbt2.clear(); System.out.println("mbt2 size: "+mbt2.getSize()); if(!mbt2.search(1)) System.out.println("did not contains!"); else System.out.println("have this number!"); MyBinaryTree<Integer> mbt3 = new MyBinaryTree<Integer>(a); System.out.print("mbt3 inorder: "); mbt3.inorder(); System.out.print("mbt3 preorder: "); mbt3.preorder(); System.out.print("mbt3 postorder: "); mbt3.postorder(); System.out.println("before delete size:"+mbt3.getSize()); System.out.println(mbt3.delete(26)); System.out.println("after delete size:"+mbt3.getSize()); System.out.print("mbt3 inorder: "); mbt3.inorder(); System.out.print("mbt3 preorder: "); mbt3.preorder(); System.out.print("mbt3 postorder: "); mbt3.postorder(); System.out.println("before delete size:"+mbt3.getSize()); System.out.println(mbt3.delete(1888)); System.out.println("after delete size:"+mbt3.getSize()); } }

    最后是运行结果

    //result
    mbt size: 7 have this number! mbt inorder: 4 6 12 24 34 40 346 mbt preorder: 12 6 4 34 24 346 40 mbt postorder: 4 6 24 40 346 34 12 mbt2 inorder: 1 7 24 26 43 55 56 89 100 mbt2 preorder: 56 26 24 1 7 55 43 89 100 mbt2 postorder: 7 1 24 43 55 26 100 89 56 mbt2 size: 9 mbt2 size: 0 did not contains! mbt3 inorder: 1 7 24 26 43 55 56 89 100 mbt3 preorder: 56 26 24 1 7 55 43 89 100 mbt3 postorder: 7 1 24 43 55 26 100 89 56 before delete size:9 true after delete size:8 mbt3 inorder: 1 7 24 43 55 56 89 100 mbt3 preorder: 56 24 1 7 55 43 89 100 mbt3 postorder: 7 1 43 55 24 100 89 56 before delete size:8 false after delete size:8
  • 相关阅读:
    前端实现文件下载
    es6 Promise简单介绍
    es6开发环境搭建,babel 将es6转化成es5
    如何让写得html页面自动刷新
    es6箭头函数
    js对象跟数组多层嵌套,检测没有此数据就添加有则不添加以及超过限制条件删除操作
    小程序分享进入H5动态网页
    数据结构与算法--基本概念
    Unity中的常用输入
    Unity中的物体旋转
  • 原文地址:https://www.cnblogs.com/kumu/p/6838839.html
Copyright © 2020-2023  润新知