在一颗二叉树上插入节点

在一颗二叉树上插入节点,插入是建立在小于父节点,则插入到父节点左边,如果大于父节点,则插入到父节点右边。在插入树节点,需要判断树根是否为空,如果不为空,则需要当前节点指向树根和父节点指向当前节点。直到当前节点等于null,那么可以在父节点左边或者右边插入新节点,并且返回树根跳出循环。如果树根为空,直接把树根指向新创建的节点,实现过程如下所示:

package cn.edu.nwu.structs.tree;

/**
 * @author jcm
 * 插入树节点
 *时间 2016年9月2日
 */
public class InsertBinaryTreeNode {
	public static void main(String[] args) {
		BinaryTreeNode root = null;
		root = insertBinaryTNode(root,43);
		root = insertBinaryTNode(root,87);
		root = insertBinaryTNode(root,-43);
		root = insertBinaryTNode(root,98);
		show(root,1);
	}
	/**
	 * @author jcm
	 *  在一颗二叉树上插入节点,如果二叉树为空,就让树根指向新插入的节点
	 * @param root 树根
	 * @param data 数据
	 * @return 返回树根的节点
	 */
	public static BinaryTreeNode insertBinaryTNode(BinaryTreeNode root,int data){
		//创建树节点
		BinaryTreeNode newTNode = new BinaryTreeNode(data);
		//如果树根是空的,就让树根指向新创建的树节点
		if(root == null){
			root = newTNode;
			return root;
		}else{
			//建立当前树节点
			BinaryTreeNode currentTNode = root;
			while(true){
				//让父节点指向当前节点
				BinaryTreeNode parentTNode = currentTNode;
				if(currentTNode.data > data){
					currentTNode = currentTNode.leftTreeNode;
					if(currentTNode == null){
						parentTNode.leftTreeNode = newTNode;
						return root;//跳出循环,并返回树根
					}
				}else{
					currentTNode = currentTNode.rightTreeNode;
					if(currentTNode == null){
						parentTNode.rightTreeNode = newTNode;
						return root;
					}
				}
			}
		}
	}
	//中序遍历,通过n控制层数
	public static void show(BinaryTreeNode root,int n){
		if(root == null){
			return ;
		}else{
			show(root.leftTreeNode,n+1);
			for (int i=0;i<n;i++){
				System.out.print("	");
			}
			System.out.println(root.data);
			show(root.rightTreeNode,n+1);
		}
	}
}


如果要将一棵树插入到另一棵二叉树,我们可以按照以下步骤进行: 1. 定义二叉树节点结构: ```cpp struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; ``` 2. 编写递归函数,将一棵树的节点逐个插入二叉树: ```cpp void insertTree(TreeNode* root, TreeNode* tree) { if (tree == nullptr) { return; } // 插入当前节点 root = insert(root, tree->val); // 递归插入左子树和右子树 insertTree(root, tree->left); insertTree(root, tree->right); } ``` 3. 编写插入节点的函数,与之前提到的二叉树插入节点的函数相同: ```cpp TreeNode* insert(TreeNode* root, int val) { if (root == nullptr) { return new TreeNode(val); } if (val < root->val) { root->left = insert(root->left, val); } else { root->right = insert(root->right, val); } return root; } ``` 4. 在主函数创建两棵树,并将其一棵树插入到另一棵树: ```cpp int main() { // 创建第一棵二叉树 TreeNode* root1 = nullptr; root1 = insert(root1, 5); root1 = insert(root1, 3); root1 = insert(root1, 7); // 创建第二棵树 TreeNode* root2 = nullptr; root2 = insert(root2, 2); root2 = insert(root2, 4); root2 = insert(root2, 6); // 将第二棵树插入到第一棵树 insertTree(root1, root2); // 遍历合并后的二叉树 std::cout << "序遍历结果: "; inorderTraversal(root1); std::cout << std::endl; return 0; } ``` 这段代码演示了将一棵树插入到另一棵二叉树,并使用序遍历验证合并操作。注意,合并后的二叉树的结构可能会改变,具体结果取决于插入的位置。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值