十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
你說的是二叉樹吧·····
創(chuàng)新互聯(lián)公司是一家從事企業(yè)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、行業(yè)門戶網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計制作的專業(yè)網(wǎng)站建設(shè)公司,擁有經(jīng)驗豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁設(shè)計人員,具備各種規(guī)模與類型網(wǎng)站建設(shè)的實力,在網(wǎng)站建設(shè)領(lǐng)域樹立了自己獨(dú)特的設(shè)計風(fēng)格。自公司成立以來曾獨(dú)立設(shè)計制作的站點(diǎn)上1000家。
/**
* 二叉樹測試二叉樹順序存儲在treeLine中,遞歸前序創(chuàng)建二叉樹。另外還有能
* 夠前序、中序、后序、按層遍歷二叉樹的方法以及一個返回遍歷結(jié)果asString的
* 方法。
*/
public class BitTree {
public static Node2 root;
public static String asString;
//事先存入的數(shù)組,符號#表示二叉樹結(jié)束。
public static final char[] treeLine = {'a','b','c','d','e','f','g',' ',' ','j',' ',' ','i','#'};
//用于標(biāo)志二叉樹節(jié)點(diǎn)在數(shù)組中的存儲位置,以便在創(chuàng)建二叉樹時能夠找到節(jié)點(diǎn)對應(yīng)的數(shù)據(jù)。
static int index;
//構(gòu)造函數(shù)
public BitTree() {
System.out.print("測試二叉樹的順序表示為:");
System.out.println(treeLine);
this.index = 0;
root = this.setup(root);
}
//創(chuàng)建二叉樹的遞歸程序
private Node2 setup(Node2 current) {
if (index = treeLine.length) return current;
if (treeLine[index] == '#') return current;
if (treeLine[index] == ' ') return current;
current = new Node2(treeLine[index]);
index = index * 2 + 1;
current.left = setup(current.left);
index ++;
current.right = setup(current.right);
index = index / 2 - 1;
return current;
}
//二叉樹是否為空。
public boolean isEmpty() {
if (root == null) return true;
return false;
}
//返回遍歷二叉樹所得到的字符串。
public String toString(int type) {
if (type == 0) {
asString = "前序遍歷:\t";
this.front(root);
}
if (type == 1) {
asString = "中序遍歷:\t";
this.middle(root);
}
if (type == 2) {
asString = "后序遍歷:\t";
this.rear(root);
}
if (type == 3) {
asString = "按層遍歷:\t";
this.level(root);
}
return asString;
}
//前序遍歷二叉樹的循環(huán)算法,每到一個結(jié)點(diǎn)先輸出,再壓棧,然后訪問它的左子樹,
//出棧,訪問其右子樹,然后該次循環(huán)結(jié)束。
private void front(Node2 current) {
StackL stack = new StackL((Object)current);
do {
if (current == null) {
current = (Node2)stack.pop();
current = current.right;
} else {
asString += current.ch;
current = current.left;
}
if (!(current == null)) stack.push((Object)current);
} while (!(stack.isEmpty()));
}
//中序遍歷二叉樹
private void middle(Node2 current) {
if (current == null) return;
middle(current.left);
asString += current.ch;
middle(current.right);
}
//后序遍歷二叉樹的遞歸算法
private void rear(Node2 current) {
if (current == null) return;
rear(current.left);
rear(current.right);
asString += current.ch;
}
}
/**
* 二叉樹所使用的節(jié)點(diǎn)類。包括一個值域兩個鏈域
*/
public class Node2 {
char ch;
Node2 left;
Node2 right;
//構(gòu)造函數(shù)
public Node2(char c) {
this.ch = c;
this.left = null;
this.right = null;
}
//設(shè)置節(jié)點(diǎn)的值
public void setChar(char c) {
this.ch = c;
}
//返回節(jié)點(diǎn)的值
public char getChar() {
return ch;
}
//設(shè)置節(jié)點(diǎn)的左孩子
public void setLeft(Node2 left) {
this.left = left;
}
//設(shè)置節(jié)點(diǎn)的右孩子
public void setRight (Node2 right) {
this.right = right;
}
//如果是葉節(jié)點(diǎn)返回true
public boolean isLeaf() {
if ((this.left == null) (this.right == null)) return true;
return false;
}
}
一個作業(yè)題,里面有你要的東西。
主函數(shù)自己寫吧。當(dāng)然其它地方也有要改的。
import java.util.*;
public class Size{
public static void main(String[] args)throws Exception{
Scanner s= new Scanner(System.in);
System.out.print("請輸入第一個數(shù):");
float a=s.nextFloat();
System.out.print("請輸入第二個數(shù):");
float b=s.nextFloat();
System.out.println("兩個數(shù)的和是:"+(a+b));
System.out.println("兩個數(shù)的差是:"+(a-b));
System.out.println("兩個數(shù)的積是:"+(a*b));
System.out.println("兩個數(shù)的商是:"+(a/b));
}
}
這個是輸出菱形的
import java.util.*;
public class lingxing
{
public static void main(String []args)
{
int i,j,k,x,n;
System.out.println("請輸入你定義的數(shù)值");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
for(i=0;i=n-1;i++)
{
for(j=0;j=n-i-1;j++)
{
System.out.print(" ");
}
for(k=0;k=2*i;k++)
{
System.out.print("*");
}
System.out.print("\n");
}
for(i=0;i=n-1;i++)
{
for(j=0;j=i+1;j++)
{
System.out.print(" ");
}
for(k=0;k=2*(n-i-2);k++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
}
package com.wdy.reg;
public class Calculator {
private int a;
private int b;
public Calculator(int a,int b){
this.a=a;
this.b=b;
}
public int add(){
return a+b;
}
public int sub(){
return a-b;
}
public int mul(){
return a*b;
}
public int div(){
return a/b;
}
public static void main(String[] args) {
Calculator cal=new Calculator(12, 4);
System.out.println(cal.add());
}
}