【DataStructure】Implemantation of Binary Tree

來源:互聯網
上載者:User

標籤:datastructure   java   

Statements: This blog was written by me, but most of content  is quoted from book【Data Structure with Java Hubbard】 

Here is a class for binary trees that directly implements the recursive definition. By extending the AbstractCollectionclass, it remains consistent with the Java Collections Framework.

【Implementation】

package com.albertshao.ds.tree;//  Data Structures with Java, Second Edition//  by John R. Hubbard//  Copyright 2007 by McGraw-Hillimport java.util.*;public class BinaryTree<E> extends AbstractCollection {  protected E root;  protected BinaryTree<E> left, right, parent;  protected int size;    public BinaryTree() {  }    public BinaryTree(E root) {    this.root = root;    size = 1;  }    public BinaryTree(E root, BinaryTree<E> left, BinaryTree<E> right) {    this(root);    if (left != null) {      this.left = left;      left.parent = this;      size += left.size();    }    if (right != null) {      this.right = right;      right.parent = this;      size += right.size();    }  }  public boolean equals(Object object) {    if (object == this) {      return true;    } else if (!(object instanceof BinaryTree)) {      return false;    }    BinaryTree that = (BinaryTree)object;    return that.root.equals(this.root)        && that.left.equals(this.left)        && that.right.equals(this.right)        && that.parent.equals(this.parent)        && that.size == this.size;  }  public int hashCode() {    return root.hashCode() + left.hashCode() + right.hashCode() + size;  }    public int size() {    return size;  }  public Iterator iterator() {    return new java.util.Iterator() { // anonymous inner class      private boolean rootDone;      private Iterator lIt, rIt;  // child iterators      public boolean hasNext() {        return !rootDone || lIt != null && lIt.hasNext()                         || rIt != null && rIt.hasNext();      }      public Object next() {        if (rootDone) {          if (lIt != null && lIt.hasNext()) {            return lIt.next();          }          if (rIt != null && rIt.hasNext()) {            return rIt.next();          }          return null;        }        if (left != null) {          lIt = left.iterator();        }        if (right != null) {          rIt = right.iterator();        }        rootDone = true;        return root;      }      public void remove() {        throw new UnsupportedOperationException();       }    };  }}

【Test】

package com.albertshao.ds.tree;//  Data Structures with Java, Second Edition//  by John R. Hubbard//  Copyright 2007 by McGraw-Hillpublic class TestBinaryTree {  static public void main(String[] args) {    BinaryTree<String> e = new BinaryTree<String>("E");    BinaryTree<String> g = new BinaryTree<String>("G");    BinaryTree<String> h = new BinaryTree<String>("H");    BinaryTree<String> i = new BinaryTree<String>("I");    BinaryTree<String> d = new BinaryTree<String>("D", null, g);    BinaryTree<String> f = new BinaryTree<String>("F", h, i);    BinaryTree<String> b = new BinaryTree<String>("B", d, e);    BinaryTree<String> c = new BinaryTree<String>("C", f, null);    BinaryTree<String> tree = new BinaryTree<String>("A", b, c);    System.out.printf("tree: %s", tree);  }}

【Result】
tree: [A, B, D, G, E, C, F, H, I]

The example shows two views of the same tree. The larger view shows all the details, representing each
object reference with an arrow.





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.