java 递归求二进制,用Java递归创建二进制搜索树

我一直在尝试创建一个递归方法,它将创建一个完整的二叉搜索树 . 此方法返回对此树的根的引用 . 作为参数,我传递树的深度以及存储在当前子树的根中的数字 . 我已经设法为2个基本情况设计解决方案,当深度为0和1时,但是当我尝试使用大于1的数字时,我只得到0级和1级实例化而不是下一个 . 任何帮助都会很棒

public class BinaryNode {

private int data;

private BinaryNode left, right;

public BinaryNode(int initialData, BinaryNode initialLeft,

BinaryNode initialRight) {

data = initialData;

left = initialLeft;

right = initialRight;

}

public static BinaryNode BSTFactory(int top,int depth) {

BinaryNode root=new BinaryNode(top,null,null);

BinaryNode leftChild,rightChild;

if(depth==0)

//return root;

if(depth==1){

//create 2 children left and right

leftChild=new BinaryNode(top-1,null,null);

rightChild=new BinaryNode(top+1,null,null);

root=new BinaryNode(top,leftChild,rightChild);

//return root;

}

if(depth>1){

leftChild=BSTFactory(top-1,depth-1);

rightChild=BSTFactory(top+1,depth-1);

root=new BinaryNode(top,leftChild,rightChild);

//return root;

}

return root;

}

public class Applications {

public static void main(String[] args){

BinaryNode root2=BinaryNode.BSTFactory(8, 2);

System.out.println(root2.toString());

}

}

This is the output:

data: 8

8's left: data: 7

7's left: null

7's right: null

8's right: data: 9

9's left: null

9's right: null

二进制树形搜索算法Java实现需要设计一个二叉的数据结构,并实现节点的插入和搜索操作。下面是一个基于递归实现的二叉数据结构的Java代码示例,包括插入和搜索操作。 ```java public class BinarySearchTree { private Node root; // 定义节点类 private class Node { int key; Node left; Node right; public Node(int key) { this.key = key; this.left = null; this.right = null; } } // 插入节点 public void insert(int key) { root = insert(root, key); } private Node insert(Node node, int key) { if (node == null) { return new Node(key); } if (key < node.key) { node.left = insert(node.left, key); } else if (key > node.key) { node.right = insert(node.right, key); } return node; } // 查找节点 public Node search(int key) { return search(root, key); } private Node search(Node node, int key) { if (node == null || node.key == key) { return node; } if (key < node.key) { return search(node.left, key); } else { return search(node.right, key); } } } ``` 在上述代码实现中,我们定义了一个Node类作为二叉的节点,其中包含一个键值key,以及左右子节点的引用。在插入操作中,我们通过递归实现了节点的插入。在查找操作中,同样使用递归方式查找节点。这些操作的时间复杂度均为O(log n)。 对于二进制树形搜索算法实现,我们需要在节点类中添加一个二进制编码的属性,用于存储每个节点的编码信息。在插入节点时,我们需要先将要插入的元素转换为二进制编码,然后按照字典序与二叉中的节点进行比较,从而找到对应的位置。在查找节点时,同样需要将要查找的元素转换为二进制编码,然后按照字典序与二叉中的节点进行比较,从而找到对应的节点。 下面是基于二进制树形搜索算法Java代码实现示例: ```java public class BinarySearchTree { private Node root; // 定义节点类 private class Node { int key; String code; Node left; Node right; public Node(int key, String code) { this.key = key; this.code = code; this.left = null; this.right = null; } } // 插入节点 public void insert(int key) { String code = toBinaryString(key); root = insert(root, key, code); } private Node insert(Node node, int key, String code) { if (node == null) { return new Node(key, code); } if (code.compareTo(node.code) < 0) { node.left = insert(node.left, key, code); } else if (code.compareTo(node.code) > 0) { node.right = insert(node.right, key, code); } return node; } // 查找节点 public Node search(int key) { String code = toBinaryString(key); return search(root, key, code); } private Node search(Node node, int key, String code) { if (node == null || node.key == key) { return node; } if (code.compareTo(node.code) < 0) { return search(node.left, key, code); } else { return search(node.right, key, code); } } // 生成二进制编码 private String toBinaryString(int n) { StringBuilder sb = new StringBuilder(); while (n > 0) { sb.append(n % 2); n /= 2; } return sb.reverse().toString(); } } ``` 在上述代码实现中,我们修改了Node类,添加了一个code属性,用于存储每个节点的二进制编码信息。在插入节点时,我们先将要插入的元素转换为二进制编码,然后按照字典序与二叉中的节点进行比较,从而找到对应的位置。在查找节点时,同样需要将要查找的元素转换为二进制编码,然后按照字典序与二叉中的节点进行比较,从而找到对应的节点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值