java二叉判定树代码 java判断二叉树是否为完全二叉树

利用JAVA 先序建立二叉树 #表示空树 。例如输入ABC##DE#G##F### 先序遍历输出ABCDEGF 我写的程序不能输出你的程序有诸多问题,你的程序运行时候应该也会报错的吧?
这个写法不是很通用,不过我还是按照你的源码修改成了你想要的结果 。
结构上基本一致,可实现基本已经面目全非了 。
我用字符串代替了手工输入,你要是喜欢手工输入 , 你可以把我那个注释掉,用你自己的……
以下是修改后可用的代码:
import java.util.*;
class Node {
Node left;
Node Right;
char data;// 节点数据
void print() {
System.out.println(data"");
}
public Node() {
this.left = null;
this.Right = null;
}
public Node(char data) {
this.left = null;
this.Right = null;
this.data = https://www.04ip.com/post/data;
}
}
class BTree {
static Node root = new Node();// 为根节点分配空间
static char ch[];// 输入的字符串
static int i = 0;
static Node CreateTree()// 先序建立二叉树
{
Node node = null;
if (ch[i] == '#') {
node = null;
i;
}else {
node = new Node();
node.data = https://www.04ip.com/post/ch[i];
i;
node.left = CreateTree();
node.Right = CreateTree();
}
return node;
}
static public void preorder(Node node)// 先序遍历二叉树
{
if (node != null) {
node.print();
preorder(node.left);
preorder(node.Right);
} else {
System.out.println("Tree node is empty");
}
}
}
public class Tree {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
BTree.ch = new char[16];
BTree.ch[0] = 'a';
// 读取输入字符数组,以*结尾
BTree.ch = "ABC##DE#G##F###".toCharArray();
//for (int i = 0; (BTree.ch[i] = reader.next().charAt(0)) != '*'; i){}
BTree.root = BTree.CreateTree();
BTree.preorder(BTree.root);
}
}
二叉树的遍历的完整代码是什么二叉树遍历代码
#include"iostream.h"
#include"stdlib.h"
#include"stdio.h"
#includestack
using namespace std;
#define NULL 0
#define OK 1
#define OVERFLOW -1
typedef int Status;
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}*bitree;
【java二叉判定树代码 java判断二叉树是否为完全二叉树】int k=0;
int depth(bitree T)//树的高度
{
if(!T)return 0;
else
{
int m=depth(T-lchild); int n=depth(T-rchild); return (mn?m:n) 1;
}
}
//先序,中序 建树
struct node *create(char *pre,char *ord,int n) {
struct node * T;
int m;
T=NULL;
if(n=0)
{
return NULL;
}
else
{
m=0;
T=new(struct node);
T-data=https://www.04ip.com/post/*pre;
T-lchild=T-rchild=NULL; while(ord[m]!=*pre)
m;
T-lchild=create(pre 1,ord,m);
T-rchild=create (pre m 1,ord m 1,n-m-1); return T;
}
}
//中序递归遍历
void inorder(struct node *T)
{
if(!T)
return;
else
{
inorder(T-lchild );
coutT-data;
inorder(T-rchild );
}
}
void inpre(struct node *T)
{
if(!T)
return;
else
{
coutT-data;
inpre(T-lchild );
inpre(T-rchild );
}
}
void postorder(struct node *T)
{
if(!T)
return;
else
{
postorder (T-lchild );
postorder (T-rchild );
coutT-data;
}
}
//先序非递归遍历
void inpre1(struct node *T)
第2/4页
{
struct node *p;
struct node *stack[20];
int top=0;
p=T;
cout"非递归先序为:"endl;
while(p||top!=0)
{
while (p)
{
stack[top]=p;
coutp-data;
p=p-lchild;
}
p=stack[--top];
p=p-rchild ;
}
}
//中序非递归遍历
void inorder1(struct node *T)
{
struct node *p;
struct node *stack[20];
int top=0;
p=T;
cout"非递归中序为:"endl;
while(p||top!=0)
{
while (p)
{
stack[top]=p;
p=p-lchild ;
}
p=stack[--top];
coutp-data;
p=p-rchild ;
}
}
//主函数
int main()
{
bitree T;
char pre[30],ord[30];
第3/4页
int n,m;
cout"请输入先序为- a*b?ef的二叉树:"endl; gets(pre);
cout"请输入中序为a b*c%d-e/f的二叉树:"endl; gets(ord);
n=strlen(pre);
T=create(pre,ord,n);
cout "后序遍历为:"endl;
postorder (T);
coutendl;
inpre1(T);
coutendl;
inorder1(T);
coutendl;
m=depth(T);
cout"二叉树高度为:"mendl;
return 0;
}
Java数据结构二叉树深度递归调用算法求内部算法过程详解二叉树
1
23
45 67
这个二叉树的深度是3,树的深度是最大结点所在的层,这里是3.
应该计算所有结点层数,选择最大的那个 。
根据上面的二叉树代码,递归过程是:
f(1)=f(2) 1f(3)1 ? f(2)1 : f(3)1
f(2) 跟f(3)计算类似上面,要计算左右结点,然后取大者
所以计算顺序是f(4.left) = 0, f(4.right) = 0
f(4) = f(4.right)1 = 1
然后计算f(5.left) = 0,f(5.right) = 0
f(5) = f(5.right)1 =1
f(2) = f(5)1 =2
f(1.left) 计算完毕 , 计算f(1.right) f(3) 跟计算f(2)的过程一样 。
得到f(3) = f(7)1 = 2
f(1) = f(3)1 =3
if(depleftdepright){
return depleft 1;
}else{
return depright 1;
}
只有left大于right的时候采取left1,相等是取right
关于java二叉判定树代码和java判断二叉树是否为完全二叉树的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读