zoukankan      html  css  js  c++  java
  • 二叉树链式存储Java实现(未完待续)

    package com.wrh.lab.dataStructure.tree;

    /**
    * the node of the binary tree
    *
    @author wrh
    *
    */

    public class BinaryNode<E> {
    private E element; //the element
    private BinaryNode<E> left; //the left point
    private BinaryNode<E> right; //the right point

    /**
    * build an node
    *
    @param element
    *
    @param left
    *
    @param right
    */

    public BinaryNode(E element, BinaryNode<E> left, BinaryNode<E> right) {
    this.element = element;
    this.left = left;
    this.right = right;
    }

    public E getElement() {
    return element;
    }

    public void setElement(E element) {
    this.element = element;
    }

    public BinaryNode<E> getLeft() {
    return left;
    }

    public void setLeft(BinaryNode<E> left) {
    this.left = left;
    }

    public BinaryNode<E> getRight() {
    return right;
    }

    public void setRight(BinaryNode<E> right) {
    this.right = right;
    }

    public boolean isLeaf() {
    boolean isLeaf = false;
    if (null == left && null == right) {
    isLeaf = true;
    } else {
    isLeaf = false;
    }
    return isLeaf;
    }


    }
    package com.wrh.lab.dataStructure.tree;

    public interface LinkedBinaryTree<E> {

    /**
    * insert a node to the left child of the binary tree
    */

    public void insertToLeft(E element);

    /**
    * insert a node to the right child of the binary tree
    */

    public void insertToRight(E element);

    /**
    * preorder traversal the binary tree
    */

    public void preOrder(BinaryNode<E> root);

    /**
    * inorder traversal the binary tree
    */

    public void inOrder(BinaryNode<E> root);

    /**
    * postorder traversal the binary tree
    */

    public void postOrder(BinaryNode<E> root);
    }
    package com.wrh.lab.dataStructure.treeImpl;

    import com.wrh.lab.dataStructure.stackAndQueue.Stack;
    import com.wrh.lab.dataStructure.stackAndQueueImpl.LinkedStackImpl;
    import com.wrh.lab.dataStructure.tree.BinaryNode;
    import com.wrh.lab.dataStructure.tree.LinkedBinaryTree;

    public class LinkedBinaryTreeImpl<E> implements LinkedBinaryTree<E> {
    //private BinaryNode<E> left;
    //private BinaryNode<E> right;
    private BinaryNode<E> root; //the root node of the bianry tree
    private E element;


    public LinkedBinaryTreeImpl(E element) {
    root = new BinaryNode<E>(element, null, null);
    }

    public BinaryNode<String> create() {
    // create the tree by hands
    BinaryNode<String> root = new BinaryNode<String>("A", null, null);

    // LinkedBinaryTree<String> root = new
    // LinkedBinaryTreeImpl<String>("A");
    BinaryNode<String> b = new BinaryNode<String>("B", null, null);

    BinaryNode<String> c = new BinaryNode<String>("C", null, null);
    BinaryNode<String> d = new BinaryNode<String>("D", null, null);
    BinaryNode<String> e = new BinaryNode<String>("E", null, null);
    root.setLeft(b);
    root.setRight(c);
    b.setRight(d);
    c.setLeft(e);
    return root;
    }

    @Override
    public void insertToLeft(E element) {
    BinaryNode<E> left = new BinaryNode(element, null, null);
    root.setLeft(left);

    }

    @Override
    public void insertToRight(E element) {
    BinaryNode<E> right = new BinaryNode(element, null, null);
    root.setRight(right);
    }

    @Override
    public void preOrder(BinaryNode<E> root) {
    if(null != root) {
    System.out.println(root.getElement());
    preOrder(root.getLeft());
    preOrder(root.getRight());
    }


    }

    @Override
    public void inOrder(BinaryNode<E> root) {
    if (null != root) {
    inOrder(root.getLeft());
    System.out.println(root.getElement());
    inOrder(root.getRight());
    }

    }

    @Override
    public void postOrder(BinaryNode<E> root) {
    if (null != root) {
    postOrder(root.getLeft());
    postOrder(root.getRight());
    System.out.println(root.getElement());
    }
    }

    /**
    * nonrecursive inorder traverse the tree
    *
    @param t
    */

    public void inOrderTraverse(BinaryNode<E> t) {
    Stack<BinaryNode<E>> s = new LinkedStackImpl<BinaryNode<E>>();
    BinaryNode<E> p = t;
    s.push(p);
    while (!s.isEmpty()) {
    while (null != p){ //go to the end of the left
    s.push(p.getLeft());

    p = p.getLeft();
    }
    s.pop(); //null pointer pop
    if (!s.isEmpty()) {

    System.out.println(s.getTop().getElement());
    //System.out.println(s.pop().getElement());
    p = s.pop();

    p = p.getRight();
    s.push(p);
    }

    }
    t = null;
    p = null;
    s.clear();
    }

    }




    跟我走啊~~
  • 相关阅读:
    在PHP中如何获取来源URL
    函数中的static关键字
    ubuntu server 10.04 上安装oracle 10G 开发版
    ajax同步和异步提交的区别
    jQuery对Select的操作集合[终结篇]
    从IFARME中直接跳转到外层页面
    用mount命令挂载远程文件系统
    js小技巧(输入框提示信息自动消失)
    js 获取事件源
    Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
  • 原文地址:https://www.cnblogs.com/wrh526/p/2354638.html
Copyright © 2011-2022 走看看