算法 3.2节 二叉查找树的实现
2018-03-11 13:56
162 查看
根据书上的代码,用了递归的方法实现二叉查找树的各项功能,很多地方比较难理解,费了不少功夫
import java.util.Iterator; import java.util.Stack; public class BST { private Node root; private class Node { private int key; private int value; private Node left; private Node right; private int count; public Node(int key, int value, int count) { this.key = key; this.value= value; this.count = count; } } public BST() { root.count = 1; root.left = null; root.right = null; } public int size() { return size(root); } private int size(Node x) { return x == null ? 0 : x.count; } public int get(int key) { return get(root, key); } private int get(Node x, int key) { if (x == null) return -1; else if (x.key == key) return x.value; else if (x.key > key) return get(x.left, key); else if (x.key < key) return get(x.right,key); } public void insert(int key, int value) { root = insert(key, value, root); } private Node insert(int key, int value, Node x) { if (x == null) return new Node(key, value, 1); else if (x.key > key) x.left = insert(key, value, x.left); else if (x.key < key) x.right = insert(key,value,x.right); else x.value = value; x.count =size(x.left) + size(x.right) +1; return x; } private void insert_without_recursion(int key, int value) { Node temp = new Node(key,value,1); if (root == null) root = temp; Node parent =null,t= root; while (t != null) { parent = t; if (t.key < key) t= t.right; if (t.key > key) t=t.left; if (t.key == key) { t.value = value; // t的类型不是基础类型,所以是传递引用而不是值,相当于指针 return; } } boolean cmp = parent.key > key; if (cmp) parent.left = temp; else parent.right = temp; } public int min() { return min(root).value; } private Node min(Node x) { if(x.left == null) return x; else return min(x.left); } public Node floor(int key) { return floor(key,root); } private Node floor(int key, Node x) { if (x == null) return null; if (x.key == key) return x; else if (x.key > key) { return floor(key, x.left); } else { Node t = floor(key, x.right); if (t != null) return t; else return x; } } public Node select_by_rank(int k) { return select_by_rank(k,root); } private Node select_by_rank(int k,Node x) { //有k个小于这个键的键 if (x == null) return null; int cmp = size(x.left); if (cmp == k) return x; else if (cmp > k ) return select_by_rank(k,x.left); else return select_by_rank(k - cmp -1,x.right); } public void delectMin() { root= delectMin(root); } private Node delectMin(Node x) { if (x.left == null) return x.right; else { x.left = delectMin(x.left); x.count = size(x.left) +size(x.right) +1; return x; } } public void delect(int key) { root = delect(key, root); } private Node delect(int key, Node x) { if (x.key > key) x.left = delect(key, x.left); else if (x.key < key) { x.right = delect(key, x.right); } else { if (x.left == null) return x.right; if (x.right == null) return x.left; Node t = x; x= min(t.right); x.right = delectMin(t.right); x.left = t.left; } x.count = size(x.left) + size(x.right) +1; return x; } public Iterable<Integer> keys(int low, int high) { Stack<Integer> stack = new Stack<>() ; keys(low, high, stack, root); return stack; } public Iterable<Integer> traverse_without_recursion() { Stack<Node> stack = new Stack<Node>(); Stack<Integer> queue = new Stack<Integer>(); Node x = root; while (x != null || !stack.isEmpty()) { if (x != null) { stack.push(x); x = x.left; } else { x = stack.pop(); queue.push(x.key); x = x.right; } } return queue; } private void keys(int low, int high, Stack stack, Node x) { if (x == null) return; if (x.key > low) keys(low, high, stack, x.left); if (x.key >= low && x.key <= high) stack.push(x.key); if (x.key < high) keys(low, high, stack, x.right); } public int height() { return height(root); } private int height(Node x) { if (x == null) return -1; int a = height(x.left); int b = height(x.right); return 1+ a>b ? a : b; } }
相关文章推荐
- 算法学习 - 平衡二叉查找树实现(AVL树)
- 数据结构C#实现-二叉查找树的创建,查找,以及各种递归(非递归)遍历算法
- js代码实现二叉查找树的算法
- 二分查找,二叉查找树(二叉排序树)的基本思想以及算法实现
- 【算法导论】二叉查找树的操作C++实现
- 二叉查找树——算法导论第12章简易代码实现~
- 算法导论——(2)二叉查找树的实现
- [算法导论]二叉查找树的实现 @ Python
- 二叉查找树相关算法实现(算法导论12章)
- 算法练习之二叉查找树 C++实现
- 关于二叉查找树中的某个节点的前趋和后继的算法(Python实现)
- 二叉查找树(二叉排序树、有序二叉树)算法分析及实现
- 二叉查找树(AVL)插入算法Java实现
- 【算法设计-二叉搜索树】二叉查找树的操作与实现
- 算法学习 - 平衡二叉查找树实现(AVL树)
- 算法(第四版)学习笔记之java实现二叉查找树
- 二叉查找树的算法实现
- Unsharp Mask(USM)锐化算法的的原理及其实现
- 数字图像去噪典型算法及matlab实现
- 0001-算法笔记分治法实现棋盘覆盖问题