java中先序创建一棵树_JAVA创建二叉树将数组中的数依次存入二叉树,并分别用先序,后序,中序遍历; | 学步园...

用JAVA创建二叉树将数组中的数依次存入二叉树,若数值为负数或零则对应的节点为空

定义二叉树的结构

public class Bitree {

int data;

Bitree left;

Bitree right;

int i;      //用于记录此节点的值在数组中存放的位置;

public Bitree(){

}

public Bitree(int i){

this.i = i;

}

public Bitree(int data,Bitree l,Bitree r){

this.data =data;

this.left =l;

this.right =r;

}

public void Print(){

System.out.print(this.data+",");

}

}

接口

public interface VisitTreeInterface {

public Bitree Creatroot(Bitree node);//创建根节点

public Bitree Creatleft(Bitree node);//插入左节点

public Bitree Creatright(Bitree node);//插入右节点

public void PreOrder(Bitree  node);//先序遍历二叉树

public void LastOrder(Bitree node);//后序遍历

public void MidOrder(Bitree node);//中序遍历

}

实现接口的类

public class Creattree implements VisitTreeInterface {

int[] b;

public Creattree(int[] a){

this.b=a;

}

public Bitree Creatroot(Bitree node){      //创建根节点

if(node.i

if(b[node.i]<=0)

node=null;

else if(node!=null){

node.data=b[node.i];

System.out.println(node.i+" "+node.data);

node.left=Creatleft(node);

node.right=Creatright(node);

}

}

return node;

}

public Bitree Creatleft(Bitree node){        //左节点

if(node!=null)

{

node.left=new Bitree();

node.left.i=2*node.i+1;          // 根节点在数组中存放的位置为i 则左节点为2*i+1

Creatroot(node.left);            //调用递归;

}

return node.left;

}

public Bitree Creatright(Bitree node){       //右节点

if(node!=null)

{

node.right=new Bitree();

node.right.i=2*node.i+2;

Creatroot(node.right);

}

return node.right;

}

public void PreOrder(Bitree node){

if(node!=null){

node.Print();

PreOrder(node.left );

PreOrder(node.right );

}

}

public void LastOrder(Bitree node){

if(node!=null){

LastOrder(node.left);

LastOrder(node.right);

node.Print();

}

}

public void MidOrder(Bitree node){

if(node!=null){

MidOrder(node.left);

node.Print();

MidOrder(node.right);

}

}

}

创建二叉树并遍历

public class Main {

public static void main(String[] args){

int[] b={1,4,5,2,-6,5,9,10};

Bitree node=new Bitree(0);

Bitree bt;

Creattree ct=new Creattree(b);

bt=ct.Creatroot(node);

ct.PreOrder(bt);

System.out.print("/n");

ct.LastOrder(bt);

System.out.print("/n");

ct.MidOrder(bt);

System.out.print("/n");

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C语言编写的代码,可以根据给定的先序遍历和中序遍历序列求得后序遍历序列: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 // 定义二叉树结点结构体 typedef struct TreeNode { char data; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 先序遍历序列和中序遍历序列构建二叉树 TreeNode* buildTree(char* preorder, char* inorder, int preorderStart, int preorderEnd, int inorderStart, int inorderEnd) { if (preorderStart > preorderEnd || inorderStart > inorderEnd) { return NULL; } // 构建根结点 char rootValue = preorder[preorderStart]; TreeNode* root = (TreeNode*) malloc(sizeof(TreeNode)); root->data = rootValue; root->left = NULL; root->right = NULL; // 在中序遍历序列中找到根结点的位置 int rootPosition = -1; for (int i = inorderStart; i <= inorderEnd; i++) { if (inorder[i] == rootValue) { rootPosition = i; break; } } // 根据中序遍历序列,划分左右子树,并递归构建左右子树 int leftSize = rootPosition - inorderStart; root->left = buildTree(preorder, inorder, preorderStart + 1, preorderStart + leftSize, inorderStart, rootPosition - 1); root->right = buildTree(preorder, inorder, preorderStart + leftSize + 1, preorderEnd, rootPosition + 1, inorderEnd); return root; } // 后序遍历二叉树 void postorderTraversal(TreeNode* root, char* postorder, int* index) { if (root == NULL) { return; } // 后序遍历左子树 postorderTraversal(root->left, postorder, index); // 后序遍历右子树 postorderTraversal(root->right, postorder, index); // 将当前结点的值存入后序遍历序列中 postorder[(*index)++] = root->data; } int main() { char preorder[MAX_SIZE] = "ABDECFG"; char inorder[MAX_SIZE] = "DBEAFCG"; char postorder[MAX_SIZE]; int index = 0; TreeNode* root = buildTree(preorder, inorder, 0, 6, 0, 6); postorderTraversal(root, postorder, &index); printf("后序遍历序列为:%s\n", postorder); return 0; } ``` 以上代码中,我们先定义了一个二叉树结点结构体,然后编写了一个函`buildTree`,根据先序遍历和中序遍历序列递归构建二叉树。接下来,我们编写了一个函`postorderTraversal`,递归后序遍历二叉树,并将遍历结果存入后序遍历序列中。最后,我们在主函中调

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值