二叉树排序
来源:互联网 发布:higher brothers 知乎 编辑:程序博客网 时间:2024/06/14 08:48
package test.sort;
public class BinaryNode {
/**
* author: sunxing007,
* 转载请注明来自http://blog.csdn.net/sunxing007
**/
private int value;//current value
private BinaryNode lChild;//left child
private BinaryNode rChild;//right child
public BinaryNode(int value, BinaryNode l, BinaryNode r){
this.value = value;
this.lChild = l;
this.rChild = r;
}
public BinaryNode getLChild() {
return lChild;
}
public void setLChild(BinaryNode child) {
lChild = child;
}
public BinaryNode getRChild() {
return rChild;
}
public void setRChild(BinaryNode child) {
rChild = child;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
//iterate all node.
public static void iterate(BinaryNode root){
if(root.lChild!=null){
iterate(root.getLChild());
}
System.out.print(root.getValue() + " ");
if(root.rChild!=null){
iterate(root.getRChild());
}
}
/**
* add child to the current node to construct a tree.
* Time: O( nlog(n) )
* **/
public void addChild(int n){
if(n<value){
if(lChild!=null){
lChild.addChild(n);
}
else{
lChild = new BinaryNode(n, null, null);
}
}
else{
if(rChild!=null){
rChild.addChild(n);
}
else{
rChild = new BinaryNode(n, null, null);
}
}
}
//test case.
public static void main(String[] args){
System.out.println();
int[] arr = new int[]{23,54,1,65,9,3,100};
BinaryNode root = new BinaryNode(arr[0], null, null);
for(int i=1; i<arr.length; i++){
root.addChild(arr[i]);
}
BinaryNode.iterate(root);
}
}
public class BinaryNode {
/**
* author: sunxing007,
* 转载请注明来自http://blog.csdn.net/sunxing007
**/
private int value;//current value
private BinaryNode lChild;//left child
private BinaryNode rChild;//right child
public BinaryNode(int value, BinaryNode l, BinaryNode r){
this.value = value;
this.lChild = l;
this.rChild = r;
}
public BinaryNode getLChild() {
return lChild;
}
public void setLChild(BinaryNode child) {
lChild = child;
}
public BinaryNode getRChild() {
return rChild;
}
public void setRChild(BinaryNode child) {
rChild = child;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
//iterate all node.
public static void iterate(BinaryNode root){
if(root.lChild!=null){
iterate(root.getLChild());
}
System.out.print(root.getValue() + " ");
if(root.rChild!=null){
iterate(root.getRChild());
}
}
/**
* add child to the current node to construct a tree.
* Time: O( nlog(n) )
* **/
public void addChild(int n){
if(n<value){
if(lChild!=null){
lChild.addChild(n);
}
else{
lChild = new BinaryNode(n, null, null);
}
}
else{
if(rChild!=null){
rChild.addChild(n);
}
else{
rChild = new BinaryNode(n, null, null);
}
}
}
//test case.
public static void main(String[] args){
System.out.println();
int[] arr = new int[]{23,54,1,65,9,3,100};
BinaryNode root = new BinaryNode(arr[0], null, null);
for(int i=1; i<arr.length; i++){
root.addChild(arr[i]);
}
BinaryNode.iterate(root);
}
}
0 0
- 二叉树,排序二叉树
- 排序算法-二叉树排序
- 树-排序二叉树
- 二叉树排序
- 排序二叉树
- 使用二叉树排序
- 排序二叉树 原理
- 二叉树排序法
- 二叉树排序
- java-二叉树排序
- 二叉排序(查找)树
- 排序二叉树
- 二叉树排序
- 二叉树排序
- 二叉树排序算法
- 二叉排序(查找)树
- 二叉树排序原理
- 二叉树排序
- OD IDA 找到延迟代码,编码实现加速
- quartz定时任务
- Particle for alexa smart home skill (2)
- POJ1416-Shredding Company
- __builtin_expect(!!(x), 1) 之__builtin_expect
- 二叉树排序
- Back Top ①(回到顶部)原生JS实现
- Unable to get the default Bean Validation factory
- nefu 65 数的长度
- ACDream 1067 Convex
- 校招算法复习之冒泡排序
- STL中的set容器的一点总结
- 数据结构实验之排序三:bucket sort
- CodeForce 706C 动态规划 利用下标表示状态 递推 (伪状态压缩)