欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
java實(shí)現二叉樹(shù)啟遍歷的算法
java實(shí)現二叉樹(shù)啟遍歷的算法
 
public class TreeNode1 { //二叉樹(shù)的結點(diǎn)類(lèi)
public String data; //數據元數
public TreeNode1 left,right; //指向左,右孩子結點(diǎn)的鏈

public TreeNode1(){
this("?");
}

public TreeNode1(String d){ //構造有值結點(diǎn)
data = d;
left = right = null;
}

public void preorder(TreeNode1 p){ //先根次序遍歷二叉樹(shù)
if(p!=null){
System.out.print(p.data+" ");
preorder(p.left);
preorder(p.right);
}
}

public void inorder(TreeNode1 p){ //中根次序遍歷二叉樹(shù)
if(p!=null){
inorder(p.left);
System.out.print(p.data+" ");
inorder(p.right);
}
}

public void postorder(TreeNode1 p){ //后根次序遍歷二叉樹(shù)
if(p!=null){
postorder(p.left);
postorder(p.right);
System.out.print(p.data+" ");
}
}
}
 
下面來(lái)個(gè)更為完整詳細的:
public class BinaryNode {
Object element;
BinaryNode left;
BinaryNode right;

}


import java.util.*;

public class Queue {

protected LinkedList list;

// Postcondition: this Queue object has been initialized.
public Queue() {

list = new LinkedList();

} // default constructor

// Postcondition: the number of elements in this Queue object has been
// returned.
public int size() {

return list.size();

} // method size

// Postcondition: true has been returned if this Queue object has no
// elements. Otherwise, false has been returned.
public boolean isEmpty() {

return list.isEmpty();

} // method isEmpty

// Postconditon: A copy of element has been inserted at the back of this
// Queue object. The averageTime (n) is constant and
// worstTime (n) is O (n).
public void enqueue(Object element) {

list.addLast(element);

} // method enqueue

// Precondition: this Queue object is not empty. Otherwise,
// NoSuchElementException will be thrown.
// Postcondition: The element that was at the front of this Queue object -
// just before this method was called -- has been removed
// from this Queue object and returned.
public Object dequeue() {

return list.removeFirst();

} // method dequeue

// Precondition: this Queue object is not empty. Otherwise,
// NoSuchElementException will be thrown.
// Postcondition: the element at index 0 in this Queue object has been
// returned.
public Object front() {

return list.getFirst();

} // method front

} // Queue class


import java.io.IOException;


public class BinaryTree {
BinaryNode root;


public BinaryTree() {
super();
// TODO 自動(dòng)生成構造函數存根
root=this.createPre();
}

public BinaryNode createPre()
//按照先序遍歷的輸入方法,建立二叉樹(shù)
{
BinaryNode t=null;
char ch;
try {
ch = (char)System.in.read();

if(ch==‘ ‘)
t=null;
else
{
t=new BinaryNode();
t.element=(Object)ch;
t.left=createPre();
t.right=createPre();
}
} catch (IOException e) {
// TODO 自動(dòng)生成 catch 塊
e.printStackTrace();
}
return t;
}

public void inOrder()
{
this.inOrder(root);
}

public void inOrder(BinaryNode t)
//中序遍歷二叉樹(shù)
{
if(t!=null)
{
inOrder(t.left);
System.out.print(t.element);
inOrder(t.right);
}
}

public void postOrder()
{
this.postOrder(root);
}

public void postOrder(BinaryNode t)
//后序遍歷二叉樹(shù)
{
if(t!=null)
{
postOrder(t.left);
System.out.print(t.element);
postOrder(t.right);
}
}

public void preOrder()
{
this.preOrder(root);
}
public void preOrder(BinaryNode t)
//前序遍歷二叉樹(shù)
{
if(t!=null)
{
System.out.print(t.element);
preOrder(t.left);
preOrder(t.right);
}
}

public void breadthFirst()
{
Queue treeQueue=new Queue();
BinaryNode p;
if(root!=null)
treeQueue.enqueue(root);
while(!treeQueue.isEmpty())
{
System.out.print(((BinaryNode)(treeQueue.front())).element);
p=(BinaryNode)treeQueue.dequeue();
if(p.left!=null)
treeQueue.enqueue(p.left);
if(p.right!=null)
treeQueue.enqueue(p.right);
}
}
}


public class BinaryTreeTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動(dòng)生成方法存根
BinaryTree tree = new BinaryTree();

System.out.println("先序遍歷:");
tree.preOrder();
System.out.println();

System.out.println("中序遍歷:");
tree.inOrder();
System.out.println();

System.out.println("后序遍歷:");
tree.postOrder();
System.out.println();

System.out.println("層次遍歷:");
tree.breadthFirst();
System.out.println();
}

}
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Java 創(chuàng )建二叉樹(shù)并遍歷
java二叉樹(shù)
Python數據結構與算法-二叉樹(shù)的遍歷
非遞歸求二叉樹(shù)的前序、中序和后序遍歷
數據結構_二叉樹(shù)的遍歷_課程設計
二叉樹(shù)的建立、遍歷、打印
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久