二叉查找树的构造与遍历[Java实现]

构造二叉查找树之插入算法:
比较新节点关键字与格子树根节点的大小关系。如果新节点关键字小,则递归进入相应根节点的左子树,直到找到左子树为空的位置;否则,递归进入相应根节点的右子树,直到找到右子树为空的位置。

树节点:

[java] view plain copy print ?
  1. package org.pbdevj.ds.search.binarysearchtree;
  2. import java.io.Serializable;
  3. /**树节点*/
  4. public class TreeNode implements Serializable {
  5. public Object nodeValue;//数据域
  6. public TreeNode lChild;//左子树
  7. public TreeNode rChild;//右子树
  8. public TreeNode() {
  9. // TODO Auto-generated constructor stub
  10. }
  11. public TreeNode(Object nodeValue, TreeNode lChild, TreeNode rChild) {
  12. super();
  13. this.nodeValue = nodeValue;
  14. this.lChild = lChild;
  15. this.rChild = rChild;
  16. }
  17. }
package org.pbdevj.ds.search.binarysearchtree; import java.io.Serializable; /**树节点*/ public class TreeNode implements Serializable { public Object nodeValue;//数据域 public TreeNode lChild;//左子树 public TreeNode rChild;//右子树 public TreeNode() { // TODO Auto-generated constructor stub } public TreeNode(Object nodeValue, TreeNode lChild, TreeNode rChild) { super(); this.nodeValue = nodeValue; this.lChild = lChild; this.rChild = rChild; } }

查找回调接口:

[java] view plain copy print ?
  1. package org.pbdevj.ds.search.binarysearchtree;
  2. /**查找回调接口*/
  3. public interface ISearchCallBack {
  4. void visit(TreeNode e);
  5. }
package org.pbdevj.ds.search.binarysearchtree; /**查找回调接口*/ public interface ISearchCallBack { void visit(TreeNode e); }

查找接口:

[java] view plain copy print ?
  1. package org.pbdevj.ds.search.binarysearchtree;
  2. /**查找接口*/
  3. public interface ISearch {
  4. TreeNode search(Object e);
  5. TreeNode search(Object e,ISearchCallBack searchCallBack);
  6. void output(ISearchCallBack searchCallBack);
  7. }
package org.pbdevj.ds.search.binarysearchtree; /**查找接口*/ public interface ISearch { TreeNode search(Object e); TreeNode search(Object e,ISearchCallBack searchCallBack); void output(ISearchCallBack searchCallBack); }

二叉查找树:

[java] view plain copy print ?
  1. package org.pbdevj.ds.search.binarysearchtree;
  2. /**二叉查找树*/
  3. public class BinarySearchTree implements ISearch {
  4. private Object[] elements;
  5. TreeNode rootNode;//树根节点
  6. public BinarySearchTree(Object[] elements) {
  7. // TODO Auto-generated constructor stub
  8. this.elements = elements;
  9. buildBinarySearchTree();
  10. }
  11. public BinarySearchTree(TreeNode rootNode) {
  12. // TODO Auto-generated constructor stub
  13. this.rootNode = rootNode;
  14. }
  15. /**构造二叉查找树*/
  16. private void buildBinarySearchTree(){
  17. if(elements==null || elements.length==0)
  18. return;
  19. rootNode =new TreeNode(elements[0],null,null);
  20. if (elements.length==1)
  21. return;
  22. for (int index = 1; index < elements.length; index++) {//每次循环从数组中取一个元素
  23. TreeNode tempNode = rootNode;
  24. while(true){//将这个元素与左子树或右子树循环比较,小于则继续进入左子树的左子树,否则继续进入右子树的右子树
  25. if (elements[index].toString().compareTo(tempNode.nodeValue.toString())<0){//小于中间节点
  26. if (tempNode.lChild==null){
  27. tempNode.lChild = new TreeNode(elements[index],null,null);
  28. break;
  29. }
  30. tempNode = tempNode.lChild;//继续比较左子树
  31. } else {
  32. if (tempNode.rChild==null){
  33. tempNode.rChild = new TreeNode(elements[index],null,null);
  34. break;
  35. }
  36. tempNode = tempNode.rChild;//继续比较右子树
  37. }
  38. }
  39. }
  40. }
  41. /**中序输出这个二叉查找树*/
  42. @Override
  43. public void output(ISearchCallBack searchCallBack) {
  44. // TODO Auto-generated method stub
  45. if (rootNode==null)
  46. return;
  47. _output(rootNode,searchCallBack);
  48. }
  49. /**中序输出这个二叉查找树[递归函数]*/
  50. private void _output(TreeNode node,ISearchCallBack searchCallBack){
  51. if (node.lChild!=null)//遍历左子树
  52. _output(node.lChild,searchCallBack);
  53. if (searchCallBack!=null)//访问根节点
  54. searchCallBack.visit(node);
  55. if (node.rChild!=null)//遍历右子树
  56. _output(node.rChild,searchCallBack);
  57. }
  58. @Override
  59. public TreeNode search(Object e) {
  60. // TODO Auto-generated method stub
  61. return null;
  62. }
  63. @Override
  64. public TreeNode search(Object e, ISearchCallBack searchCallBack) {
  65. // TODO Auto-generated method stub
  66. return null;
  67. }
  68. }
package org.pbdevj.ds.search.binarysearchtree; /**二叉查找树*/ public class BinarySearchTree implements ISearch { private Object[] elements; TreeNode rootNode;//树根节点 public BinarySearchTree(Object[] elements) { // TODO Auto-generated constructor stub this.elements = elements; buildBinarySearchTree(); } public BinarySearchTree(TreeNode rootNode) { // TODO Auto-generated constructor stub this.rootNode = rootNode; } /**构造二叉查找树*/ private void buildBinarySearchTree(){ if(elements==null || elements.length==0) return; rootNode =new TreeNode(elements[0],null,null); if (elements.length==1) return; for (int index = 1; index < elements.length; index++) {//每次循环从数组中取一个元素 TreeNode tempNode = rootNode; while(true){//将这个元素与左子树或右子树循环比较,小于则继续进入左子树的左子树,否则继续进入右子树的右子树 if (elements[index].toString().compareTo(tempNode.nodeValue.toString())<0){//小于中间节点 if (tempNode.lChild==null){ tempNode.lChild = new TreeNode(elements[index],null,null); break; } tempNode = tempNode.lChild;//继续比较左子树 } else { if (tempNode.rChild==null){ tempNode.rChild = new TreeNode(elements[index],null,null); break; } tempNode = tempNode.rChild;//继续比较右子树 } } } } /**中序输出这个二叉查找树*/ @Override public void output(ISearchCallBack searchCallBack) { // TODO Auto-generated method stub if (rootNode==null) return; _output(rootNode,searchCallBack); } /**中序输出这个二叉查找树[递归函数]*/ private void _output(TreeNode node,ISearchCallBack searchCallBack){ if (node.lChild!=null)//遍历左子树 _output(node.lChild,searchCallBack); if (searchCallBack!=null)//访问根节点 searchCallBack.visit(node); if (node.rChild!=null)//遍历右子树 _output(node.rChild,searchCallBack); } @Override public TreeNode search(Object e) { // TODO Auto-generated method stub return null; } @Override public TreeNode search(Object e, ISearchCallBack searchCallBack) { // TODO Auto-generated method stub return null; } }

单元测试:

[java] view plain copy print ?
  1. package org.pbdevj.ds.search.binarysearchtree.utest;
  2. import org.junit.Before;
  3. import org.junit.Test;
  4. import org.pbdevj.ds.search.binarysearchtree.BinarySearchTree;
  5. import org.pbdevj.ds.search.binarysearchtree.ISearch;
  6. import org.pbdevj.ds.search.binarysearchtree.ISearchCallBack;
  7. import org.pbdevj.ds.search.binarysearchtree.TreeNode;
  8. public class BinarySearchTreeTest {
  9. @Test
  10. public void testOutput(){
  11. ISearch search = new BinarySearchTree(new Object[]{
  12. 50,30,10,20,60,90,70,45,43,32,38,44,21,20,55,65,67,76,77,88,77
  13. });
  14. search.output(new ISearchCallBack() {
  15. @Override
  16. public void visit(TreeNode e) {
  17. // TODO Auto-generated method stub
  18. System.out.println(e.nodeValue);
  19. }
  20. });
  21. }
  22. }

你可能感兴趣的:(sql)