线性表

插入

  1. public void insert(int i, Object x) throws Exception {
  2. if (curLen == listElem.length) // 判断顺序表是否已满
  3. throw new Exception("顺序表已满");// 输出异常
  4. if (i < 0 || i > curLen) // i小于0或者大于表长
  5. throw new Exception("插入位置不合理");// 输出异常
  6. for (int j = curLen; j > i; j--)
  7. listElem[j] = listElem[j - 1];// 插入位置及之后的元素后移
  8. listElem[i] = x; // 插入x
  9. curLen++;// 表长度增1
  10. }

删除

  1. public void remove(int i) throws Exception {
  2. if (i < 0 || i > curLen - 1) // i小于1或者大于表长减1
  3. throw new Exception("删除位置不合理");// 输出异常
  4. for (int j = i; j < curLen - 1; j++)
  5. listElem[j] = listElem[j + 1];// 被删除元素之后的元素左移
  6. curLen--; // 表长度减1
  7. }

查找

  1. public int indexOf(Object x) {
  2. int j = 0;// j为计数器
  3. while (j < curLen && !listElem[j].equals(x))
  4. // 从顺序表中的首结点开始查找,直到listElem[j]指向元素x或到达顺序表的表尾
  5. j++;
  6. if (j < curLen)// 判断j的位置是否位于表中
  7. return j; // 返回x元素在顺序表中的位置
  8. else
  9. return -1;// x元素不在顺序表中
  10. }

逆置

  1. public void reverse() {
  2. for (int i = 0,j=curLen-1; i < j; i++,j--) {
  3. Object temp = listElem[i];
  4. listElem[i] = listElem[j];
  5. listElem[j] = temp;
  6. }
  7. }

有序表插入

  1. void insert(int x) throws Exception {
  2. int i;
  3. if (n>MaxSize) //表满不能插入
  4. throw new Exception("Overflow");
  5. r[0]=x;
  6. for(i=n;r[i]>x;i--)
  7. r[i+1]=r[i];//将i位置元素后移
  8. r[i+1]=x;//在位置i+1插入元素x
  9. n++;//线性表长度增1
  10. }

单链表

获取

  1. public Object get(int i) throws Exception {
  2. Node p = head.next;// 初始化,p指向首结点,j为计数器
  3. int j = 0;
  4. while (p != null && j < i) {// 从首结点向后查找,直到p指向第i个元素或p为空
  5. p = p.next;// 指向后继结点
  6. ++j;// 计数器的值增1
  7. }
  8. if (j > i || p == null) { // i小于0或者大于表长减1
  9. throw new Exception("第" + i + "个元素不存在");// 输出异常
  10. }
  11. return p.data;// 返回元素p
  12. }

插入

  1. public void insert(int i, Object x) throws Exception {
  2. Node p = head;// 初始化p为头结点,j为计数器
  3. int j = -1; // 第i个结点前驱的位置
  4. while (p != null && j < i - 1) {// 寻找i个结点的前驱
  5. p = p.next;
  6. ++j;// 计数器的值增1
  7. }
  8. if (j > i - 1 || p == null) // i不合法
  9. throw new Exception("插入位置不合理");// 输出异常
  10. Node s = new Node(x); // 生成新结点
  11. s.next=p.next;// 插入单链表中
  12. p.next=s;
  13. }

删除

  1. public void remove(int i) throws Exception {
  2. Node p = head;// p指向要删除结点的前驱结点
  3. int j = -1;
  4. while (p.next != null && j < i - 1) {// 寻找i个结点的前驱
  5. p = p.next;
  6. ++j;// 计数器的值增1
  7. }
  8. if (j > i - 1 || p.next == null) { // i小于0或者大于表长减1
  9. throw new Exception("删除位置不合理");// 输出异常
  10. }
  11. p.next=p.next.next;// 删除结点
  12. }

查找

  1. public int indexOf(Object x) {
  2. Node p = head.next;// 初始化,p指向首结点,j为计数器
  3. int j = 0;
  4. while (p != null && !p.data.equals(x)) {// 从单链表中的首结点元素开始查找,直到p.data指向元素x或到达单链表的表尾
  5. p = p.next;// 指向下一个元素
  6. ++j;// 计数器的值增1
  7. }
  8. if (p != null)// 如果p指向表中的某一元素
  9. return j;// 返回x元素在顺序表中的位置
  10. else
  11. return -1;// x元素不在顺序表中
  12. }

逆置

  1. public void reverse() {
  2. if(head==null||head.next==null)
  3. return;
  4. Node pre = null;
  5. Node cur = null;
  6. Node next = null;
  7. cur = head.next;
  8. next = cur.next;
  9. cur.next = null;
  10. pre = cur;
  11. cur = next;
  12. while(cur.next != null){
  13. next = cur.next;
  14. cur.next = pre;
  15. pre = cur;
  16. cur = next;
  17. }
  18. cur.next = pre;
  19. head.next = cur;
  20. }

二叉链表

  1. class BiTree {
  2. private BiTreeNode root;// 树的根结点
  3. public BiTree() {// 构造一棵空树
  4. this.root = null;
  5. }
  6. public BiTree(BiTreeNode root) {// 构造一棵树
  7. this.root = root;
  8. }
  9. // 由先根遍历的数组和中根遍历的数组建立一棵二叉树
  10. // 其中参数preOrder是整棵树的 先根遍历,inOrder是整棵树的中根遍历,preIndex是
  11. // 先根遍历从preOrder字符串中的开始位置,inIndex是中根遍历从字符串inOrder中的开始位置,count表示树结点的个数
  12. public BiTree(String preOrder, String inOrder, int preIndex, int inIndex,
  13. int count) {
  14. if (count > 0) {// 先根和中根非空
  15. char r = preOrder.charAt(preIndex);// 取先根字符串中的第一个元素作为根结点
  16. int i = 0;
  17. for (; i < count; i++)
  18. // 寻找根结点在中根遍历字符串中的索引
  19. if (r == inOrder.charAt(i + inIndex))
  20. break;
  21. root = new BiTreeNode(r);// 建立树的根结点
  22. root.lchild=new BiTree(preOrder, inOrder, preIndex + 1, inIndex,
  23. i).root;// 建立树的左子树
  24. root.rchild=new BiTree(preOrder, inOrder, preIndex + i + 1,
  25. inIndex + i + 1, count - i - 1).root;// 建立树的右子树
  26. }
  27. }
  28. // 由标明空子树的先根遍历序列建立一棵二叉树
  29. private static int index = 0;// 用于记录preStr的索引值
  30. public BiTree(String[] preStr) {
  31. String c = preStr[index++];// 取出字符串索引为index的字符,且index增1
  32. if (!c.equals("#") ) {// 字符不为#
  33. root = new BiTreeNode(c);// 建立树的根结点
  34. root.lchild=new BiTree(preStr).root;// 建立树的左子树
  35. root.rchild=new BiTree(preStr).root;// 建立树的右子树
  36. } else
  37. root = null;
  38. }
  39. // 先根遍历二叉树基本操作的递归算法
  40. public void preRootTraverse(BiTreeNode T) {
  41. if (T != null) {
  42. System.out.print(T.data); // 访问根结点
  43. preRootTraverse(T.lchild);// 访问左子树
  44. preRootTraverse(T.rchild);// 访问右子树
  45. }
  46. }
  47. // 先根遍历二叉树基本操作的非递归算法
  48. public void preRootTraverse() {
  49. BiTreeNode T = root;
  50. if (T != null) {
  51. LinkStack S = new LinkStack();// 构造栈
  52. S.push(T);// 根结点入栈
  53. while (!S.isEmpty()) {
  54. T = (BiTreeNode) S.pop();// 移除栈顶结点,并返回其值
  55. System.out.print(T.data +" "); // 访问结点
  56. while (T != null) {
  57. if (T.lchild != null) // 访问左孩子
  58. System.out.print(T.lchild.data +" "); // 访问结点
  59. if (T.rchild != null)// 右孩子非空入栈
  60. S.push(T.rchild);
  61. T = T.lchild;
  62. }
  63. }
  64. }
  65. }
  66. // 中根遍历二叉树基本操作的递归算法
  67. public void inRootTraverse(BiTreeNode T) {
  68. if (T != null) {
  69. inRootTraverse(T.lchild);// 访问左子树
  70. System.out.print(T.data); // 访问根结点
  71. inRootTraverse(T.rchild);// 访问右子树
  72. }
  73. }
  74. // 中根遍历二叉树基本操作的非递归算法
  75. public void inRootTraverse() {
  76. BiTreeNode T = root;
  77. if (T != null) {
  78. LinkStack S = new LinkStack();// 构造链栈
  79. S.push(T); // 根结点入栈
  80. while (!S.isEmpty()) {
  81. while (S.peek() != null)
  82. // 将栈顶结点的所有左孩子结点入栈
  83. S.push(((BiTreeNode) S.peek()).lchild);
  84. S.pop(); // 空结点退栈
  85. if (!S.isEmpty()) {
  86. T = (BiTreeNode) S.pop();// 移除栈顶结点,并返回其值
  87. System.out.print(T.data +" "); // 访问结点
  88. S.push(T.rchild);// 结点的右孩子入栈
  89. }
  90. }
  91. }
  92. }
  93. // 后根遍历二叉树基本操作的递归算法
  94. public void postRootTraverse(BiTreeNode T) {
  95. if (T != null) {
  96. postRootTraverse(T.lchild);// 访问左子树
  97. postRootTraverse(T.rchild);// 访问右子树
  98. System.out.print(T.data); // 访问根结点
  99. }
  100. }
  101. // 后根遍历二叉树基本操作的非递归算法
  102. public void postRootTraverse() {
  103. BiTreeNode T = root;
  104. if (T != null) {
  105. LinkStack S = new LinkStack();// 构造链栈
  106. S.push(T); // 根结点进栈
  107. Boolean flag;// 访问标记
  108. BiTreeNode p = null;// p指向刚被访问的结点
  109. while (!S.isEmpty()) {
  110. while (S.peek() != null)
  111. // 将栈顶结点的所有左孩子结点入栈
  112. S.push(((BiTreeNode) S.peek()).lchild);
  113. S.pop(); // 空结点退栈
  114. while (!S.isEmpty()) {
  115. T = (BiTreeNode) S.peek();// 查看栈顶元素
  116. if (T.rchild == null || T.rchild == p) {
  117. System.out.print(T.data +" "); // 访问结点
  118. S.pop();// 移除栈顶元素
  119. p = T;// p指向刚被访问的结点
  120. flag = true;// 设置访问标记
  121. } else {
  122. S.push(T.rchild);// 右孩子结点入栈
  123. flag = false;// 设置未被访问标记
  124. }
  125. if (!flag)
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. // 层次遍历二叉树基本操作的算法(自左向右)
  132. public void levelTraverse() {
  133. BiTreeNode T = root;
  134. if (T != null) {
  135. LinkQueue L = new LinkQueue();// 构造队列
  136. L.offer(T);// 根结点入队列
  137. while (!L.isEmpty()) {
  138. T = (BiTreeNode) L.poll();
  139. System.out.print(T.data); // 访问结点
  140. if (T.lchild != null)// 左孩子非空,入队列
  141. L.offer(T.lchild);
  142. if (T.rchild != null)// 右孩子非空,入队列
  143. L.offer(T.rchild);
  144. }
  145. }
  146. }
  147. public BiTreeNode getRoot() {
  148. return root;
  149. }
  150. public void setRoot(BiTreeNode root) {
  151. this.root = root;
  152. }
  153. // 统计叶结点数目
  154. public int countLeafNode(BiTreeNode T) {
  155. int count = 0;
  156. if (T != null) {
  157. if (T.lchild == null && T.rchild == null) {
  158. ++count;// 叶结点个数加1
  159. } else {
  160. count += countLeafNode(T.lchild); // 加上左子树上叶结点的个数
  161. count += countLeafNode(T.rchild);// 加上右子树上叶结点的个数
  162. }
  163. }
  164. return count;
  165. }
  166. // 统计结点的数目
  167. public int countNode(BiTreeNode T) {
  168. int count = 0;
  169. if (T != null) {
  170. ++count;// 结点的个数加1
  171. count += countNode(T.lchild); // 加上左子树上结点的个数
  172. count += countNode(T.rchild);// 加上右子树上结点的个数
  173. }
  174. return count;
  175. }
  176. public int getDepth(BiTreeNode T) {
  177. if (T != null) {
  178. int lDepth = getDepth(T.lchild);// 左子树的深度
  179. int rDepth = getDepth(T.rchild);// 右子树的深度
  180. return 1 + (lDepth > rDepth ? lDepth : rDepth);// 返回左子树的深度和右子树的深度中的最大值加1
  181. }
  182. return 0;
  183. }
  184. // 采用递归模型方法,计算二叉树的深度
  185. public int getDepth1(BiTreeNode T) {
  186. if (T==null)
  187. return 0;
  188. else if (T.lchild==null&&T.rchild==null)
  189. return 1;
  190. else
  191. return 1 + (getDepth1(T.lchild)> getDepth1(T.rchild)? getDepth1(T.lchild) : getDepth1(T.rchild));
  192. }
  193. }

图的邻接矩阵

  1. class MGraph {
  2. private int vexNum, arcNum;
  3. private Object[] vexs;
  4. private int[][] arcs;
  5. private static boolean[] visited;
  6. public MGraph() {
  7. this(0, 0, null, null);
  8. }
  9. public MGraph(int vexNum, int arcNum, Object[] vexs,
  10. int[][] arcs) {
  11. this.vexNum = vexNum;
  12. this.arcNum = arcNum;
  13. this.vexs = vexs;
  14. this.arcs = arcs;
  15. }
  16. public void createGraph(String[] str, int n, int e, int[][] arc){
  17. vexNum = n;
  18. arcNum = e;
  19. vexs = new Object[vexNum];
  20. this.vexs = str;
  21. arcs = new int[vexNum][vexNum];
  22. for (int v = 0; v < vexNum; v++)
  23. for (int u = 0; u < vexNum; u++)
  24. arcs[v][u] = 0;
  25. for (int k = 0; k < arcNum; k++) {
  26. int v = arc[k][0];
  27. int u = arc[k][1];
  28. arcs[v][u] = arcs[u][v] = 1;
  29. }
  30. }
  31. public int getVexNum() {
  32. return vexNum;
  33. }
  34. public int getArcNum() {
  35. return arcNum;
  36. }
  37. public void setArcNum(int arcNum) {
  38. this.arcNum = arcNum;
  39. }
  40. public void setArcs(int[][] arcs) {
  41. this.arcs = arcs;
  42. }
  43. public void setVexNum(int vexNum) {
  44. this.vexNum = vexNum;
  45. }
  46. public void setVexs(Object[] vexs) {
  47. this.vexs = vexs;
  48. }
  49. public int[][] getArcs() {
  50. return arcs;
  51. }
  52. public Object[] getVexs() {
  53. return vexs;
  54. }
  55. public int locateVex(Object vex) {
  56. for (int v = 0; v < vexNum; v++)
  57. if (vexs[v].equals(vex))
  58. return v;
  59. return -1;
  60. }
  61. public Object getVex(int v) throws Exception {
  62. if (v < 0 && v >= vexNum)
  63. throw new Exception("no position");
  64. return vexs[v];
  65. }
  66. public int firstAdjVex(int v) throws Exception {
  67. if (v < 0 && v >= vexNum)
  68. throw new Exception("!");
  69. for (int j = 0; j < vexNum; j++)
  70. if (arcs[v][j] != 0 && arcs[v][j] < 9999)
  71. return j;
  72. return -1;
  73. }
  74. public int nextAdjVex(int v, int w) throws Exception {
  75. if (v < 0 && v >= vexNum)
  76. throw new Exception("no position");
  77. for (int j = w + 1; j < vexNum; j++)
  78. if (arcs[v][j] != 0 && arcs[v][j] < 9999)
  79. return j;
  80. return -1;
  81. }
  82. public void DFSTraverse(MGraph G) throws Exception {
  83. visited = new boolean[G.getVexNum()];
  84. for (int v = 0; v < G.getVexNum(); v++)
  85. visited[v] = false;
  86. for (int v = 0; v < G.getVexNum(); v++)
  87. if (!visited[v])
  88. DFS(G, v);
  89. }
  90. private void DFS(MGraph G, int v) throws Exception {
  91. visited[v] = true;
  92. System.out.print(G.getVex(v).toString() + " ");
  93. for (int w = G.firstAdjVex(v); w >= 0; w = G.nextAdjVex(v, w))
  94. if (!visited[w])
  95. DFS(G, w);
  96. }
  97. }
  98. public class Main {
  99. public static Scanner sc;
  100. public static void main(String[] args) throws Exception {
  101. Scanner sc = new Scanner(System.in);
  102. String a[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
  103. "L", "M", "N" };
  104. int i, n, e, j;
  105. n = sc.nextInt();
  106. e = sc.nextInt();
  107. int[][] arc = new int[e][2];
  108. for (i = 0; i < e; i++)
  109. for (j = 0; j < 2; j++)
  110. arc[i][j] = sc.nextInt();
  111. MGraph G = new MGraph();
  112. G.createGraph(a, n, e, arc);
  113. System.out.print("DFS:");
  114. G.DFSTraverse(G);
  115. System.out.println();
  116. }
  117. }

邻接表

  1. class ALGraph implements IGraph {
  2. private int vexNum, arcNum;
  3. private VNode[] vexs;
  4. private static boolean[] visited;// 访问标志数组
  5. public ALGraph() {
  6. this(0, 0, null);
  7. }
  8. public ALGraph(int vexNum, int arcNum, VNode[] vexs) {
  9. this.vexNum = vexNum;
  10. this.arcNum = arcNum;
  11. this.vexs = vexs;
  12. }
  13. public void createGraph() {
  14. Scanner sc = new Scanner(System.in);
  15. vexNum = sc.nextInt();
  16. arcNum = sc.nextInt();
  17. vexs = new VNode[vexNum];
  18. String Node[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N"}; //顶点信息
  19. for (int v = 0; v < vexNum; v++)
  20. vexs[v] = new VNode(Node[v]);
  21. for (int k = 0; k < arcNum; k++) {
  22. int v = sc.nextInt();// 弧尾
  23. int u = sc.nextInt();// 弧头
  24. int value = 1;
  25. addArc(v, u, value);
  26. addArc(u, v, value);
  27. }
  28. }
  29. public void addArc(int v, int u, int value) {
  30. ArcNode arc = new ArcNode(u, value);
  31. arc.nextArc=vexs[v].firstArc;
  32. vexs[v].firstArc=arc;
  33. }
  34. public int getVexNum() {
  35. return vexNum;
  36. }
  37. public int getArcNum() {
  38. return arcNum;
  39. }
  40. public int locateVex(Object vex) {
  41. for (int v = 0; v < vexNum; v++)
  42. if (vexs[v].data.equals(vex))
  43. return v;
  44. return -1;
  45. }
  46. public VNode[] getVexs() {
  47. return vexs;
  48. }
  49. public Object getVex(int v) throws Exception {
  50. if (v < 0 && v >= vexNum)
  51. throw new Exception("第" + v + "个顶点不存在!");
  52. return vexs[v].data;
  53. }
  54. public int firstAdjVex(int v) throws Exception {
  55. if (v < 0 && v >= vexNum)
  56. throw new Exception("第" + v + "个顶点不存在!");
  57. VNode vex = vexs[v];
  58. if (vex.firstArc != null)
  59. return vex.firstArc.adjVex;
  60. else
  61. return -1;
  62. }
  63. public int nextAdjVex(int v, int w) throws Exception {
  64. if (v < 0 && v >= vexNum)
  65. throw new Exception("第" + v + "个顶点不存在!");
  66. VNode vex = vexs[v];
  67. ArcNode arcvw = null;
  68. for (ArcNode arc = vex.firstArc; arc != null; arc = arc.nextArc)
  69. if (arc.adjVex == w) {
  70. arcvw = arc;
  71. break;
  72. }
  73. if (arcvw != null && arcvw.nextArc != null)
  74. return arcvw.nextArc.adjVex;
  75. else
  76. return -1;
  77. }
  78. public void setArcNum(int arcNum) {
  79. this.arcNum = arcNum;
  80. }
  81. public void setVexNum(int vexNum) {
  82. this.vexNum = vexNum;
  83. }
  84. public void setVexs(VNode[] vexs) {
  85. this.vexs = vexs;
  86. }
  87. public void DFSTraverse() throws Exception {
  88. visited = new boolean[getVexNum()];
  89. for (int v = 0; v < getVexNum(); v++)
  90. // 访问标志数组初始化
  91. visited[v] = false;
  92. System.out.print("DFS:");
  93. for (int v = 0; v < getVexNum(); v++)
  94. if (!visited[v])// 对尚未访问的顶点调用DFS
  95. DFS(v);
  96. }
  97. private void DFS(int v) throws Exception {
  98. visited[v] = true;
  99. // 访问第v个顶点
  100. System.out.print(getVex(v).toString() + " ");
  101. for (int w = firstAdjVex(v); w >= 0; w = nextAdjVex(v, w))
  102. if (!visited[w])// 对v的尚未访问的邻接顶点w递归调用DFS
  103. DFS(w);
  104. }
  105. public void BFSTraverse() throws Exception {
  106. visited = new boolean[getVexNum()];// 访问标志数组
  107. for (int v = 0; v < getVexNum(); v++)
  108. // 访问标志数组初始化
  109. visited[v] = false;
  110. System.out.print("BFS:");
  111. for (int v = 0; v < getVexNum(); v++)
  112. if (!visited[v]) // v尚未访问
  113. BFS(v);
  114. }
  115. private void BFS(int v) throws Exception {
  116. visited[v] = true;
  117. System.out.print(getVex(v).toString() + " ");
  118. CirQueue Q = new CirQueue();// 辅助队列Q
  119. Q.EnQueue(v);// v入队列
  120. while (!Q.isEmpty()) {
  121. int u = (Integer) Q.DeQueue();// 队头元素出队列并赋值给u
  122. for (int w = firstAdjVex(u); w >= 0; w = nextAdjVex(u, w))
  123. if (!visited[w]) {// w为u的尚未访问的邻接顶点
  124. visited[w] = true;
  125. System.out.print(getVex(w).toString() + " ");
  126. Q.EnQueue(w);
  127. }
  128. }
  129. }
  130. void DispMGraph(){
  131. int i;
  132. ArcNode p;
  133. System.out.println("Graph adjlist:");
  134. for(i=0;i<vexNum;i++){
  135. System.out.print(i + " " +vexs[i].data + " ");
  136. p = vexs[i].firstArc;
  137. while(p!=null){
  138. System.out.print(p.adjVex + " ");
  139. p= p.nextArc;
  140. }
  141. System.out.println("");
  142. }
  143. }
  144. }

折半查找

插入

  1. oid insert(int x) throws Exception {
  2. int i;
  3. if (n>MaxSize) //表满不能插入
  4. throw new Exception("Overflow");
  5. r[0]=x;
  6. for(i=n;r[i]>x;i--)
  7. r[i+1]=r[i];//将i位置元素后移
  8. r[i+1]=x;//在位置i+1插入元素x
  9. n++;//线性表长度增1
  10. }

查找

  1. int Bin_Search(int key){
  2. if(n>0){
  3. int low = 0,high = n;
  4. while(low<=high){
  5. int mid = (low+high)/2;
  6. if(r[mid]==key){
  7. return mid;
  8. }else if(r[mid]>key){
  9. high = mid - 1;
  10. }else{
  11. low = mid + 1;
  12. }
  13. }
  14. }
  15. return -1;
  16. }

查找(递归)

  1. int Bin_Search(int key){
  2. return Bin_Search(1,n,key);
  3. }
  4. int Bin_Search(int low,int high,int key){
  5. if(low>high)
  6. return -1;
  7. int mid = (low + high) >> 1;
  8. if(r[mid] > key)
  9. return Bin_Search(low, mid - 1, key);
  10. else if(r[mid] < key)
  11. return Bin_Search(mid + 1, high, key);
  12. return mid;
  13. }

二叉排序树(1)-递归方式

  1. public class BSTree { //二叉排序树类
  2. public BiTreeNode root; //根结点
  3. public BSTree() { //构造空二叉排序树
  4. root = null;
  5. }
  6. /*
  7. public BiTreeNode getRoot() {
  8. return root;
  9. }
  10. public void setRoot(BiTreeNode root) {
  11. this.root = root;
  12. }
  13. */
  14. public boolean isEmpty() { //判断是否空二叉树
  15. return this.root == null;
  16. }
  17. public void inOrderTraverse(BiTreeNode p) { //中根次序遍历以p结点为根的二叉树
  18. if (p != null) {
  19. inOrderTraverse(p.lchild);
  20. System.out.print(((RecordNode) p.data).toString() + "");
  21. inOrderTraverse(p.rchild);
  22. }
  23. }
  24. //查找关键字值为key的结点,若查找成功返回结点值,否则返回null
  25. public Object searchBST(Comparable key) {
  26. if (key == null || !(key instanceof Comparable)) {
  27. return null;
  28. }
  29. return searchBST(root, key);
  30. }
  31. //二叉排序树查找的递归算法
  32. //在二叉排序树中查找关键字为key的结点。若查找成功则返回结点值,否则返回null
  33. private Object searchBST(BiTreeNode p, Comparable key) {
  34. if (p != null) {
  35. if (key.compareTo(((RecordNode) p.data).key) == 0) //查找成功
  36. {
  37. return p.data;
  38. }
  39. // System.out.print(((RecordNode) p.data).key + "? ");
  40. if (key.compareTo(((RecordNode) p.data).key) < 0) {
  41. return searchBST(p.lchild, key); //在左子树中查找
  42. } else {
  43. return searchBST(p.rchild, key); //在右子树中查找
  44. }
  45. }
  46. return null;
  47. }
  48. //在二叉排序树中插入关键字为Key,数据项为theElement的结点,若插入成功返回true,否则返回false
  49. public boolean insertBST(Comparable key, Object theElement) {
  50. if (key == null || !(key instanceof Comparable)) {//不能插入空对象或不可比较大小的对象
  51. return false;
  52. }
  53. if (root == null) {
  54. root = new BiTreeNode(new RecordNode(key, theElement)); //建立根结点
  55. return true;
  56. }
  57. return insertBST(root, key, theElement);
  58. }
  59. //将关键字为key,数据项为theElement的结点插入到以p为根的二叉排序树中的递归算法
  60. private boolean insertBST(BiTreeNode p, Comparable key, Object theElement) {
  61. if (key.compareTo(((RecordNode) p.data).key) == 0) {
  62. return false; //不插入关键字重复的结点
  63. }
  64. if (key.compareTo(((RecordNode) p.data).key) < 0) {
  65. if (p.lchild == null) { //若p的左子树为空
  66. p.lchild=new BiTreeNode(new RecordNode(key, theElement)); //建立叶子结点作为p的左孩子
  67. return true;
  68. } else { //若p的左子树非空
  69. return insertBST(p.lchild, key, theElement); //插入到p的左子树中
  70. }
  71. } else if (p.rchild == null) { //若p的右子树为空
  72. p.rchild=new BiTreeNode(new RecordNode(key, theElement)); //建立叶子结点作为p的右孩子
  73. return true;
  74. } else { //若p的右子树非空
  75. return insertBST(p.rchild, key, theElement); //插入到p的右子树中
  76. }
  77. }
  78. //二叉排序树中删除结点算法。若删除成功返回删除结点值,否则返回null
  79. public Object removeBST(Comparable key) {
  80. if (root == null || key == null || !(key instanceof Comparable)) {
  81. return null;
  82. }
  83. //在以root为根的二叉排序树中删除关键字为elemKey的结点
  84. return removeBST(root, key, null);
  85. }
  86. //在以p为根的二叉排序树中删除关键字为elemKey的结点。parent是p的父结点,递归算法
  87. private Object removeBST(BiTreeNode p, Comparable elemKey, BiTreeNode parent) {
  88. if (p != null) {
  89. if (elemKey.compareTo(((RecordNode) p.data).key) < 0) { //在左子树中删除
  90. return removeBST(p.lchild, elemKey, p); //在左子树中递归搜索
  91. } else if (elemKey.compareTo(((RecordNode) p.data).key) > 0) { //在右子树中删除
  92. return removeBST(p.rchild, elemKey, p); //在右子树中递归搜索
  93. } else if (p.lchild != null && p.rchild != null) {
  94. //相等且该结点有左右子树
  95. BiTreeNode innext = p.rchild; //寻找p在中根次序下的后继结点innext
  96. while (innext.lchild != null) { //即寻找右子树中的最左孩子
  97. innext = innext.lchild;
  98. }
  99. p.data=innext.data; //以后继结点值替换p
  100. return removeBST(p.rchild, ((RecordNode) p.data).key, p); //递归删除结点p
  101. } else {//p是1度和叶子结点
  102. if (parent == null) { //删除根结点,即p==root
  103. if (p.lchild != null) {
  104. root = p.lchild;
  105. } else {
  106. root = p.rchild;
  107. }
  108. return p.data; //返回删除结点p值
  109. }
  110. if (p == parent.lchild) { //p是parent的左孩子
  111. if (p.lchild != null) {
  112. parent.lchild=p.lchild; //以p的左子树填补
  113. } else {
  114. parent.lchild=p.rchild;
  115. }
  116. } else if (p.lchild != null) { //p是parent的右孩子且p的左子树非空
  117. parent.rchild=p.lchild;
  118. } else {
  119. parent.rchild=p.rchild;
  120. }
  121. return p.data;
  122. }
  123. }
  124. return null;
  125. }
  126. }

排序

不带监视哨的直接插入排序算法

  1. public void insertSort() {
  2. RecordNode temp;
  3. int i, j;
  4. for (i = 1; i < this.curlen; i++) {//n-1趟扫描
  5. temp = r[i]; //将待插入的第i条记录暂存在temp中
  6. for (j = i - 1; j >= 0 && temp.key.compareTo(r[j].key) < 0; j--) { //将前面比r[i]大的记录向后移动
  7. r[j + 1] = r[j];
  8. }
  9. r[j + 1] = temp; //r[i]插入到第j+1个位置
  10. display();
  11. }
  12. }

带监视哨的直接插入排序算法

  1. public void insertSortWithGuard() {
  2. int i, j;
  3. for (i = 2; i <this.curlen; i++) { //n-1趟扫描
  4. r[0] = r[i]; //将待插入的第i条记录暂存在r[0]中,同时r[0]为监视哨
  5. for (j = i - 1; r[0].key.compareTo(r[j].key) < 0; j--) { //将前面较大元素向后移动
  6. r[j + 1] = r[j];
  7. }
  8. r[j + 1] = r[0]; // r[i]插入到第j+1个位置
  9. display();
  10. }
  11. }

冒泡排序算法

  1. public void bubbleSort() {
  2. // System.out.println("冒泡排序");
  3. RecordNode temp; //辅助结点
  4. boolean flag = true; //是否交换的标记
  5. for (int i = 1; i < this.curlen && flag; i++) { //有交换时再进行下一趟,最多n-1趟
  6. flag = false; //假定元素未交换
  7. for (int j = 0; j < this.curlen - i; j++) { //一次比较、交换
  8. if (r[j].key.compareTo(r[j + 1].key) > 0) { //逆序时,交换
  9. temp = r[j];
  10. r[j] = r[j + 1];
  11. r[j + 1] = temp;
  12. flag = true;
  13. }
  14. }
  15. display();
  16. }
  17. }

快速排序算法

  1. //交换排序表r[i..j]的记录,使支点记录到位,并返回其所在位置
  2. //此时,在支点之前(后)的记录关键字均不大于(小于)它
  3. public int Partition(int i, int j) {
  4. RecordNode pivot = r[i]; //第一个记录作为支点记录
  5. // System.out.print(i + ".." + j + ", pivot=" + pivot.key + " ");
  6. while (i < j) { //从表的两端交替地向中间扫描
  7. while (i < j && pivot.key.compareTo(r[j].key) <= 0) {
  8. j--;
  9. }
  10. if (i < j) {
  11. r[i] = r[j]; //将比支点记录关键字小的记录向前移动
  12. i++;
  13. }
  14. while (i < j && pivot.key.compareTo(r[i].key) > 0) {
  15. i++;
  16. }
  17. if (i < j) {
  18. r[j] = r[i]; //将比支点记录关键字大的记录向后移动
  19. j--;
  20. }
  21. }
  22. r[i] = pivot; //支点记录到位
  23. // display();
  24. return i; //返回支点位置
  25. }
  26. //对子表r[low..high]快速排序
  27. public void qSort(int low, int high) {
  28. if (low < high) {
  29. int pivotloc = Partition(low, high); //一趟排序,将排序表分为两部分
  30. qSort(low, pivotloc - 1); //低子表递归排序
  31. qSort(pivotloc + 1, high); //高子表递归排序
  32. }
  33. }
  34. public void quickSort() {
  35. qSort(0, this.curlen - 1);
  36. }

直接选择排序

  1. public void selectSort() {
  2. RecordNode temp; //辅助结点
  3. for (int i = 0; i < this.curlen - 1; i++) {//n-1趟排序
  4. //每趟在从r[i]开始的子序列中寻找最小元素
  5. int min = i; //设第i条记录的关键字最小
  6. for (int j = i + 1; j < this.curlen; j++) {//在子序列中选择关键字最小的记录
  7. if (r[j].key.compareTo(r[min].key) < 0) {
  8. min = j; //记住关键字最小记录的下标
  9. }
  10. }
  11. if (min != i) { //将本趟关键字最小的记录与第i条记录交换
  12. temp = r[i];
  13. r[i] = r[min];
  14. r[min] = temp;
  15. }
  16. }
  17. }

扩展

散列表

  1. class HashList{
  2. int MaxSize = 11;
  3. int[] ht = new int[MaxSize];
  4. int HashFunc(int k) {
  5. return k%MaxSize;
  6. }
  7. public HashList() {
  8. for(int i=0;i<MaxSize;i++)
  9. ht[i]=-1;
  10. }
  11. void Display(){
  12. for (int i=0;i<MaxSize;i++)
  13. System.out.print(ht[i]+" ");
  14. System.out.println();
  15. }
  16. int HashSearch(int k) throws Exception {
  17. int j = HashFunc(k);
  18. if (ht[j] == k)
  19. return 1;
  20. else if (ht[j] == -1)
  21. ht[j] = k;
  22. else {
  23. int i = (j + 1) % MaxSize;
  24. int cnt = 1;
  25. while (ht[i] != -1 && i != j) {
  26. cnt++;
  27. if (ht[i] == k) {
  28. return cnt;
  29. } else
  30. i = (i + 1) % MaxSize;
  31. }
  32. if(i == j) throw new Exception("Overflow");
  33. else {
  34. ht[i] = k;
  35. }
  36. }
  37. return -1;
  38. }
  39. double HashASL() throws Exception {
  40. double ASL=0;
  41. int n=0;
  42. for(int i=0;i<MaxSize;i++)
  43. if(ht[i]!=-1) {
  44. ASL+=HashSearch(ht[i]);
  45. System.out.println(ht[i] + " " + HashSearch(ht[i]));
  46. n++;
  47. }
  48. return (double)Math.round(ASL/n*100000)/100000;
  49. }
  50. }
  51. class LinkHash{
  52. int MaxSize = 11;
  53. Node[] ht = new Node[MaxSize];
  54. public LinkHash() {
  55. for (int i = 0; i < MaxSize; i++)
  56. ht[i] = null; //NULL is empty
  57. }
  58. void Display() {
  59. for (int i = 0; i < MaxSize; i++) {
  60. System.out.print("Hash address:" + i + ",value:");
  61. for (Node p = ht[i]; p != null; p = p.next)
  62. System.out.print(p.data + " ");
  63. System.out.println();
  64. }
  65. }
  66. int HashFunc(int k){
  67. return k % 11; //hash函数,假设为除余法,余数为11
  68. }
  69. int HashSearch(int k){
  70. int j = HashFunc(k);
  71. Node p = ht[j];
  72. while(p!=null && p.data != k) {
  73. p = p.next;
  74. }
  75. if(p!=null && p.data == k) return 1;
  76. else {
  77. Node s = new Node();
  78. s.data = k;
  79. s.next = ht[j];
  80. ht[j] = s;
  81. }
  82. return 0;
  83. }
  84. }

  1. class SeqStack{
  2. int stackMax = 5;
  3. Object[] data;
  4. int top = 0;
  5. public SeqStack() {
  6. data = new Object[stackMax];
  7. top = 0;
  8. }
  9. public void push(Object x) throws Exception {
  10. if(top==stackMax){
  11. throw new Exception("Overflow");
  12. }else{
  13. data[top++] = x;
  14. }
  15. }
  16. public Object pop() throws Exception {
  17. if(top==0){
  18. throw new Exception("Downflow");
  19. }else {
  20. return data[--top];
  21. }
  22. }
  23. public Object getTop() throws Exception {
  24. if(top==0){
  25. throw new Exception("Downflow");
  26. }else {
  27. return data[top - 1];
  28. }
  29. }
  30. public boolean isEmpty(){
  31. return top==0;
  32. }
  33. }
  34. class LinkStack{
  35. Node top;
  36. public LinkStack() {
  37. top = null;
  38. }
  39. public void push(Object x){
  40. top = new Node(x,top);
  41. }
  42. public Object pop() throws Exception {
  43. if(isEmpty()){
  44. throw new Exception("Downflow");
  45. }else {
  46. Node p = top;
  47. top = top.next;
  48. return p.data;
  49. }
  50. }
  51. public Object getTop() throws Exception {
  52. if(isEmpty()){
  53. throw new Exception("Downflow");
  54. }else {
  55. return top.data;
  56. }
  57. }
  58. public boolean isEmpty(){
  59. return top==null;
  60. }
  61. }

队列

  1. class LinkQueue{
  2. Node front;
  3. Node rear;
  4. public LinkQueue(){
  5. rear = front = null;
  6. }
  7. void EnQueue(Object data){
  8. Node p =new Node(data);
  9. if(front != null){
  10. rear.next = p;
  11. rear = p;
  12. }else{
  13. front = rear = p;
  14. }
  15. }
  16. Object DeQueue() throws Exception{
  17. if(front != null){
  18. Node p = front;
  19. front = front.next;
  20. if(p==rear){
  21. rear = null;
  22. }
  23. return p.data;
  24. }else{
  25. throw new Exception("Downflow");
  26. }
  27. }
  28. Object GetQueue() throws Exception{
  29. if(front != null){
  30. return front.data;
  31. }else{
  32. throw new Exception("Downflow");
  33. }
  34. }
  35. boolean isEmpty(){
  36. return front == null;
  37. }
  38. }
  39. class CirQueue{
  40. Object[] queueElem;
  41. int front;
  42. int rear;
  43. int QueueSize=5;
  44. public CirQueue(){
  45. front = rear = 0;
  46. queueElem = new Object[QueueSize];
  47. }
  48. public void clear(){
  49. front = rear = 0;
  50. }
  51. public boolean isEmpty(){
  52. return front == rear;
  53. }
  54. public int length(){
  55. return (rear-front+queueElem.length)% queueElem.length;
  56. }
  57. public boolean isFull(){
  58. return (rear+1) % QueueSize ==front;
  59. }
  60. public void EnQueue(Object x) throws Exception{
  61. if((rear+1)%queueElem.length==front){
  62. throw new Exception(" Overflow");
  63. }else{
  64. queueElem[rear] = x;
  65. rear = (rear+1)%queueElem.length;
  66. }
  67. }
  68. public Object DeQueue() throws Exception {
  69. if(front==rear){
  70. throw new Exception("Downflow");
  71. }else{
  72. Object t = queueElem[front];
  73. front = (front+1)%queueElem.length;
  74. return t;
  75. }
  76. }
  77. public Object GetQueue() throws Exception {
  78. if(front==rear){
  79. throw new Exception("Downflow");
  80. }else{
  81. Object t = queueElem[front];
  82. return t;
  83. }
  84. }
  85. }