java建立二叉排序树,为什么最后打印null,没有建成呢

classBSTreeNode{
intdata;
BSTreeNodeleftchild;
BSTreeNoderightchild;
}
classBSTree{
publicBSTreeNodeinsert(BSTreeNodenode,intkey){
if(node==null){
node=newBSTreeNode();
node.data=key;
node.leftchild=node.rightchild=null;
}elseif(key<node.data)
node.leftchild=insert(node.leftchild,key);
elseif(key>node.data)
node.rightchild=insert(node.rightchild,key);
returnnode;
}
publicBSTreeNodecreateBSTree(){
BSTreeNodenode=null;
int[]data={20,10,30,8,12,25,40};
for(inti=0;i<data.length;i++)
node=insert(node,data[i]);
returnnode;
}
}
publicclassBSTree1{
publicstaticvoidmain(String[]args){
BSTreetree=newBSTree();
BSTreeNoderoot=tree.createBSTree();
System.out.println(root);
}
}

Ⅱ 二叉排序树的插入与查找 求Java大神帮忙


//二叉树结构类Btree.java
publicclassBtree{
privateintdata;
privateBtreeleft;
privateBtreeright;
publicvoidsetData(intdata){
this.data=data;
}
publicintgetData(){
returndata;
}
publicvoidsetLeft(Btreebtree){
this.left=btree;
}
publicvoidsetRight(Btreebtree){
this.right=btree;
}
publicBtreegetLeft(){
returnleft;
}
publicBtreegetRight(){
returnright;
}
publicBtree(){
super();
}
}
//工具类,二叉树创建,查找,遍历,排序。都在这了Tools.java
publicclassTools{
publicBtreecreate(intdata){
Btreebtree=newBtree();
btree.setData(data);
returnbtree;
}
publicvoidadd(Btreebtree,intdata){
if(data<btree.getData()){
if(btree.getLeft()!=null){
btree=btree.getLeft();
add(btree,data);
}else{
btree.setLeft(create(data));
}
}else{
if(btree.getRight()!=null){
btree=btree.getRight();
add(btree,data);
}else{
btree.setRight(create(data));
}
}
}
//中序遍历
publicvoidmidSerch(Btreebtree){
if(btree.getLeft()!=null)
midSerch(btree.getLeft());
System.out.print(btree.getData()+"");
if(btree.getRight()!=null)
midSerch(btree.getRight());
}
//二叉树查找
publicvoidfind(Btreebtree,intdata){
if(btree.getData()>data)
{
if(btree.getLeft()==null)
{
System.out.println("没有这种结果,搜索完毕");
return;
}else
find(btree.getLeft(),data);
}elseif(btree.getData()==data)
{
System.out.println("查找成功,查找到的数据是"+data);
return;
}else
{
if(btree.getRight()==null){
System.out.println("没有这种结果,搜索完毕");
return;
}
else
find(btree.getRight(),data);
}
}
}
//主类,与注释的自己看MidSerchBtree.java
publicclassMidSerchBtree{
publicstaticvoidmain(Stringargs[]){
Toolstools=newTools();
intdatas[]={6,4,3,7,8,9,2,1,5,8,9,12,23,45,3,7,5};
Btreebtree=tools.create(datas[0]);
for(inti=1;i<datas.length;i++){//第一个初始化插入作为根节点了
tools.add(btree,datas[i]);
}
tools.midSerch(btree);//中根遍历小的插入左边,大的在右边,所以,中序遍历一遍就是排序
tools.find(btree,56);
}
}

Ⅲ 数据结构(Java):给出在二叉排序树上查找键值为K的算法函数

p* find(p* r, int v){if(v==p->data)return p;if(v < p->data) return find(p->lchild, v);elsereturn find(p->rchild, v);}// 大概思路就是这样当前节点是要查找的值 直接返回小于查找左子树大于查找右子数
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
java版:
node search(node p, data key) // data是你的键值类型
{
if(p == null || p.key == key)
return p;
if(p.key < key)
return search(p.rchild, key);
else
return search(p.lchild, key);
}

Ⅳ 计算机C语言数据结构javaIT二叉排序树的构造

字符串的大小是逐字符比较,比较字符的ascii码。
排序树,可以是左树比根节点大,右树比它小,或者反过来也行。这样就是有序的,可以从根开始查找串。

Ⅳ JAVA如何将英文字母进行二叉树排序

如果仅限于java,而且是实际应用,java里有一个叫做TreeSet的东西,是个有序的树结构。Sring类型的英文字符可在里面自排序。

如果是考试,应该是靠你如何实现一个类似于TreeSet的东西

Ⅵ java写的关于二叉排序树的删除操作的问题

package ggg;

import java.math.BigDecimal;
import java.util.Scanner;
import java.util.Random;
import java.util.Stack;

/**
* 二叉排序树(又称二叉查找树)
* (1)可以是一颗空树
* (2)若左子树不空,则左子树上所有的结点的值均小于她的根节点的值
* (3)若右子树不空,则右子树上所有的结点的值均大于她的根节点的值
* (4)左、右子树也分别为二叉排序树
*
*
* 性能分析:
* 查找性能:
* 含有n个结点的二叉排序树的平均查找长度和树的形态有关,
* (最坏情况)当先后插入的关键字有序时,构成的二叉排序树蜕变为单枝树。查找性能为O(n)
* (最好情况)二叉排序树的形态和折半查找的判定树相同,其平均查找长度和log2(n)成正比
*
*
* 插入、删除性能:
* 插入、删除操作间复杂度都O(log(n))级的,
* 即经过O(log(n))时间搜索到了需插入删除节点位置和删除节点的位置
* 经O(1)级的时间直接插入和删除
* 与顺序表相比,比序顺序表插入删除O(n)(查找时间O(log(n))移动节点时间O(n))要快
* 与无序顺序表插入时间O(1),删除时间O(n)相比,因为是有序的,所查找速度要快很多
*
*
*
* 作者:小菜鸟
* 创建时间:2014-08-17
*
*/

public class BinarySortTree {

private Node root = null;

/**查找二叉排序树中是否有key值*/
public boolean searchBST(int key){
Node current = root;
while(current != null){
if(key == current.getValue())
return true;
else if(key < current.getValue())
current = current.getLeft();
else
current = current.getRight();
}
return false;
}

/**向二叉排序树中插入结点*/
public void insertBST(int key){
Node p = root;
/**记录查找结点的前一个结点*/
Node prev = null;
/**一直查找下去,直到到达满足条件的结点位置*/
while(p != null){
prev = p;
if(key < p.getValue())
p = p.getLeft();
else if(key > p.getValue())
p = p.getRight();
else
return;
}
/**prve是要安放结点的父节点,根据结点值得大小,放在相应的位置*/
if(root == null)
root = new Node(key);
else if(key < prev.getValue())
prev.setLeft(new Node(key));
else prev.setRight(new Node(key));
}

/**
* 删除二叉排序树中的结点
* 分为三种情况:(删除结点为*p ,其父结点为*f)
* (1)要删除的*p结点是叶子结点,只需要修改它的双亲结点的指针为空
* (2)若*p只有左子树或者只有右子树,直接让左子树/右子树代替*p
* (3)若*p既有左子树,又有右子树
* 用p左子树中最大的那个值(即最右端S)代替P,删除s,重接其左子树
* */
public void deleteBST(int key){
deleteBST(root, key);
}
private boolean deleteBST(Node node, int key) {
if(node == null) return false;
else{
if(key == node.getValue()){
return delete(node);
}
else if(key < node.getValue()){
return deleteBST(node.getLeft(), key);
}
else{
return deleteBST(node.getRight(), key);
}
}
}

private boolean delete(Node node) {
Node temp = null;
/**右子树空,只需要重接它的左子树
* 如果是叶子结点,在这里也把叶子结点删除了
* */
if(node.getRight() == null){
temp = node;
node = node.getLeft();
}
/**左子树空, 重接它的右子树*/
else if(node.getLeft() == null){
temp = node;
node = node.getRight();
}
/**左右子树均不为空*/
else{
temp = node;
Node s = node;
/**转向左子树,然后向右走到“尽头”*/
s = s.getLeft();
while(s.getRight() != null){
temp = s;
s = s.getRight();
}
node.setValue(s.getValue());
if(temp != node){
temp.setRight(s.getLeft());
}
else{
temp.setLeft(s.getLeft());
}
}
return true;
}

/**中序非递归遍历二叉树
* 获得有序序列
* */
public void nrInOrderTraverse(){
Stack<Node> stack = new Stack<Node>();
Node node = root;
while(node != null || !stack.isEmpty()){
while(node != null){
stack.push(node);
node = node.getLeft();
}
node = stack.pop();
System.out.println(node.getValue());
node = node.getRight();
}
}

public static void main(String[] args){
BinarySortTree bst = new BinarySortTree();
/**构建的二叉树没有相同元素*/
int[] num = {4,7,2,1,10,6,9,3,8,11,2, 0, -2};
for(int i = 0; i < num.length; i++){
bst.insertBST(num[i]);
}
bst.nrInOrderTraverse();
System.out.println(bst.searchBST(10));
bst.deleteBST(2);
bst.nrInOrderTraverse();
}

/**二叉树的结点定义*/
public class Node{
private int value;
private Node left;
private Node right;

public Node(){
}
public Node(Node left, Node right, int value){
this.left = left;
this.right = right;
this.value = value;
}
public Node(int value){
this(null, null, value);
}

public Node getLeft(){
return this.left;
}
public void setLeft(Node left){
this.left = left;
}
public Node getRight(){
return this.right;
}
public void setRight(Node right){
this.right = right;
}
public int getValue(){
return this.value;
}
public void setValue(int value){
this.value = value;
}
}

}

Ⅶ java二叉排序树问题

给分!!!!!!:....还行不难写....其实我很怀念学C时写树......
class TreeNode 数据结构
class SortTree 实现树的建立 和 中序遍历

public class TreeNode
{
public TreeNode left;
public TreeNode right;
public Integer value;

TreeNode ( Integer value )
{
this.value = value;
System.out.println(value);
}
}

public class SortTree
{
SortTree(Integer...integers)
{
createSortTree(integers);
}

private TreeNode root;

private void createSortTree(Integer[] integers)
{
for(Integer nodeValue:integers)
{
if(root == null)
{
System.out.println("Create Root");
root = new TreeNode(nodeValue);
}
else if(root.value > nodeValue)
{
insertLeft(root , nodeValue);
}
else
{
insertRight(root , nodeValue);
}
}
}

private void insertLeft(TreeNode root2, Integer nodeValue)
{
if(root2.left == null)
{
System.out.println("Create Left");
root2.left = new TreeNode(nodeValue);
}
else if(root2.left.value > nodeValue)
{
insertLeft(root2.left,nodeValue);
}
else
{
insertRight(root2.left,nodeValue);
}

}

private void insertRight(TreeNode root2, Integer nodeValue)
{
if(root2.right == null)
{
System.out.println("Create Right");
root2.right = new TreeNode(nodeValue);
}
else if(root2.right.value > nodeValue)
{
insertLeft(root2.right,nodeValue);
}
else
{
insertRight(root2.right,nodeValue);
}
}

private void middleTraversing(TreeNode root)
{
if(root.left !=null) /** 第一步访问左子树 */
middleTraversing(root.left);
System.out.println(root.value); /** 第二步访问根结点 */
if(root.right !=null)
middleTraversing(root.right); /** 第三步访问右子树 */
}

public TreeNode getRoot()
{
return root;
}

public static void main (String[] args)
{
SortTree tree = new SortTree(new Integer(5),new Integer(6),new Integer(4),new Integer(3),new Integer(7));
System.out.println("MiddleTraversing:");
tree.middleTraversing(tree.getRoot());
}
}

Ⅷ java二叉树排序问题

importjava.util.TreeSet;
publicclassStudentTest{
publicstaticvoidmain(String[]args){
TreeSet<Student>ts=newTreeSet<>();
for(;ts.size()<10;){
Stringname="同学"+(char)((Math.round(Math.random()*26+65)));
intid=(int)(Math.round(Math.random()*80+10));
floatfl=(int)Math.floor(Math.random()*50+40);
ts.add(newStudent(name,fl,id));
}
for(Studenta:ts){
System.out.println(a);
}
}
}
<Student>{
privateStringname;
privateFloathp;
privateintid;
publicStudent(Stringname,floathp,intid){
this.name=name;
this.hp=hp;
this.id=id;
}
publicStringtoString(){
return"(name:"+name+"id:"+id+"hp:"+hp+")";
}
publicintgetId(){
returnid;
}
publicintcompareTo(Studentstu){
returnInteger.compare(this.id,stu.getId());
}
}

Ⅸ java二叉排序树,已有代码,如何调通输出

你好,很高兴回答你的问题。

目前已经有了二叉树以及二叉树节点的类。

需要一个内main方法,在其中创建节点(容通过节点类的构造方法),构建树(通过树的构造方法以及insert方法)。可以执行查询的方法以及展示的方法。

如果有帮助到你,请点击点赞。

Ⅹ 二叉排序树(BST) Java实现

public class Node<E> {
int key; // data used as key value
E data; // other data
Node<E> leftChild; // this node's left child
Node<E> rightChild; // this node's right child

public Node(int key, E o) {
this.key = key;
this.data = o;
}

public void displayNode() {
System.out.printf("%d, %s\n", key, data.toString());
}
}

===============================================================

package net.acoder.adt.tree;

public class Tree<E> {
private Node<E> root;

public Tree(Node<E> root) {
if (root == null) {
throw new NullPointerException("root can't be null");
}
this.root = root;
}

public Tree(int key, E o) {
this(new Node<E>(key, o));
}

public Node<E> getRoot() {
return root;
}

/**
* find a node by its key
*
* @param key
* @return
*/
public Node<E> find(int key) {
Node<E> current = root;
while (current.key != key) {
if (key < current.key) {
current = root.leftChild;
} else {
current = root.rightChild;
}
if (current == null) {
return null;
}
}
return current;
}

/**
* insert a node to this tree
*
* @param key
* @param o
*/
public void insert(int key, E o) {
Node<E> aNode = new Node<E>(key, o);
if (root == null) {
this.root = aNode;
return;
}

Node<E> current = root;
Node<E> parent = root;
while (true) {
parent = current;
if (key < parent.key) {
current = parent.leftChild;
if (current == null) {
parent.leftChild = aNode;
return;
}
} else {
current = parent.rightChild;
if (current == null) {
parent.rightChild = aNode;
return;
}
}
}
}

public boolean delete(int key) {
Node<E> current = root;
Node<E> parent = root;
boolean isLeftChild = true;
// search for node
while (current.key != key) {
parent = current;
if (key < current.key) {
isLeftChild = true;
current = current.leftChild;
} else {
isLeftChild = false;
current = current.rightChild;
}
if (current == null) {
return false;
}
}

// if no children, simply delete it
if (current.leftChild == null && current.rightChild == null) {
if (current == parent) {
root = null;
} else if (isLeftChild == true) {
parent.leftChild = null;
} else if (isLeftChild == false) {
parent.rightChild = null;
}
return true;
}

// if no left children, replace with right subtree
if (current.leftChild == null) {
if (current == root) {
root = current.rightChild;
} else if (isLeftChild) {
parent.leftChild = current.rightChild;
} else if (!isLeftChild) {
parent.leftChild = current.rightChild;
}
return true;
}

// if no right children, replace with left subtree
if (current.rightChild == null) {
if (current == root) {
root = current.leftChild;
} else if (isLeftChild) {
parent.leftChild = current.leftChild;
} else if (!isLeftChild) {
parent.leftChild = current.leftChild;
}
return true;
}

// get successor of node to delete
Node<E> successor = getSuccessor(current);
if (current == root) {
current = successor;
} else if (isLeftChild) {
parent.leftChild = successor;
} else {
parent.rightChild = successor;
}
successor.leftChild = current.leftChild;
return true;
}

private Node<E> getSuccessor(Node<E> delNode) {
Node<E> successorParent = delNode;
Node<E> successor = delNode;
Node<E> current = delNode.rightChild;
while (current != null) {
successorParent = successor;
successor = current;
current = current.leftChild;
}

if (successor != delNode.rightChild) {
successorParent.leftChild = successor.rightChild;
successor.rightChild = delNode.rightChild;

}
return successor;
}

public void inOrder(Node<E> aNode) {
if (aNode != null) {
inOrder(aNode.leftChild);
aNode.displayNode();
inOrder(aNode.rightChild);
}
}

public void preOrder(Node<E> aNode) {
if (aNode != null) {
aNode.displayNode();
inOrder(aNode.leftChild);
inOrder(aNode.rightChild);
}
}

public void backOrder(Node<E> aNode) {
if (aNode != null) {
inOrder(aNode.leftChild);
inOrder(aNode.rightChild);
aNode.displayNode();
}
}

public Node<E> minimum() {
Node<E> current = this.root;
Node<E> result = null;
while (current != null) {
result = current;
current = current.leftChild;
}
return result;
}

public Node<E> maximum() {
Node<E> current = this.root;
Node<E> result = null;
while (current != null) {
result = current;
current = current.rightChild;
}
return result;
}
}

以前的代码, 记得没写完, 好像就是BST