首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

java深度算法题 求解,解决办法

2012-01-15 
java深度算法题 求解,急!有个这样的,比如说:1/ \23/ \/\45 6 7这样的结构,怎么用深度算法遍历出来啊!求代

java深度算法题 求解,急!
有个这样的,比如说:
  1
  / \
  2 3
  / \ /\
 4 5 6 7
这样的结构,怎么用深度算法遍历出来啊!求代码,哪位大虾帮忙啊!很急!

[解决办法]

Java code
public void preOrder(BNode root){        if(root == null) return;        Stack<BNode> stack = new Stack<BNode>();        for(BNode iter=root; !stack.empty() || iter!=null; ){            if(iter == null){                iter=stack.pop();            }            iter.visit();            if(iter.getRight() != null){                stack.push(iter.getRight());            }            iter=iter.getLeft();        }    }
[解决办法]
Java code
package org.zwj.tree;/** * 节点类 */class Node{    private int data;    private Node leftChild;    private Node rightChild;    public int getData()    {        return data;    }    public void setData(int data)    {        this.data = data;    }    public Node getLeftChild()    {        return leftChild;    }    public void setLeftChild(Node leftChild)    {        this.leftChild = leftChild;    }    public Node getRightChild()    {        return rightChild;    }    public void setRightChild(Node rightChild)    {        this.rightChild = rightChild;    }    public void addNode(Node node)    {        if (this.leftChild == null)        {            this.leftChild = node;        } else        {            if (this.rightChild == null)            {                if (this.leftChild.getData() < node.getData())                {                    this.rightChild = node;                }            } else            {                this.leftChild.addNode(node);            }        }    }    public void printNode()    {        if (this.leftChild != null)        {            System.out.print(this.leftChild.getData() + "\t");            if (this.rightChild != null)            {                System.out.print(this.rightChild.getData() + "\t");            }            this.leftChild.printNode();        }    }}/** * 一棵树,形状:(节点data<左孩子data<右孩子data) *  * @author zKF44321 *  */class Tree{    private Node root;    public Node getRoot()    {        return root;    }    public void setRoot(Node root)    {        this.root = root;    }    /**     * 添加节点     *      * @param data     */    public void add(int data)    {        Node newNode = new Node();        newNode.setData(data);        if (this.root == null)        {            this.root = newNode;        } else        {            if (this.root.getData() < newNode.getData())            {                this.root.addNode(newNode);            }        }    }    /**     * 打印树,从小到大     */    public void print()    {        if (this.root != null)        {            System.out.print(this.root.getData() + "\t");            this.root.printNode();        }    }}public class TestTree{    public static void main(String args[])    {        Tree tree = new Tree();        tree.add(1);        tree.add(2);        tree.add(3);        tree.add(4);        tree.add(5);        tree.add(6);        tree.add(7);        tree.print();    }} 

热点排行