java根据层次遍历建立二叉树

节点类:

package Tree;

public class BTNode<E> {
	private E data;
	private BTNode<E> left, right;
	
	public BTNode(E initialData,BTNode<E> initialLeft,BTNode<E> initialRight){
		left=initialLeft;
		right=initialRight;
		data=initialData;
	}
	
	public E getData()
	{
		return data;
	}
	
	public BTNode<E> getLeft()
	{
		return left;
	}
	
	public E getLeftmostData()
	{
		if(left==null)
			return data;
		else 
			return left.getLeftmostData();
	}
	
	public BTNode<E> getRight()
	{
		return right;
	}
	
	public E getRightmostData()
	{
		if(right==null)
			return data;
		else 
			return right.getRightmostData();
	}
	
	//中序序遍历
	public void inorderPrint()
	{
		if(left!=null)
			left.inorderPrint();
		System.out.println(data);
		if(right!=null)
			right.inorderPrint();
	}
	
	//中序 遍历
	public void postorderPrint()
	{
		if(left!=null)
			left.postorderPrint();
		if(right!=null)
			right.postorderPrint();
		System.out.println(data);
	}
	
	//前序遍历
	public void preorderPrint()
	{
		System.out.println(data);
		if(left!=null)
			left.preorderPrint();
		if(right!=null)
			right.preorderPrint();
	}
	
	public boolean isLeaf()
	{
		return(left==null)&&(right==null);
	}
	
	public void print(int depth)
	{
		int i;
		for(i=1;i<=depth;i++)
			System.out.print("    ");
		System.out.println(data);
		
		//打印子树
		if(left!=null)
		{
			left.print(depth+1);
		}
		else if(right!=null)
		{
			for(i=0;i<=depth;i++)
				System.out.print("    ");
			System.out.println("--");
		}
		
		if(right!=null)
		{
			right.print(depth+1);
		}
		else if(left!=null)
		{
			for(i=0;i<=depth;i++)
				System.out.print("    ");
			System.out.println("--");
		}
	}
	
	public BTNode<E> removeLeftmost()
	{
		if(left==null)
		{
			return right;
		}
		else 
		{
			left=left.removeLeftmost();
			return this;
		}
	}
	
	public BTNode<E> removeRightmost()
	{
		if(right==null)
		{
			return left;
		}
		else 
		{
			right=right.removeRightmost();
			return this;
		}
	}
	
	public void setData(E newData)
	{
		data=newData;
	}
	public void setLeft(BTNode<E> newLeft)
	{
		left=newLeft;
	}
	public void setRight(BTNode<E> newRight)
	{
		right=newRight;
	}
	
	public static <E> BTNode<E> treeCopy(BTNode<E> source)
	{
		BTNode<E> leftCopy,rightCopy;
		if(source==null)
			return null;
		else{
			leftCopy=treeCopy(source.left);
			rightCopy=treeCopy(source.right);
			
			return new BTNode<E>(source.data, leftCopy, rightCopy);
		}
	}
	
	public static <E> int treesize(BTNode<E> root) {
	
		if(root==null)
			return 0;
		else {
			return 1+treesize(root.left)+treesize(root.right);
		}

	}
}
层次遍历建立二叉树,这里使用的递归的方法,当然也可以建立一个队列来初始化这个二叉树!

package Tree;

import java.io.*;

public class Erchashu {
	
	public static BTNode<Integer> BuildTree(int[] treedata,int n){
		if(treedata.length==0)
			return null;
		else{
			if(n<treedata.length)
			{
				int l=n*2+1;
				int r=n*2+2;
				
				BTNode<Integer> TreeRoot=new BTNode<Integer>(treedata[n], BuildTree(treedata, l), BuildTree(treedata, r));
				
				return TreeRoot;
			}	
			else 
				return null;
		}
	}
	
	
	public static void main(String args[]) throws Exception {
		
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String tempdata=br.readLine();
		String[] data=tempdata.split(" ");
		
		
		BTNode<Integer> treeroot;
		int[] treedata=new int[data.length];
		
		for(int i=0;i<treedata.length;i++)
		{
			treedata[i]=Integer.parseInt(data[i]);
			System.out.println(treedata[i]);
		}
		
		treeroot=BuildTree(treedata,0);
		
		treeroot.print(0);
		System.out.println("后序遍历");
		treeroot.postorderPrint();
		System.out.println("前序遍历");
		treeroot.preorderPrint();
		System.out.println("中序遍历");
		treeroot.inorderPrint();
	}
}




  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
层次遍历,也称为广度优先遍历(Breadth-First Search, BFS),是一种用于访问或搜索树和图中节点的有效方法。在二叉树中,层次遍历按照从上到下、从左到右的顺序依次访问每个节点,类似于打印一棵树的层级结构。 在C++中,使用队列来实现层次遍历,步骤如下: 1. 创建一个队列,将根节点入队。 2. 当队列不为空时,执行以下操作: a. 弹出队首元素作为当前节点。 b. 访问当前节点。 c. 将当前节点的左右子节点(如果存在)依次入队。 3. 重复步骤2,直到队列为空。 以下是一个简单的C++代码示例,假设我们有一个名为`Node`的二叉树节点类: ```cpp #include <iostream> #include <queue> // 假设Node类有一个整型值和两个指向子节点的指针 class Node { public: int val; Node* left; Node* right; Node(int val = 0, Node* left = nullptr, Node* right = nullptr) : val(val), left(left), right(right) {} }; void levelOrderTraversal(Node* root) { if (root == nullptr) return; // 如果根节点为空,直接返回 std::queue<Node*> q; // 定义队列 q.push(root); // 将根节点入队 while (!q.empty()) { Node* node = q.front(); // 取出队首节点 q.pop(); // 出队 std::cout << node->val << " "; // 访问节点 if (node->left) q.push(node->left); // 左子节点入队 if (node->right) q.push(node->right); // 右子节点入队 } } int main() { // 创建并初始化二叉树 Node* tree = ...; // 用实际的节点构建树 levelOrderTraversal(tree); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值