java二叉排序
Ⅰ 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