├── pictures ├── graph │ ├── prim │ │ ├── 01.jpg │ │ └── 02.jpg │ ├── basic │ │ ├── 01.jpg │ │ ├── 02.jpg │ │ ├── 03.jpg │ │ ├── 04.jpg │ │ ├── 05.jpg │ │ ├── 06.jpg │ │ ├── 07.jpg │ │ └── 08.jpg │ ├── floyd │ │ ├── 01.jpg │ │ └── 02.jpg │ ├── dijkstra │ │ ├── 01.jpg │ │ └── 02.jpg │ ├── iterator │ │ ├── 01.jpg │ │ ├── 02.jpg │ │ ├── 03.jpg │ │ ├── 04.jpg │ │ ├── 05.jpg │ │ └── 06.jpg │ ├── kruskal │ │ ├── 01.jpg │ │ ├── 02.jpg │ │ ├── 03.jpg │ │ └── 04.jpg │ └── topsort │ │ ├── 01.jpg │ │ └── 02.jpg ├── linear │ ├── array_01.jpg │ ├── dlink_01.jpg │ ├── dlink_02.jpg │ ├── dlink_03.jpg │ ├── dlink_m1.jpg │ ├── list_01.jpg │ ├── list_02.jpg │ ├── list_03.jpg │ ├── slink_01.jpg │ ├── slink_02.jpg │ ├── slink_03.jpg │ ├── stack_01.jpg │ ├── stack_02.jpg │ ├── stack_03.jpg │ ├── linux_dlink_01.jpg │ ├── linux_dlink_02.jpg │ └── linux_dlink_03.jpg ├── tree │ ├── huffman │ │ ├── 01.jpg │ │ ├── 02.jpg │ │ └── 03.jpg │ ├── bstree │ │ ├── order.jpg │ │ ├── bstree_01.jpg │ │ ├── bstree_02.jpg │ │ ├── bstree_03.jpg │ │ ├── bstree_04.jpg │ │ ├── bstree_05.jpg │ │ ├── bstree_06.jpg │ │ ├── bstree_07.jpg │ │ ├── bstree_08.jpg │ │ ├── bstree_09.jpg │ │ ├── bstree_test.jpg │ │ ├── bstree_bstree.jpg │ │ ├── bstree_delete.jpg │ │ ├── bstree_insert.jpg │ │ ├── bstree_5status.jpg │ │ ├── bstree_example.jpg │ │ ├── bstree_fulltree.jpg │ │ └── bstree_completetree.jpg │ ├── avltree │ │ ├── avltree_LL.jpg │ │ ├── avltree_LR.jpg │ │ ├── avltree_RL.jpg │ │ ├── avltree_RR.jpg │ │ ├── avltree_LR_01.jpg │ │ ├── avltree_RL_01.jpg │ │ ├── avltree_test_01.jpg │ │ ├── avltree_test_02.jpg │ │ ├── avltree_test_03.jpg │ │ ├── avltree_test_04.jpg │ │ ├── avltree_test_05.jpg │ │ ├── avltree_test_06.jpg │ │ ├── avltree_test_07.jpg │ │ ├── avltree_test_08.jpg │ │ ├── avltree_test_09.jpg │ │ ├── avltree_test_10.jpg │ │ ├── avltree_test_11.jpg │ │ ├── avltree_test_12.jpg │ │ ├── avltree_test_13.jpg │ │ ├── avltree_test_14.jpg │ │ ├── avltree_test_15.jpg │ │ ├── avltree_test_17.jpg │ │ ├── avltree_02_rotate.jpg │ │ ├── avltree_rotate_01.jpg │ │ ├── avltree_rotate_02.jpg │ │ ├── avltree_01_compare.jpg │ │ ├── avltree_example_01.jpg │ │ ├── avltree_example_02.jpg │ │ └── avltree_test_16_add_over.jpg │ ├── rbtree │ │ ├── rbtree_01.jpg │ │ ├── rbtree_02.jpg │ │ ├── rbtree_03.jpg │ │ ├── rbtree_04.jpg │ │ ├── rbtree_05.jpg │ │ ├── rbtree_06.jpg │ │ ├── rbtree_d01.jpg │ │ ├── rbtree_d02.jpg │ │ ├── rbtree_d03.jpg │ │ ├── rbtree_d04.jpg │ │ ├── rbtree_i01.jpg │ │ ├── rbtree_i02.jpg │ │ └── rbtree_i03.jpg │ └── splaytree │ │ ├── splaytree_00.jpg │ │ ├── splaytree_01.jpg │ │ ├── splaytree_02.jpg │ │ ├── splaytree_03.jpg │ │ ├── splaytree_04.jpg │ │ └── splaytree_05.jpg ├── algrithm │ ├── bubble_01.jpg │ ├── bucket_01.jpg │ ├── heap_01.jpg │ ├── heap_02.jpg │ ├── heap_p2.jpg │ ├── insert_01.jpg │ ├── merge_01.jpg │ ├── merge_02.jpg │ ├── merge_03.jpg │ ├── quick_01.jpg │ ├── radix_01.jpg │ ├── radix_02.jpg │ ├── radix_03.jpg │ ├── select_01.jpg │ ├── shell_01.jpg │ ├── shell_02.jpg │ ├── shell_03.jpg │ ├── heap_p1_01.jpg │ ├── heap_p1_02.jpg │ ├── heap_p1_03.jpg │ ├── heap_p1_04.jpg │ └── heap_p1_05.jpg └── heap │ ├── binomial │ ├── 01.jpg │ ├── 02.jpg │ ├── 03.jpg │ ├── 04.jpg │ ├── 05.jpg │ ├── 06.jpg │ └── 07.jpg │ ├── fibonacci │ ├── 01.jpg │ ├── 02.jpg │ ├── 03.jpg │ ├── 04.jpg │ ├── 05.jpg │ ├── 06.jpg │ └── 07.jpg │ ├── erchadui │ ├── heap_01.jpg │ ├── heap_02.jpg │ ├── heap_03.jpg │ ├── heap_04.jpg │ ├── heap_05.jpg │ └── heap_06.jpg │ └── leftist │ ├── leftist_01.jpg │ ├── leftist_02.jpg │ ├── leftist_03.jpg │ ├── leftist_04.jpg │ ├── leftist_05.jpg │ ├── leftist_06.jpg │ ├── leftist_07.jpg │ ├── leftist_08.jpg │ ├── leftist_09.jpg │ └── leftist_10.jpg ├── .gitignore ├── README.md └── source ├── tree ├── rbtree │ ├── jdk │ │ └── TreeMap.zip │ ├── stl │ │ └── rb_tree_map_.zip │ ├── c │ │ ├── rbtree.h │ │ └── rbtree_test.c │ ├── cplus │ │ └── RBTreeTest.cpp │ └── java │ │ └── RBTreeTest.java ├── huffman │ ├── cplus │ │ ├── HuffmanNode.h │ │ └── HuffmanTest.cpp │ ├── c │ │ ├── huffman.h │ │ └── huffman_test.c │ └── java │ │ ├── HuffmanTest.java │ │ ├── HuffmanNode.java │ │ ├── Huffman.java │ │ └── MinHeap.java ├── bstree │ ├── c │ │ ├── btree_test.c │ │ └── bstree.h │ ├── cplus │ │ └── BSTreeTest.cpp │ └── java │ │ └── BSTreeTest.java ├── splaytree │ ├── cplus │ │ └── SplayTreeTest.cpp │ ├── java │ │ └── SplayTreeTest.java │ └── c │ │ ├── splaytree_test.c │ │ └── splay_tree.h └── avl_tree │ ├── c │ ├── avltree.h │ └── avltree_test.c │ ├── cplus │ └── AVLTreeTest.cpp │ └── java │ └── AVLTreeTest.java ├── linear ├── stack │ ├── cplus │ │ ├── stl_stack │ │ │ ├── StackList.h │ │ │ ├── StlStack.cpp │ │ │ └── StackList.cpp │ │ └── self │ │ │ ├── Main.cpp │ │ │ └── ArrayStack.h │ ├── java │ │ ├── util_stack │ │ │ ├── StackTest.java │ │ │ └── StackList.java │ │ ├── self │ │ │ └── ArrayStack.java │ │ └── generical │ │ │ └── GeneralArrayStack.java │ └── c │ │ ├── var_dlink │ │ ├── double_link.h │ │ └── dlink_stack.c │ │ ├── double_link │ │ ├── double_link.h │ │ └── dlink_stack.c │ │ ├── linux │ │ ├── test.c │ │ ├── linux_stack.c │ │ └── list.h │ │ ├── array │ │ └── array_stack.c │ │ └── single_link │ │ └── slink_stack.c ├── linux_dlink │ ├── macros │ │ ├── offset_test.c │ │ └── container_test.c │ └── linux │ │ ├── test.c │ │ ├── dlink_stack.c │ │ └── list.h ├── queue │ ├── cplus │ │ ├── stl_stack │ │ │ └── StlQueue.cpp │ │ └── self │ │ │ ├── Main.cpp │ │ │ └── ArrayQueue.h │ ├── java │ │ ├── util_queue │ │ │ ├── QueueTest.java │ │ │ └── StackList.java │ │ └── self │ │ │ └── ArrayQueue.java │ └── c │ │ ├── var_dlink │ │ ├── double_link.h │ │ └── dlink_queue.c │ │ ├── double_link │ │ ├── double_link.h │ │ └── dlink_queue.c │ │ ├── array │ │ └── array_queue.c │ │ └── single_link │ │ └── slink_queue.c └── basic │ ├── single_link_list │ ├── cplus │ │ └── double_link.h │ └── c │ │ └── var_slink │ │ ├── slink.h │ │ ├── slink.c │ │ └── slink_test.c │ └── double_link_list │ ├── c │ └── var_dlink │ │ ├── double_link.h │ │ └── dlink_test.c │ ├── cplus │ └── DlinkTest.cpp │ └── java │ ├── DoubleLink.java │ └── DlinkTest.java ├── heap ├── skewheap │ ├── c │ │ ├── skewheap.h │ │ └── skewheap_test.c │ ├── cplus │ │ └── SkewHeapTest.cpp │ └── java │ │ └── SkewHeapTest.java ├── leftist │ ├── c │ │ ├── leftist.h │ │ └── leftist_test.c │ ├── java │ │ └── LeftistHeapTest.java │ └── cplus │ │ └── LeftistHeapTest.cpp ├── binomial │ ├── c │ │ └── binomial_heap.h │ └── cplus │ │ └── Main.cpp └── fibonacci │ └── c │ └── fibonacci_heap.h └── algrightm └── sort ├── insert_sort ├── cplus │ └── InsertSort.cpp ├── c │ └── insert_sort.c └── java │ └── InsertSort.java ├── selection_sort ├── c │ └── select_sort.c ├── cplus │ └── SelectSort.cpp └── java │ └── SelectSort.java ├── bucket_sort ├── java │ └── BucketSort.java ├── cplus │ └── BucketSort.cpp └── c │ └── bucket_sort.c ├── quick_sort ├── java │ └── QuickSort.java ├── cplus │ └── QuickSort.cpp └── c │ └── quick_sort.c ├── bubble ├── c │ └── bubble_sort.c ├── cplus │ └── BubbleSort.cpp └── java │ └── BubbleSort.java ├── radix_sort ├── cplus │ └── RadixSort.cpp ├── c │ └── radix_sort.c └── java │ └── RadixSort.java ├── shell_sort ├── cplus │ └── ShellSort.cpp ├── java │ └── ShellSort.java └── c │ └── shell_sort.c ├── merge_sort ├── java │ └── MergeSort.java ├── cplus │ └── MergeSort.cpp └── c │ └── merge_sort.c └── heap_sort └── java └── HeapSort.java /pictures/graph/prim/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/prim/01.jpg -------------------------------------------------------------------------------- /pictures/graph/prim/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/prim/02.jpg -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | 3 | *.[sS] 4 | *.o 5 | *.swp 6 | *.vim 7 | *.tgz 8 | *.out 9 | *.class 10 | 11 | drafts/ 12 | 13 | -------------------------------------------------------------------------------- /pictures/graph/basic/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/basic/01.jpg -------------------------------------------------------------------------------- /pictures/graph/basic/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/basic/02.jpg -------------------------------------------------------------------------------- /pictures/graph/basic/03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/basic/03.jpg -------------------------------------------------------------------------------- /pictures/graph/basic/04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/basic/04.jpg -------------------------------------------------------------------------------- /pictures/graph/basic/05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/basic/05.jpg -------------------------------------------------------------------------------- /pictures/graph/basic/06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/basic/06.jpg -------------------------------------------------------------------------------- /pictures/graph/basic/07.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/basic/07.jpg -------------------------------------------------------------------------------- /pictures/graph/basic/08.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/basic/08.jpg -------------------------------------------------------------------------------- /pictures/graph/floyd/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/floyd/01.jpg -------------------------------------------------------------------------------- /pictures/graph/floyd/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/floyd/02.jpg -------------------------------------------------------------------------------- /pictures/linear/array_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/array_01.jpg -------------------------------------------------------------------------------- /pictures/linear/dlink_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/dlink_01.jpg -------------------------------------------------------------------------------- /pictures/linear/dlink_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/dlink_02.jpg -------------------------------------------------------------------------------- /pictures/linear/dlink_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/dlink_03.jpg -------------------------------------------------------------------------------- /pictures/linear/dlink_m1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/dlink_m1.jpg -------------------------------------------------------------------------------- /pictures/linear/list_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/list_01.jpg -------------------------------------------------------------------------------- /pictures/linear/list_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/list_02.jpg -------------------------------------------------------------------------------- /pictures/linear/list_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/list_03.jpg -------------------------------------------------------------------------------- /pictures/linear/slink_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/slink_01.jpg -------------------------------------------------------------------------------- /pictures/linear/slink_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/slink_02.jpg -------------------------------------------------------------------------------- /pictures/linear/slink_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/slink_03.jpg -------------------------------------------------------------------------------- /pictures/linear/stack_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/stack_01.jpg -------------------------------------------------------------------------------- /pictures/linear/stack_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/stack_02.jpg -------------------------------------------------------------------------------- /pictures/linear/stack_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/stack_03.jpg -------------------------------------------------------------------------------- /pictures/tree/huffman/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/huffman/01.jpg -------------------------------------------------------------------------------- /pictures/tree/huffman/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/huffman/02.jpg -------------------------------------------------------------------------------- /pictures/tree/huffman/03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/huffman/03.jpg -------------------------------------------------------------------------------- /pictures/algrithm/bubble_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/bubble_01.jpg -------------------------------------------------------------------------------- /pictures/algrithm/bucket_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/bucket_01.jpg -------------------------------------------------------------------------------- /pictures/algrithm/heap_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/heap_01.jpg -------------------------------------------------------------------------------- /pictures/algrithm/heap_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/heap_02.jpg -------------------------------------------------------------------------------- /pictures/algrithm/heap_p2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/heap_p2.jpg -------------------------------------------------------------------------------- /pictures/algrithm/insert_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/insert_01.jpg -------------------------------------------------------------------------------- /pictures/algrithm/merge_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/merge_01.jpg -------------------------------------------------------------------------------- /pictures/algrithm/merge_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/merge_02.jpg -------------------------------------------------------------------------------- /pictures/algrithm/merge_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/merge_03.jpg -------------------------------------------------------------------------------- /pictures/algrithm/quick_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/quick_01.jpg -------------------------------------------------------------------------------- /pictures/algrithm/radix_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/radix_01.jpg -------------------------------------------------------------------------------- /pictures/algrithm/radix_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/radix_02.jpg -------------------------------------------------------------------------------- /pictures/algrithm/radix_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/radix_03.jpg -------------------------------------------------------------------------------- /pictures/algrithm/select_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/select_01.jpg -------------------------------------------------------------------------------- /pictures/algrithm/shell_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/shell_01.jpg -------------------------------------------------------------------------------- /pictures/algrithm/shell_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/shell_02.jpg -------------------------------------------------------------------------------- /pictures/algrithm/shell_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/shell_03.jpg -------------------------------------------------------------------------------- /pictures/graph/dijkstra/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/dijkstra/01.jpg -------------------------------------------------------------------------------- /pictures/graph/dijkstra/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/dijkstra/02.jpg -------------------------------------------------------------------------------- /pictures/graph/iterator/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/iterator/01.jpg -------------------------------------------------------------------------------- /pictures/graph/iterator/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/iterator/02.jpg -------------------------------------------------------------------------------- /pictures/graph/iterator/03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/iterator/03.jpg -------------------------------------------------------------------------------- /pictures/graph/iterator/04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/iterator/04.jpg -------------------------------------------------------------------------------- /pictures/graph/iterator/05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/iterator/05.jpg -------------------------------------------------------------------------------- /pictures/graph/iterator/06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/iterator/06.jpg -------------------------------------------------------------------------------- /pictures/graph/kruskal/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/kruskal/01.jpg -------------------------------------------------------------------------------- /pictures/graph/kruskal/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/kruskal/02.jpg -------------------------------------------------------------------------------- /pictures/graph/kruskal/03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/kruskal/03.jpg -------------------------------------------------------------------------------- /pictures/graph/kruskal/04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/kruskal/04.jpg -------------------------------------------------------------------------------- /pictures/graph/topsort/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/topsort/01.jpg -------------------------------------------------------------------------------- /pictures/graph/topsort/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/graph/topsort/02.jpg -------------------------------------------------------------------------------- /pictures/heap/binomial/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/binomial/01.jpg -------------------------------------------------------------------------------- /pictures/heap/binomial/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/binomial/02.jpg -------------------------------------------------------------------------------- /pictures/heap/binomial/03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/binomial/03.jpg -------------------------------------------------------------------------------- /pictures/heap/binomial/04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/binomial/04.jpg -------------------------------------------------------------------------------- /pictures/heap/binomial/05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/binomial/05.jpg -------------------------------------------------------------------------------- /pictures/heap/binomial/06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/binomial/06.jpg -------------------------------------------------------------------------------- /pictures/heap/binomial/07.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/binomial/07.jpg -------------------------------------------------------------------------------- /pictures/heap/fibonacci/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/fibonacci/01.jpg -------------------------------------------------------------------------------- /pictures/heap/fibonacci/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/fibonacci/02.jpg -------------------------------------------------------------------------------- /pictures/heap/fibonacci/03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/fibonacci/03.jpg -------------------------------------------------------------------------------- /pictures/heap/fibonacci/04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/fibonacci/04.jpg -------------------------------------------------------------------------------- /pictures/heap/fibonacci/05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/fibonacci/05.jpg -------------------------------------------------------------------------------- /pictures/heap/fibonacci/06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/fibonacci/06.jpg -------------------------------------------------------------------------------- /pictures/heap/fibonacci/07.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/fibonacci/07.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/order.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/order.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 数据结构和算法的详细介绍(图片+文字详细说明),请参考"[wangkuiwu的博客][link_blog]" 3 | 4 | [link_blog]: http://wangkuiwu.github.com "blog" 5 | 6 | -------------------------------------------------------------------------------- /pictures/algrithm/heap_p1_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/heap_p1_01.jpg -------------------------------------------------------------------------------- /pictures/algrithm/heap_p1_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/heap_p1_02.jpg -------------------------------------------------------------------------------- /pictures/algrithm/heap_p1_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/heap_p1_03.jpg -------------------------------------------------------------------------------- /pictures/algrithm/heap_p1_04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/heap_p1_04.jpg -------------------------------------------------------------------------------- /pictures/algrithm/heap_p1_05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/algrithm/heap_p1_05.jpg -------------------------------------------------------------------------------- /pictures/heap/erchadui/heap_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/erchadui/heap_01.jpg -------------------------------------------------------------------------------- /pictures/heap/erchadui/heap_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/erchadui/heap_02.jpg -------------------------------------------------------------------------------- /pictures/heap/erchadui/heap_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/erchadui/heap_03.jpg -------------------------------------------------------------------------------- /pictures/heap/erchadui/heap_04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/erchadui/heap_04.jpg -------------------------------------------------------------------------------- /pictures/heap/erchadui/heap_05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/erchadui/heap_05.jpg -------------------------------------------------------------------------------- /pictures/heap/erchadui/heap_06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/erchadui/heap_06.jpg -------------------------------------------------------------------------------- /pictures/heap/leftist/leftist_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/leftist/leftist_01.jpg -------------------------------------------------------------------------------- /pictures/heap/leftist/leftist_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/leftist/leftist_02.jpg -------------------------------------------------------------------------------- /pictures/heap/leftist/leftist_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/leftist/leftist_03.jpg -------------------------------------------------------------------------------- /pictures/heap/leftist/leftist_04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/leftist/leftist_04.jpg -------------------------------------------------------------------------------- /pictures/heap/leftist/leftist_05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/leftist/leftist_05.jpg -------------------------------------------------------------------------------- /pictures/heap/leftist/leftist_06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/leftist/leftist_06.jpg -------------------------------------------------------------------------------- /pictures/heap/leftist/leftist_07.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/leftist/leftist_07.jpg -------------------------------------------------------------------------------- /pictures/heap/leftist/leftist_08.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/leftist/leftist_08.jpg -------------------------------------------------------------------------------- /pictures/heap/leftist/leftist_09.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/leftist/leftist_09.jpg -------------------------------------------------------------------------------- /pictures/heap/leftist/leftist_10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/heap/leftist/leftist_10.jpg -------------------------------------------------------------------------------- /pictures/linear/linux_dlink_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/linux_dlink_01.jpg -------------------------------------------------------------------------------- /pictures/linear/linux_dlink_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/linux_dlink_02.jpg -------------------------------------------------------------------------------- /pictures/linear/linux_dlink_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/linear/linux_dlink_03.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_LL.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_LL.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_LR.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_LR.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_RL.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_RL.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_RR.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_RR.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_01.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_02.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_03.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_04.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_05.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_06.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_07.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_07.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_08.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_08.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_09.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_09.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_test.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_01.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_02.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_03.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_04.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_05.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_06.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_d01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_d01.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_d02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_d02.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_d03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_d03.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_d04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_d04.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_i01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_i01.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_i02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_i02.jpg -------------------------------------------------------------------------------- /pictures/tree/rbtree/rbtree_i03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/rbtree/rbtree_i03.jpg -------------------------------------------------------------------------------- /source/tree/rbtree/jdk/TreeMap.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/source/tree/rbtree/jdk/TreeMap.zip -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_bstree.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_bstree.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_delete.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_delete.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_insert.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_insert.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_LR_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_LR_01.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_RL_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_RL_01.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_01.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_02.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_03.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_04.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_05.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_06.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_07.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_07.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_08.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_08.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_09.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_09.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_10.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_11.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_12.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_13.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_14.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_15.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_17.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_5status.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_5status.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_example.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_fulltree.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_fulltree.jpg -------------------------------------------------------------------------------- /pictures/tree/splaytree/splaytree_00.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/splaytree/splaytree_00.jpg -------------------------------------------------------------------------------- /pictures/tree/splaytree/splaytree_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/splaytree/splaytree_01.jpg -------------------------------------------------------------------------------- /pictures/tree/splaytree/splaytree_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/splaytree/splaytree_02.jpg -------------------------------------------------------------------------------- /pictures/tree/splaytree/splaytree_03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/splaytree/splaytree_03.jpg -------------------------------------------------------------------------------- /pictures/tree/splaytree/splaytree_04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/splaytree/splaytree_04.jpg -------------------------------------------------------------------------------- /pictures/tree/splaytree/splaytree_05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/splaytree/splaytree_05.jpg -------------------------------------------------------------------------------- /source/tree/rbtree/stl/rb_tree_map_.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/source/tree/rbtree/stl/rb_tree_map_.zip -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_02_rotate.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_02_rotate.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_rotate_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_rotate_01.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_rotate_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_rotate_02.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_01_compare.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_01_compare.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_example_01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_example_01.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_example_02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_example_02.jpg -------------------------------------------------------------------------------- /pictures/tree/bstree/bstree_completetree.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/bstree/bstree_completetree.jpg -------------------------------------------------------------------------------- /pictures/tree/avltree/avltree_test_16_add_over.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangkuiwu/datastructs_and_algorithm/HEAD/pictures/tree/avltree/avltree_test_16_add_over.jpg -------------------------------------------------------------------------------- /source/linear/stack/cplus/stl_stack/StackList.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef STACK_LIST_HXX 3 | #define STACK_LIST_HXX 4 | 5 | #include 6 | 7 | template class StackList{ 8 | public: 9 | StackList(); 10 | 11 | void add(T t); 12 | T front(); 13 | T pop(); 14 | int size(); 15 | int is_empty(); 16 | 17 | private: 18 | // 向队列添加数据时:(01) 将“已有的全部数据”都移到sin中。 (02) 将“新添加的数据”添加到sin中。 19 | stack sin; 20 | // 从队列获取元素时:(01) 将“已有的全部数据”都移到sout中。(02) 返回并删除sout栈顶元素。 21 | stack sout; 22 | int count; 23 | }; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /source/tree/huffman/cplus/HuffmanNode.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Huffman树节点类 3 | * 4 | * @author skywang 5 | * @date 2014/03/25 6 | */ 7 | 8 | #ifndef _HUFFMAN_NODE_HPP_ 9 | #define _HUFFMAN_NODE_HPP_ 10 | 11 | template 12 | class HuffmanNode{ 13 | public: 14 | T key; // 权值 15 | HuffmanNode *left; // 左孩子 16 | HuffmanNode *right; // 右孩子 17 | HuffmanNode *parent;// 父结点 18 | 19 | 20 | HuffmanNode(){} 21 | HuffmanNode(T value, HuffmanNode *l, HuffmanNode *r, HuffmanNode *p): 22 | key(value),left(l),right(r),parent(p) {} 23 | }; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /source/linear/stack/cplus/self/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ArrayStack.h" 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int tmp=0; 8 | ArrayStack *astack = new ArrayStack(); 9 | 10 | cout<<"main"<push(10); 14 | astack->push(20); 15 | astack->push(30); 16 | 17 | // 将“栈顶元素”赋值给tmp,并删除“栈顶元素” 18 | tmp = astack->pop(); 19 | cout<<"tmp="<peek(); 23 | 24 | astack->push(40); 25 | 26 | while (!astack->isEmpty()) 27 | { 28 | tmp = astack->pop(); 29 | cout< 2 | #include 3 | using namespace std; 4 | 5 | /** 6 | * C++ 语言: STL 自带的“栈”(stack)的示例。 7 | * 8 | * @author skywang 9 | * @date 2013/11/07 10 | */ 11 | int main () 12 | { 13 | int tmp=0; 14 | stack istack; 15 | 16 | // 将10, 20, 30 依次推入栈中 17 | istack.push(10); 18 | istack.push(20); 19 | istack.push(30); 20 | 21 | // 将“栈顶元素”赋值给tmp,并删除“栈顶元素” 22 | istack.pop(); 23 | 24 | // 只将“栈顶”赋值给tmp,不删除该元素. 25 | tmp = istack.top(); 26 | 27 | istack.push(40); 28 | 29 | while (!istack.empty()) 30 | { 31 | tmp = istack.top(); 32 | istack.pop(); 33 | cout< 3 | 4 | // 获得结构体(TYPE)的变量成员(MEMBER)在此结构体中的偏移量。 5 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 6 | 7 | struct student 8 | { 9 | char gender; 10 | int id; 11 | int age; 12 | char name[20]; 13 | }; 14 | 15 | void main() 16 | { 17 | int gender_offset, id_offset, age_offset, name_offset; 18 | 19 | gender_offset = offsetof(struct student, gender); 20 | id_offset = offsetof(struct student, id); 21 | age_offset = offsetof(struct student, age); 22 | name_offset = offsetof(struct student, name); 23 | 24 | printf("gender_offset = %d\n", gender_offset); 25 | printf("id_offset = %d\n", id_offset); 26 | printf("age_offset = %d\n", age_offset); 27 | printf("name_offset = %d\n", name_offset); 28 | } 29 | -------------------------------------------------------------------------------- /source/tree/huffman/cplus/HuffmanTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Huffman树测试程序 3 | * 4 | * @author skywang 5 | * @date 2014/03/25 6 | */ 7 | 8 | #include 9 | #include "Huffman.h" 10 | using namespace std; 11 | 12 | 13 | int main() 14 | { 15 | int a[]= {5,6,8,7,15}; 16 | int i, ilen = sizeof(a) / (sizeof(a[0])) ; 17 | Huffman* tree=new Huffman(); 18 | 19 | cout << "== 添加数组: "; 20 | for(i=0; icreate(a, ilen); 24 | 25 | cout << "\n== 前序遍历: "; 26 | tree->preOrder(); 27 | 28 | cout << "\n== 中序遍历: "; 29 | tree->inOrder(); 30 | 31 | cout << "\n== 后序遍历: "; 32 | tree->postOrder(); 33 | cout << endl; 34 | 35 | cout << "== 树的详细信息: " << endl; 36 | tree->print(); 37 | 38 | // 销毁二叉树 39 | tree->destroy(); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /source/tree/huffman/c/huffman_test.c: -------------------------------------------------------------------------------- 1 | /** 2 | * C 语言: Huffman树 3 | * 4 | * @author skywang 5 | * @date 2014/03/25 6 | */ 7 | #include 8 | #include "huffman.h" 9 | 10 | #define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) ) 11 | 12 | void main() 13 | { 14 | int a[]= {5,6,8,7,15}; 15 | int i,ilen=LENGTH(a); 16 | HuffmanTree root=NULL; 17 | 18 | printf("== 添加数组: "); 19 | for(i=0; i astack = new Stack(); 15 | 16 | // 将10, 20, 30 依次推入栈中 17 | astack.push(10); 18 | astack.push(20); 19 | astack.push(30); 20 | 21 | // 将“栈顶元素”赋值给tmp,并删除“栈顶元素” 22 | tmp = astack.pop(); 23 | //System.out.printf("tmp=%d\n", tmp); 24 | 25 | // 只将“栈顶”赋值给tmp,不删除该元素. 26 | tmp = (int)astack.peek(); 27 | //System.out.printf("tmp=%d\n", tmp); 28 | 29 | astack.push(40); 30 | while(!astack.empty()) { 31 | tmp = (int)astack.pop(); 32 | System.out.printf("tmp=%d\n", tmp); 33 | } 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /source/tree/huffman/java/HuffmanTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Huffman树的测试程序 3 | * 4 | * @author skywang 5 | * @date 2014/03/27 6 | */ 7 | 8 | public class HuffmanTest { 9 | 10 | private static final int a[]= {5,6,8,7,15}; 11 | 12 | public static void main(String[] args) { 13 | int i; 14 | Huffman tree; 15 | 16 | System.out.print("== 添加数组: "); 17 | for(i=0; i 2 | #include 3 | using namespace std; 4 | 5 | /** 6 | * C++ : STL中的队列(queue)的演示程序。 7 | * 8 | * @author skywang 9 | * @date 2013/11/07 10 | */ 11 | int main () 12 | { 13 | int tmp=0; 14 | queue iqueue; 15 | 16 | // 将10, 20, 30 依次加入队列的末尾 17 | iqueue.push(10); 18 | iqueue.push(20); 19 | iqueue.push(30); 20 | 21 | // 删除队列开头的元素 22 | iqueue.pop(); 23 | 24 | // 将“队列开头的元素”赋值给tmp,不删除该元素. 25 | tmp = iqueue.front(); 26 | cout<<"tmp="< *astack = new ArrayQueue(); 15 | 16 | // 将10, 20, 30 依次推入队列中 17 | astack->add(10); 18 | astack->add(20); 19 | astack->add(30); 20 | 21 | // 将“队列开头元素”赋值给tmp,并删除“该元素” 22 | tmp = astack->pop(); 23 | cout<<"tmp="<front(); 27 | cout<<"tmp="<add(40); 30 | 31 | cout<<"is_empty()="<is_empty()< que = new LinkedList(); 16 | 17 | // 将10, 20, 30 依次推入队列中 18 | que.add(10); 19 | que.add(20); 20 | que.add(30); 21 | 22 | // 将“队列开头的元素”赋值给tmp,并删除“该元素” 23 | tmp = que.poll(); 24 | System.out.printf("tmp=%d\n", tmp); 25 | 26 | // 将“队列开头的元素”赋值给tmp,不删除该元素. 27 | tmp = que.peek(); 28 | System.out.printf("tmp=%d\n", tmp); 29 | 30 | que.add(40); 31 | 32 | System.out.printf("isEmpty()=%b\n", que.isEmpty()); 33 | System.out.printf("size()=%d\n", que.size()); 34 | while(!que.isEmpty()) { 35 | tmp = que.poll(); 36 | System.out.printf("tmp=%d\n", tmp); 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /source/linear/basic/single_link_list/cplus/double_link.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef DOUBLE_LINK_HXX 3 | #define DOUBLE_LINK_HXX 4 | 5 | template 6 | class DNode 7 | { 8 | public: 9 | T value; 10 | DNode *prev; 11 | DNode *next; 12 | public: 13 | DNode() { } 14 | DNode(T t, DNode *prev, DNode *next) { 15 | this->value = t; 16 | this->prev = prev; 17 | this->next = next; 18 | } 19 | }; 20 | 21 | template 22 | class DoubleLink 23 | { 24 | public: 25 | DoubleLink(); 26 | ~DoubleLink(); 27 | 28 | int size(); 29 | int is_empty(); 30 | 31 | T get(int index); 32 | T get_first(); 33 | T get_last(); 34 | 35 | int insert(int index, T t); 36 | int insert_first(T t); 37 | int append_last(T t); 38 | 39 | int del(int index); 40 | int delete_first(); 41 | int delete_last(); 42 | 43 | private: 44 | int count; 45 | DNode *phead; 46 | private: 47 | DNode *get_node(int index); 48 | }; 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /source/tree/huffman/java/HuffmanNode.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Huffman节点类(Huffman.java的辅助类) 3 | * 4 | * @author skywang 5 | * @date 2014/03/27 6 | */ 7 | 8 | public class HuffmanNode implements Comparable, Cloneable { 9 | protected int key; // 权值 10 | protected HuffmanNode left; // 左孩子 11 | protected HuffmanNode right; // 右孩子 12 | protected HuffmanNode parent; // 父结点 13 | 14 | protected HuffmanNode(int key, HuffmanNode left, HuffmanNode right, HuffmanNode parent) { 15 | this.key = key; 16 | this.left = left; 17 | this.right = right; 18 | this.parent = parent; 19 | } 20 | 21 | @Override 22 | public Object clone() { 23 | Object obj=null; 24 | 25 | try { 26 | obj = (HuffmanNode)super.clone();//Object 中的clone()识别出你要复制的是哪一个对象。 27 | } catch(CloneNotSupportedException e) { 28 | System.out.println(e.toString()); 29 | } 30 | 31 | return obj; 32 | } 33 | 34 | @Override 35 | public int compareTo(Object obj) { 36 | return this.key - ((HuffmanNode)obj).key; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /source/linear/linux_dlink/macros/container_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // 获得结构体(TYPE)的变量成员(MEMBER)在此结构体中的偏移量。 5 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 6 | 7 | // 根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针 8 | #define container_of(ptr, type, member) ({ \ 9 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 10 | (type *)( (char *)__mptr - offsetof(type,member) );}) 11 | 12 | struct student 13 | { 14 | char gender; 15 | int id; 16 | int age; 17 | char name[20]; 18 | }; 19 | 20 | void main() 21 | { 22 | struct student stu; 23 | struct student *pstu; 24 | 25 | stu.gender = '1'; 26 | stu.id = 9527; 27 | stu.age = 24; 28 | strcpy(stu.name, "zhouxingxing"); 29 | 30 | // 根据"id地址" 获取 "结构体的地址"。 31 | pstu = container_of(&stu.id, struct student, id); 32 | 33 | // 根据获取到的结构体student的地址,访问其它成员 34 | printf("gender= %c\n", pstu->gender); 35 | printf("age= %d\n", pstu->age); 36 | printf("name= %s\n", pstu->name); 37 | } 38 | -------------------------------------------------------------------------------- /source/heap/leftist/c/leftist.h: -------------------------------------------------------------------------------- 1 | #ifndef _LEFTIST_TREE_H_ 2 | #define _LEFTIST_TREE_H_ 3 | 4 | typedef int Type; 5 | 6 | typedef struct _LeftistNode{ 7 | Type key; // 关键字(键值) 8 | int npl; // 零路经长度(Null Path Length) 9 | struct _LeftistNode *left; // 左孩子 10 | struct _LeftistNode *right; // 右孩子 11 | }LeftistNode, *LeftistHeap; 12 | 13 | // 前序遍历"左倾堆" 14 | void preorder_leftist(LeftistHeap heap); 15 | // 中序遍历"左倾堆" 16 | void inorder_leftist(LeftistHeap heap); 17 | // 后序遍历"左倾堆" 18 | void postorder_leftist(LeftistHeap heap); 19 | 20 | // 获取最小值(保存到pval中),成功返回0,失败返回-1。 21 | int leftist_minimum(LeftistHeap heap, int *pval); 22 | // 合并"左倾堆x"和"左倾堆y",并返回合并后的新树 23 | LeftistNode* merge_leftist(LeftistHeap x, LeftistHeap y); 24 | // 将结点插入到左倾堆中,并返回根节点 25 | LeftistNode* insert_leftist(LeftistHeap heap, Type key); 26 | // 删除结点(key为节点的值),并返回根节点 27 | LeftistNode* delete_leftist(LeftistHeap heap); 28 | 29 | // 销毁左倾堆 30 | void destroy_leftist(LeftistHeap heap); 31 | 32 | // 打印左倾堆 33 | void print_leftist(LeftistHeap heap); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /source/heap/leftist/c/leftist_test.c: -------------------------------------------------------------------------------- 1 | /** 2 | * C语言实现的左倾堆 3 | * 4 | * @author skywang 5 | * @date 2014/03/31 6 | */ 7 | 8 | #include 9 | #include "leftist.h" 10 | 11 | #define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) ) 12 | 13 | void main() 14 | { 15 | int i; 16 | int a[]= {10,40,24,30,36,20,12,16}; 17 | int b[]= {17,13,11,15,19,21,23}; 18 | int alen=LENGTH(a); 19 | int blen=LENGTH(b); 20 | LeftistHeap ha,hb; 21 | 22 | ha=hb=NULL; 23 | 24 | printf("== 左倾堆(ha)中依次添加: "); 25 | for(i=0; i 9 | #include "skewheap.h" 10 | 11 | #define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) ) 12 | 13 | void main() 14 | { 15 | int i; 16 | int a[]= {10,40,24,30,36,20,12,16}; 17 | int b[]= {17,13,11,15,19,21,23}; 18 | int alen=LENGTH(a); 19 | int blen=LENGTH(b); 20 | SkewHeap ha,hb; 21 | 22 | ha=hb=NULL; 23 | 24 | printf("== 斜堆(ha)中依次添加: "); 25 | for(i=0; i 9 | #include "SkewHeap.h" 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | int i; 15 | int a[]= {10,40,24,30,36,20,12,16}; 16 | int b[]= {17,13,11,15,19,21,23}; 17 | int alen=sizeof(a)/sizeof(a[0]); 18 | int blen=sizeof(b)/sizeof(b[0]); 19 | SkewHeap* ha=new SkewHeap(); 20 | SkewHeap* hb=new SkewHeap(); 21 | 22 | cout << "== 斜堆(ha)中依次添加: "; 23 | for(i=0; iinsert(a[i]); 27 | } 28 | cout << "\n== 斜堆(ha)的详细信息: " << endl; 29 | ha->print(); 30 | 31 | 32 | cout << "\n== 斜堆(hb)中依次添加: "; 33 | for(i=0; iinsert(b[i]); 37 | } 38 | cout << "\n== 斜堆(hb)的详细信息: " << endl; 39 | hb->print(); 40 | 41 | 42 | // 将"斜堆hb"合并到"斜堆ha"中。 43 | ha->merge(hb); 44 | cout << "\n== 合并ha和hb后的详细信息: " << endl; 45 | ha->print(); 46 | 47 | 48 | // 销毁 49 | ha->destroy(); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /source/heap/skewheap/java/SkewHeapTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Java 语言: 斜堆 3 | * 4 | * @author skywang 5 | * @date 2014/03/31 6 | */ 7 | 8 | public class SkewHeapTest { 9 | 10 | public static void main(String[] args) { 11 | 12 | int a[]= {10,40,24,30,36,20,12,16}; 13 | int b[]= {17,13,11,15,19,21,23}; 14 | SkewHeap ha=new SkewHeap(); 15 | SkewHeap hb=new SkewHeap(); 16 | 17 | System.out.printf("== 斜堆(ha)中依次添加: "); 18 | for(int i=0; i ha=new LeftistHeap(); 14 | LeftistHeap hb=new LeftistHeap(); 15 | 16 | System.out.printf("== 左倾堆(ha)中依次添加: "); 17 | for(int i=0; i 9 | #include "LeftistHeap.h" 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | int i; 15 | int a[]= {10,40,24,30,36,20,12,16}; 16 | int b[]= {17,13,11,15,19,21,23}; 17 | int alen=sizeof(a)/sizeof(a[0]); 18 | int blen=sizeof(b)/sizeof(b[0]); 19 | LeftistHeap* ha=new LeftistHeap(); 20 | LeftistHeap* hb=new LeftistHeap(); 21 | 22 | cout << "== 左倾堆(ha)中依次添加: "; 23 | for(i=0; iinsert(a[i]); 27 | } 28 | cout << "\n== 左倾堆(ha)的详细信息: " << endl; 29 | ha->print(); 30 | 31 | 32 | cout << "\n== 左倾堆(hb)中依次添加: "; 33 | for(i=0; iinsert(b[i]); 37 | } 38 | cout << "\n== 左倾堆(hb)的详细信息: " << endl; 39 | hb->print(); 40 | 41 | 42 | // 将"左倾堆hb"合并到"左倾堆ha"中。 43 | ha->merge(hb); 44 | cout << "\n== 合并ha和hb后的详细信息: " << endl; 45 | ha->print(); 46 | 47 | 48 | // 销毁 49 | ha->destroy(); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /source/linear/queue/c/var_dlink/double_link.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _DOUBLE_LINK_H 3 | #define _DOUBLE_LINK_H 4 | 5 | // 新建“双向链表”。成功,返回表头;否则,返回NULL 6 | extern int create_dlink(); 7 | // 撤销“双向链表”。成功,返回0;否则,返回-1 8 | extern int destroy_dlink(); 9 | 10 | // “双向链表是否为空”。为空的话返回1;否则,返回0。 11 | extern int dlink_is_empty(); 12 | // 返回“双向链表的大小” 13 | extern int dlink_size(); 14 | 15 | // 获取“双向链表中第index位置的元素”。成功,返回节点指针;否则,返回NULL。 16 | extern void* dlink_get(int index); 17 | // 获取“双向链表中第1个元素”。成功,返回节点指针;否则,返回NULL。 18 | extern void* dlink_get_first(); 19 | // 获取“双向链表中最后1个元素”。成功,返回节点指针;否则,返回NULL。 20 | extern void* dlink_get_last(); 21 | 22 | // 将“value”插入到index位置。成功,返回0;否则,返回-1。 23 | extern int dlink_insert(int index, void *pval); 24 | // 将“value”插入到表头位置。成功,返回0;否则,返回-1。 25 | extern int dlink_insert_first(void *pval); 26 | // 将“value”插入到末尾位置。成功,返回0;否则,返回-1。 27 | extern int dlink_append_last(void *pval); 28 | 29 | // 删除“双向链表中index位置的节点”。成功,返回0;否则,返回-1 30 | extern int dlink_delete(int index); 31 | // 删除第一个节点。成功,返回0;否则,返回-1 32 | extern int dlink_delete_first(); 33 | // 删除组后一个节点。成功,返回0;否则,返回-1 34 | extern int dlink_delete_last(); 35 | 36 | #endif 37 | 38 | -------------------------------------------------------------------------------- /source/linear/stack/c/var_dlink/double_link.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _DOUBLE_LINK_H 3 | #define _DOUBLE_LINK_H 4 | 5 | // 新建“双向链表”。成功,返回表头;否则,返回NULL 6 | extern int create_dlink(); 7 | // 撤销“双向链表”。成功,返回0;否则,返回-1 8 | extern int destroy_dlink(); 9 | 10 | // “双向链表是否为空”。为空的话返回1;否则,返回0。 11 | extern int dlink_is_empty(); 12 | // 返回“双向链表的大小” 13 | extern int dlink_size(); 14 | 15 | // 获取“双向链表中第index位置的元素”。成功,返回节点指针;否则,返回NULL。 16 | extern void* dlink_get(int index); 17 | // 获取“双向链表中第1个元素”。成功,返回节点指针;否则,返回NULL。 18 | extern void* dlink_get_first(); 19 | // 获取“双向链表中最后1个元素”。成功,返回节点指针;否则,返回NULL。 20 | extern void* dlink_get_last(); 21 | 22 | // 将“value”插入到index位置。成功,返回0;否则,返回-1。 23 | extern int dlink_insert(int index, void *pval); 24 | // 将“value”插入到表头位置。成功,返回0;否则,返回-1。 25 | extern int dlink_insert_first(void *pval); 26 | // 将“value”插入到末尾位置。成功,返回0;否则,返回-1。 27 | extern int dlink_append_last(void *pval); 28 | 29 | // 删除“双向链表中index位置的节点”。成功,返回0;否则,返回-1 30 | extern int dlink_delete(int index); 31 | // 删除第一个节点。成功,返回0;否则,返回-1 32 | extern int dlink_delete_first(); 33 | // 删除组后一个节点。成功,返回0;否则,返回-1 34 | extern int dlink_delete_last(); 35 | 36 | #endif 37 | 38 | -------------------------------------------------------------------------------- /source/tree/bstree/c/btree_test.c: -------------------------------------------------------------------------------- 1 | /** 2 | * C 语言: 二叉查找树 3 | * 4 | * @author skywang 5 | * @date 2013/11/07 6 | */ 7 | 8 | #include 9 | #include "bstree.h" 10 | 11 | static int arr[]= {1,5,4,3,2,6}; 12 | #define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) ) 13 | 14 | void main() 15 | { 16 | int i, ilen; 17 | BSTree root=NULL; 18 | 19 | printf("== 依次添加: "); 20 | ilen = TBL_SIZE(arr); 21 | for(i=0; ikey); 38 | printf("== 最大值: %d\n", bstree_maximum(root)->key); 39 | printf("== 树的详细信息: \n"); 40 | print_bstree(root, root->key, 0); 41 | 42 | printf("\n== 删除根节点: %d", arr[3]); 43 | root = delete_bstree(root, arr[3]); 44 | 45 | printf("\n== 中序遍历: "); 46 | inorder_bstree(root); 47 | printf("\n"); 48 | 49 | // 销毁二叉树 50 | destroy_bstree(root); 51 | } 52 | -------------------------------------------------------------------------------- /source/tree/bstree/cplus/BSTreeTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * C++ 语言: 二叉查找树 3 | * 4 | * @author skywang 5 | * @date 2013/11/07 6 | */ 7 | 8 | #include 9 | #include "BSTree.h" 10 | using namespace std; 11 | 12 | static int arr[]= {1,5,4,3,2,6}; 13 | #define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) ) 14 | 15 | int main() 16 | { 17 | int i, ilen; 18 | BSTree* tree=new BSTree(); 19 | 20 | cout << "== 依次添加: "; 21 | ilen = TBL_SIZE(arr); 22 | for(i=0; iinsert(arr[i]); 26 | } 27 | 28 | cout << "\n== 前序遍历: "; 29 | tree->preOrder(); 30 | 31 | cout << "\n== 中序遍历: "; 32 | tree->inOrder(); 33 | 34 | cout << "\n== 后序遍历: "; 35 | tree->postOrder(); 36 | cout << endl; 37 | 38 | cout << "== 最小值: " << tree->minimum() << endl; 39 | cout << "== 最大值: " << tree->maximum() << endl; 40 | cout << "== 树的详细信息: " << endl; 41 | tree->print(); 42 | 43 | cout << "\n== 删除根节点: " << arr[3]; 44 | tree->remove(arr[3]); 45 | 46 | cout << "\n== 中序遍历: "; 47 | tree->inOrder(); 48 | cout << endl; 49 | 50 | // 销毁二叉树 51 | tree->destroy(); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /source/linear/basic/single_link_list/c/var_slink/slink.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _DOUBLE_LINK_H 3 | #define _DOUBLE_LINK_H 4 | 5 | // 新建“双向链表”。成功,返回表头;否则,返回NULL 6 | extern int create_dlink(); 7 | // 撤销“双向链表”。成功,返回0;否则,返回-1 8 | extern int destroy_dlink(); 9 | 10 | // “双向链表是否为空”。为空的话返回1;否则,返回0。 11 | extern int dlink_is_empty(); 12 | // 返回“双向链表的大小” 13 | extern int dlink_size(); 14 | 15 | // 获取“双向链表中第index位置的元素”。成功,返回节点指针;否则,返回NULL。 16 | extern void* dlink_get(int index); 17 | // 获取“双向链表中第1个元素”。成功,返回节点指针;否则,返回NULL。 18 | extern void* dlink_get_first(); 19 | // 获取“双向链表中最后1个元素”。成功,返回节点指针;否则,返回NULL。 20 | extern void* dlink_get_last(); 21 | 22 | // 将“value”插入到index位置。成功,返回0;否则,返回-1。 23 | extern int dlink_insert(int index, void *pval); 24 | // 将“value”插入到表头位置。成功,返回0;否则,返回-1。 25 | extern int dlink_insert_first(void *pval); 26 | // 将“value”插入到末尾位置。成功,返回0;否则,返回-1。 27 | extern int dlink_append_last(void *pval); 28 | 29 | // 删除“双向链表中index位置的节点”。成功,返回0;否则,返回-1 30 | extern int dlink_delete(int index); 31 | // 删除第一个节点。成功,返回0;否则,返回-1 32 | extern int dlink_delete_first(); 33 | // 删除组后一个节点。成功,返回0;否则,返回-1 34 | extern int dlink_delete_last(); 35 | 36 | #endif 37 | 38 | -------------------------------------------------------------------------------- /source/heap/binomial/c/binomial_heap.h: -------------------------------------------------------------------------------- 1 | #ifndef _BINOMIAL_HEAP_H_ 2 | #define _BINOMIAL_HEAP_H_ 3 | 4 | typedef int Type; 5 | 6 | typedef struct _BinomialNode{ 7 | Type key; // 关键字(键值) 8 | int degree; // 度数 9 | struct _BinomialNode *child; // 左孩子 10 | struct _BinomialNode *parent; // 父节点 11 | struct _BinomialNode *next; // 兄弟 12 | }BinomialNode, *BinomialHeap; 13 | 14 | // 新建key对应的节点,并将其插入到二项堆中。 15 | BinomialNode* binomial_insert(BinomialHeap heap, Type key); 16 | // 删除节点:删除键值为key的节点,并返回删除节点后的二项树 17 | BinomialNode* binomial_delete(BinomialHeap heap, Type key); 18 | // 将二项堆heap的键值oldkey更新为newkey 19 | void binomial_update(BinomialHeap heap, Type oldkey, Type newkey); 20 | 21 | // 合并二项堆:将h1, h2合并成一个堆,并返回合并后的堆 22 | BinomialNode* binomial_union(BinomialHeap h1, BinomialHeap h2) ; 23 | 24 | // 查找:在二项堆中查找键值为key的节点 25 | BinomialNode* binomial_search(BinomialHeap heap, Type key); 26 | // 获取二项堆中的最小节点 27 | BinomialNode* binomial_minimum(BinomialHeap heap) ; 28 | // 移除最小节点,并返回移除节点后的二项堆 29 | BinomialNode* binomial_extract_minimum(BinomialHeap heap); 30 | 31 | // 打印"二项堆" 32 | void binomial_print(BinomialHeap heap); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /source/linear/basic/double_link_list/c/var_dlink/double_link.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _DOUBLE_LINK_H 3 | #define _DOUBLE_LINK_H 4 | 5 | // 新建“双向链表”。成功,返回表头;否则,返回NULL 6 | extern int create_dlink(); 7 | // 撤销“双向链表”。成功,返回0;否则,返回-1 8 | extern int destroy_dlink(); 9 | 10 | // “双向链表是否为空”。为空的话返回1;否则,返回0。 11 | extern int dlink_is_empty(); 12 | // 返回“双向链表的大小” 13 | extern int dlink_size(); 14 | 15 | // 获取“双向链表中第index位置的元素”。成功,返回节点指针;否则,返回NULL。 16 | extern void* dlink_get(int index); 17 | // 获取“双向链表中第1个元素”。成功,返回节点指针;否则,返回NULL。 18 | extern void* dlink_get_first(); 19 | // 获取“双向链表中最后1个元素”。成功,返回节点指针;否则,返回NULL。 20 | extern void* dlink_get_last(); 21 | 22 | // 将“value”插入到index位置。成功,返回0;否则,返回-1。 23 | extern int dlink_insert(int index, void *pval); 24 | // 将“value”插入到表头位置。成功,返回0;否则,返回-1。 25 | extern int dlink_insert_first(void *pval); 26 | // 将“value”插入到末尾位置。成功,返回0;否则,返回-1。 27 | extern int dlink_append_last(void *pval); 28 | 29 | // 删除“双向链表中index位置的节点”。成功,返回0;否则,返回-1 30 | extern int dlink_delete(int index); 31 | // 删除第一个节点。成功,返回0;否则,返回-1 32 | extern int dlink_delete_first(); 33 | // 删除组后一个节点。成功,返回0;否则,返回-1 34 | extern int dlink_delete_last(); 35 | 36 | #endif 37 | 38 | -------------------------------------------------------------------------------- /source/tree/splaytree/cplus/SplayTreeTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * C++ 语言: 伸展树 3 | * 4 | * @author skywang 5 | * @date 2014/02/03 6 | */ 7 | 8 | #include 9 | #include "SplayTree.h" 10 | using namespace std; 11 | 12 | static int arr[]= {10,50,40,30,20,60}; 13 | #define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) ) 14 | 15 | int main() 16 | { 17 | int i,ilen; 18 | SplayTree* tree=new SplayTree(); 19 | 20 | cout << "== 依次添加: "; 21 | ilen = TBL_SIZE(arr); 22 | for(i=0; iinsert(arr[i]); 26 | } 27 | 28 | cout << "\n== 前序遍历: "; 29 | tree->preOrder(); 30 | 31 | cout << "\n== 中序遍历: "; 32 | tree->inOrder(); 33 | 34 | cout << "\n== 后序遍历: "; 35 | tree->postOrder(); 36 | cout << endl; 37 | 38 | cout << "== 最小值: " << tree->minimum() << endl; 39 | cout << "== 最大值: " << tree->maximum() << endl; 40 | cout << "== 树的详细信息: " << endl; 41 | tree->print(); 42 | 43 | i = 30; 44 | cout << "\n== 旋转节点(" << i << ")为根节点"; 45 | tree->splay(i); 46 | cout << "\n== 树的详细信息: " << endl; 47 | tree->print(); 48 | 49 | // 销毁二叉树 50 | tree->destroy(); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /source/tree/bstree/java/BSTreeTest.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Java 语言: 二叉查找树 4 | * 5 | * @author skywang 6 | * @date 2013/11/07 7 | */ 8 | public class BSTreeTest { 9 | 10 | private static final int arr[] = {1,5,4,3,2,6}; 11 | 12 | public static void main(String[] args) { 13 | int i, ilen; 14 | BSTree tree=new BSTree(); 15 | 16 | System.out.print("== 依次添加: "); 17 | ilen = arr.length; 18 | for(i=0; i tree=new SplayTree(); 15 | 16 | System.out.print("== 依次添加: "); 17 | ilen = arr.length; 18 | for(i=0; i 9 | using namespace std; 10 | 11 | /* 12 | * 直接插入排序 13 | * 14 | * 参数说明: 15 | * a -- 待排序的数组 16 | * n -- 数组的长度 17 | */ 18 | void insertSort(int* a, int n) 19 | { 20 | int i, j, k; 21 | 22 | for (i = 1; i < n; i++) 23 | { 24 | //为a[i]在前面的a[0...i-1]有序区间中找一个合适的位置 25 | for (j = i - 1; j >= 0; j--) 26 | if (a[j] < a[i]) 27 | break; 28 | 29 | //如找到了一个合适的位置 30 | if (j != i - 1) 31 | { 32 | //将比a[i]大的数据向后移 33 | int temp = a[i]; 34 | for (k = i - 1; k > j; k--) 35 | a[k + 1] = a[k]; 36 | //将a[i]放到正确位置上 37 | a[k + 1] = temp; 38 | } 39 | } 40 | } 41 | 42 | int main() 43 | { 44 | int i; 45 | int a[] = {20,40,30,10,60,50}; 46 | int ilen = (sizeof(a)) / (sizeof(a[0])); 47 | 48 | cout << "before sort:"; 49 | for (i=0; i 9 | 10 | // 数组长度 11 | #define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) ) 12 | 13 | /* 14 | * 直接插入排序 15 | * 16 | * 参数说明: 17 | * a -- 待排序的数组 18 | * n -- 数组的长度 19 | */ 20 | void insert_sort(int a[], int n) 21 | { 22 | int i, j, k; 23 | 24 | for (i = 1; i < n; i++) 25 | { 26 | //为a[i]在前面的a[0...i-1]有序区间中找一个合适的位置 27 | for (j = i - 1; j >= 0; j--) 28 | if (a[j] < a[i]) 29 | break; 30 | 31 | //如找到了一个合适的位置 32 | if (j != i - 1) 33 | { 34 | //将比a[i]大的数据向后移 35 | int temp = a[i]; 36 | for (k = i - 1; k > j; k--) 37 | a[k + 1] = a[k]; 38 | //将a[i]放到正确位置上 39 | a[k + 1] = temp; 40 | } 41 | } 42 | } 43 | 44 | void main() 45 | { 46 | int i; 47 | int a[] = {20,40,30,10,60,50}; 48 | int ilen = LENGTH(a); 49 | 50 | printf("before sort:"); 51 | for (i=0; i 9 | #include "splay_tree.h" 10 | 11 | static int arr[]= {10,50,40,30,20,60}; 12 | #define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) ) 13 | 14 | void main() 15 | { 16 | int i, ilen; 17 | SplayTree root=NULL; 18 | 19 | printf("== 依次添加: "); 20 | ilen = TBL_SIZE(arr); 21 | for(i=0; ikey); 38 | printf("== 最大值: %d\n", splaytree_maximum(root)->key); 39 | printf("== 树的详细信息: \n"); 40 | print_splaytree(root, root->key, 0); 41 | 42 | i = 30; 43 | printf("\n== 旋转节点(%d)为根节点\n", i); 44 | printf("== 树的详细信息: \n"); 45 | root = splaytree_splay(root, i); 46 | print_splaytree(root, root->key, 0); 47 | 48 | // 销毁伸展树 49 | destroy_splaytree(root); 50 | } 51 | -------------------------------------------------------------------------------- /source/tree/avl_tree/c/avltree.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _AVL_TREE_H_ 3 | #define _AVL_TREE_H_ 4 | 5 | typedef int Type; 6 | 7 | typedef struct AVLTreeNode{ 8 | Type key; // 关键字(键值) 9 | int height; 10 | struct AVLTreeNode *left; // 左孩子 11 | struct AVLTreeNode *right; // 右孩子 12 | }Node, *AVLTree; 13 | 14 | // 获取AVL树的高度 15 | int avltree_height(AVLTree tree); 16 | 17 | // 前序遍历"AVL树" 18 | void preorder_avltree(AVLTree tree); 19 | // 中序遍历"AVL树" 20 | void inorder_avltree(AVLTree tree); 21 | // 后序遍历"AVL树" 22 | void postorder_avltree(AVLTree tree); 23 | 24 | void print_avltree(AVLTree tree, Type key, int direction); 25 | 26 | // (递归实现)查找"AVL树x"中键值为key的节点 27 | Node* avltree_search(AVLTree x, Type key); 28 | // (非递归实现)查找"AVL树x"中键值为key的节点 29 | Node* iterative_avltree_search(AVLTree x, Type key); 30 | 31 | // 查找最小结点:返回tree为根结点的AVL树的最小结点。 32 | Node* avltree_minimum(AVLTree tree); 33 | // 查找最大结点:返回tree为根结点的AVL树的最大结点。 34 | Node* avltree_maximum(AVLTree tree); 35 | 36 | // 将结点插入到AVL树中,返回根节点 37 | Node* avltree_insert(AVLTree tree, Type key); 38 | 39 | // 删除结点(key是节点值),返回根节点 40 | Node* avltree_delete(AVLTree tree, Type key); 41 | 42 | // 销毁AVL树 43 | void destroy_avltree(AVLTree tree); 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /source/algrightm/sort/selection_sort/c/select_sort.c: -------------------------------------------------------------------------------- 1 | /** 2 | * 选择排序:C 语言 3 | * 4 | * @author skywang 5 | * @date 2014/03/11 6 | */ 7 | 8 | #include 9 | 10 | // 数组长度 11 | #define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) ) 12 | #define swap(a,b) (a^=b,b^=a,a^=b) 13 | 14 | /* 15 | * 选择排序 16 | * 17 | * 参数说明: 18 | * a -- 待排序的数组 19 | * n -- 数组的长度 20 | */ 21 | void select_sort(int a[], int n) 22 | { 23 | int i; // 有序区的末尾位置 24 | int j; // 无序区的起始位置 25 | int min; // 无序区中最小元素位置 26 | 27 | for(i=0; i 9 | using namespace std; 10 | 11 | /* 12 | * 选择排序 13 | * 14 | * 参数说明: 15 | * a -- 待排序的数组 16 | * n -- 数组的长度 17 | */ 18 | void selectSort(int* a, int n) 19 | { 20 | int i; // 有序区的末尾位置 21 | int j; // 无序区的起始位置 22 | int min; // 无序区中最小元素位置 23 | 24 | for(i=0; i= 0; j--) 24 | if (a[j] < a[i]) 25 | break; 26 | 27 | //如找到了一个合适的位置 28 | if (j != i - 1) { 29 | //将比a[i]大的数据向后移 30 | int temp = a[i]; 31 | for (k = i - 1; k > j; k--) 32 | a[k + 1] = a[k]; 33 | //将a[i]放到正确位置上 34 | a[k + 1] = temp; 35 | } 36 | } 37 | } 38 | 39 | public static void main(String[] args) { 40 | int i; 41 | int[] a = {20,40,30,10,60,50}; 42 | 43 | System.out.printf("before sort:"); 44 | for (i=0; i0 ) { 33 | a[j++] = i; 34 | } 35 | } 36 | 37 | buckets = null; 38 | } 39 | 40 | public static void main(String[] args) { 41 | int i; 42 | int a[] = {8,2,3,4,3,6,6,3,9}; 43 | 44 | System.out.printf("before sort:"); 45 | for (i=0; i 9 | #include "AVLTree.h" 10 | using namespace std; 11 | 12 | static int arr[]= {3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9}; 13 | #define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) ) 14 | 15 | int main() 16 | { 17 | int i,ilen; 18 | AVLTree* tree=new AVLTree(); 19 | 20 | cout << "== 依次添加: "; 21 | ilen = TBL_SIZE(arr); 22 | for(i=0; iinsert(arr[i]); 26 | } 27 | 28 | cout << "\n== 前序遍历: "; 29 | tree->preOrder(); 30 | 31 | cout << "\n== 中序遍历: "; 32 | tree->inOrder(); 33 | 34 | cout << "\n== 后序遍历: "; 35 | tree->postOrder(); 36 | cout << endl; 37 | 38 | cout << "== 高度: " << tree->height() << endl; 39 | cout << "== 最小值: " << tree->minimum() << endl; 40 | cout << "== 最大值: " << tree->maximum() << endl; 41 | cout << "== 树的详细信息: " << endl; 42 | tree->print(); 43 | 44 | i = 8; 45 | cout << "\n== 删除根节点: " << i; 46 | tree->remove(i); 47 | 48 | cout << "\n== 高度: " << tree->height() ; 49 | cout << "\n== 中序遍历: " ; 50 | tree->inOrder(); 51 | cout << "\n== 树的详细信息: " << endl; 52 | tree->print(); 53 | 54 | // 销毁二叉树 55 | tree->destroy(); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /source/algrightm/sort/bucket_sort/cplus/BucketSort.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * 桶排序:C++ 3 | * 4 | * @author skywang 5 | * @date 2014/03/13 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | /* 13 | * 桶排序 14 | * 15 | * 参数说明: 16 | * a -- 待排序数组 17 | * n -- 数组a的长度 18 | * max -- 数组a中最大值的范围 19 | */ 20 | void bucketSort(int* a, int n, int max) 21 | { 22 | int i, j; 23 | int *buckets; 24 | 25 | if (a==NULL || n<1 || max<1) 26 | return ; 27 | 28 | // 创建一个容量为max的数组buckets,并且将buckets中的所有数据都初始化为0。 29 | if ((buckets = new int[max])==NULL) 30 | return ; 31 | memset(buckets, 0, max*sizeof(int)); 32 | 33 | // 1. 计数 34 | for(i = 0; i < n; i++) 35 | buckets[a[i]]++; 36 | 37 | // 2. 排序 38 | for (i = 0, j = 0; i < max; i++) 39 | while( (buckets[i]--) >0 ) 40 | a[j++] = i; 41 | 42 | delete[] buckets; 43 | } 44 | 45 | 46 | int main() 47 | { 48 | int i; 49 | int a[] = {8,2,3,4,3,6,6,3,9}; 50 | int ilen = (sizeof(a)) / (sizeof(a[0])); 51 | 52 | cout << "before sort:"; 53 | for (i=0; i x) 28 | j--; // 从右向左找第一个小于x的数 29 | if(i < j) 30 | a[i++] = a[j]; 31 | while(i < j && a[i] < x) 32 | i++; // 从左向右找第一个大于x的数 33 | if(i < j) 34 | a[j--] = a[i]; 35 | } 36 | a[i] = x; 37 | quickSort(a, l, i-1); /* 递归调用 */ 38 | quickSort(a, i+1, r); /* 递归调用 */ 39 | } 40 | } 41 | 42 | public static void main(String[] args) { 43 | int i; 44 | int a[] = {30,40,60,10,20,50}; 45 | 46 | System.out.printf("before sort:"); 47 | for (i=0; i tree = new AVLTree(); 14 | 15 | System.out.printf("== 依次添加: "); 16 | for(i=0; i 9 | using namespace std; 10 | 11 | /* 12 | * 快速排序 13 | * 14 | * 参数说明: 15 | * a -- 待排序的数组 16 | * l -- 数组的左边界(例如,从起始位置开始排序,则l=0) 17 | * r -- 数组的右边界(例如,排序截至到数组末尾,则r=a.length-1) 18 | */ 19 | void quickSort(int* a, int l, int r) 20 | { 21 | if (l < r) 22 | { 23 | int i,j,x; 24 | 25 | i = l; 26 | j = r; 27 | x = a[i]; 28 | while (i < j) 29 | { 30 | while(i < j && a[j] > x) 31 | j--; // 从右向左找第一个小于x的数 32 | if(i < j) 33 | a[i++] = a[j]; 34 | while(i < j && a[i] < x) 35 | i++; // 从左向右找第一个大于x的数 36 | if(i < j) 37 | a[j--] = a[i]; 38 | } 39 | a[i] = x; 40 | quickSort(a, l, i-1); /* 递归调用 */ 41 | quickSort(a, i+1, r); /* 递归调用 */ 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | int i; 48 | int a[] = {30,40,60,10,20,50}; 49 | int ilen = (sizeof(a)) / (sizeof(a[0])); 50 | 51 | cout << "before sort:"; 52 | for (i=0; i 9 | #include 10 | #include 11 | 12 | // 数组长度 13 | #define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) ) 14 | 15 | /* 16 | * 桶排序 17 | * 18 | * 参数说明: 19 | * a -- 待排序数组 20 | * n -- 数组a的长度 21 | * max -- 数组a中最大值的范围 22 | */ 23 | void bucket_sort(int a[], int n, int max) 24 | { 25 | int i, j; 26 | int *buckets; 27 | 28 | if (a==NULL || n<1 || max<1) 29 | return ; 30 | 31 | // 创建一个容量为max的数组buckets,并且将buckets中的所有数据都初始化为0。 32 | if ((buckets=(int *)malloc(max*sizeof(int)))==NULL) 33 | return ; 34 | memset(buckets, 0, max*sizeof(int)); 35 | 36 | // 1. 计数 37 | for(i = 0; i < n; i++) 38 | buckets[a[i]]++; 39 | 40 | // 2. 排序 41 | for (i = 0, j = 0; i < max; i++) 42 | while( (buckets[i]--) >0 ) 43 | a[j++] = i; 44 | 45 | free(buckets); 46 | } 47 | 48 | void main() 49 | { 50 | int i; 51 | int a[] = {8,2,3,4,3,6,6,3,9}; 52 | int ilen = LENGTH(a); 53 | 54 | printf("before sort:"); 55 | for (i=0; i 9 | 10 | // 数组长度 11 | #define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) ) 12 | 13 | /* 14 | * 快速排序 15 | * 16 | * 参数说明: 17 | * a -- 待排序的数组 18 | * l -- 数组的左边界(例如,从起始位置开始排序,则l=0) 19 | * r -- 数组的右边界(例如,排序截至到数组末尾,则r=a.length-1) 20 | */ 21 | void quick_sort(int a[], int l, int r) 22 | { 23 | if (l < r) 24 | { 25 | int i,j,x; 26 | 27 | i = l; 28 | j = r; 29 | x = a[i]; 30 | while (i < j) 31 | { 32 | while(i < j && a[j] > x) 33 | j--; // 从右向左找第一个小于x的数 34 | if(i < j) 35 | a[i++] = a[j]; 36 | while(i < j && a[i] < x) 37 | i++; // 从左向右找第一个大于x的数 38 | if(i < j) 39 | a[j--] = a[i]; 40 | } 41 | a[i] = x; 42 | quick_sort(a, l, i-1); /* 递归调用 */ 43 | quick_sort(a, i+1, r); /* 递归调用 */ 44 | } 45 | } 46 | 47 | void main() 48 | { 49 | int i; 50 | //int a[] = {30,40,60,10,20,50}; 51 | int a[] = {10,20,30,40,50,60}; 52 | int ilen = LENGTH(a); 53 | 54 | printf("before sort:"); 55 | for (i=0; i 8 | #include "avltree.h" 9 | 10 | static int arr[]= {3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9}; 11 | #define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) ) 12 | 13 | void main() 14 | { 15 | int i,ilen; 16 | AVLTree root=NULL; 17 | 18 | printf("== 依次添加: "); 19 | ilen = TBL_SIZE(arr); 20 | for(i=0; ikey, 0); 27 | printf("\n"); 28 | } 29 | 30 | printf("\n== 前序遍历: "); 31 | preorder_avltree(root); 32 | 33 | printf("\n== 中序遍历: "); 34 | inorder_avltree(root); 35 | 36 | printf("\n== 后序遍历: "); 37 | postorder_avltree(root); 38 | printf("\n"); 39 | 40 | printf("== 高度: %d\n", avltree_height(root)); 41 | printf("== 最小值: %d\n", avltree_minimum(root)->key); 42 | printf("== 最大值: %d\n", avltree_maximum(root)->key); 43 | printf("== 树的详细信息: \n"); 44 | print_avltree(root, root->key, 0); 45 | 46 | 47 | i = 8; 48 | printf("\n== 删除根节点: %d", i); 49 | root = avltree_delete(root, i); 50 | 51 | printf("\n== 高度: %d", avltree_height(root)); 52 | printf("\n== 中序遍历: "); 53 | inorder_avltree(root); 54 | printf("\n== 树的详细信息: \n"); 55 | print_avltree(root, root->key, 0); 56 | 57 | // 销毁二叉树 58 | destroy_avltree(root); 59 | } 60 | -------------------------------------------------------------------------------- /source/linear/stack/cplus/self/ArrayStack.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef ARRAY_STACK_HXX 3 | #define ARRAY_STACK_HXX 4 | 5 | #include 6 | #include "ArrayStack.h" 7 | using namespace std; 8 | 9 | template class ArrayStack{ 10 | public: 11 | ArrayStack(); 12 | ~ArrayStack(); 13 | 14 | void push(T t); 15 | T peek(); 16 | T pop(); 17 | int size(); 18 | int isEmpty(); 19 | private: 20 | T *arr; 21 | int count; 22 | }; 23 | 24 | // 创建“栈”,默认大小是12 25 | template 26 | ArrayStack::ArrayStack() 27 | { 28 | arr = new T[12]; 29 | if (!arr) 30 | { 31 | cout<<"arr malloc error!"< 37 | ArrayStack::~ArrayStack() 38 | { 39 | if (arr) 40 | { 41 | delete[] arr; 42 | arr = NULL; 43 | } 44 | } 45 | 46 | // 将val添加到栈中 47 | template 48 | void ArrayStack::push(T t) 49 | { 50 | //arr[count++] = val; 51 | arr[count++] = t; 52 | } 53 | 54 | // 返回“栈顶元素值” 55 | template 56 | T ArrayStack::peek() 57 | { 58 | return arr[count-1]; 59 | } 60 | 61 | // 返回“栈顶元素值”,并删除“栈顶元素” 62 | template 63 | T ArrayStack::pop() 64 | { 65 | int ret = arr[count-1]; 66 | count--; 67 | return ret; 68 | } 69 | 70 | // 返回“栈”的大小 71 | template 72 | int ArrayStack::size() 73 | { 74 | return count; 75 | } 76 | 77 | // 返回“栈”是否为空 78 | template 79 | int ArrayStack::isEmpty() 80 | { 81 | return size()==0; 82 | } 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /source/tree/rbtree/c/rbtree.h: -------------------------------------------------------------------------------- 1 | #ifndef _RED_BLACK_TREE_H_ 2 | #define _RED_BLACK_TREE_H_ 3 | 4 | #define RED 0 // 红色节点 5 | #define BLACK 1 // 黑色节点 6 | 7 | typedef int Type; 8 | 9 | // 红黑树的节点 10 | typedef struct RBTreeNode{ 11 | unsigned char color; // 颜色(RED 或 BLACK) 12 | Type key; // 关键字(键值) 13 | struct RBTreeNode *left; // 左孩子 14 | struct RBTreeNode *right; // 右孩子 15 | struct RBTreeNode *parent; // 父结点 16 | }Node, *RBTree; 17 | 18 | // 红黑树的根 19 | typedef struct rb_root{ 20 | Node *node; 21 | }RBRoot; 22 | 23 | // 创建红黑树,返回"红黑树的根"! 24 | RBRoot* create_rbtree(); 25 | 26 | // 销毁红黑树 27 | void destroy_rbtree(RBRoot *root); 28 | 29 | // 将结点插入到红黑树中。插入成功,返回0;失败返回-1。 30 | int insert_rbtree(RBRoot *root, Type key); 31 | 32 | // 删除结点(key为节点的值) 33 | void delete_rbtree(RBRoot *root, Type key); 34 | 35 | 36 | // 前序遍历"红黑树" 37 | void preorder_rbtree(RBRoot *root); 38 | // 中序遍历"红黑树" 39 | void inorder_rbtree(RBRoot *root); 40 | // 后序遍历"红黑树" 41 | void postorder_rbtree(RBRoot *root); 42 | 43 | // (递归实现)查找"红黑树"中键值为key的节点。找到的话,返回0;否则,返回-1。 44 | int rbtree_search(RBRoot *root, Type key); 45 | // (非递归实现)查找"红黑树"中键值为key的节点。找到的话,返回0;否则,返回-1。 46 | int iterative_rbtree_search(RBRoot *root, Type key); 47 | 48 | // 返回最小结点的值(将值保存到val中)。找到的话,返回0;否则返回-1。 49 | int rbtree_minimum(RBRoot *root, int *val); 50 | // 返回最大结点的值(将值保存到val中)。找到的话,返回0;否则返回-1。 51 | int rbtree_maximum(RBRoot *root, int *val); 52 | 53 | // 打印红黑树 54 | void print_rbtree(RBRoot *root); 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /source/linear/queue/c/double_link/dlink_queue.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "double_link.h" 4 | 5 | /** 6 | * C 语言: 双向链表实现“队列”,只能存储int数据。 7 | * 8 | * @author skywang 9 | * @date 2013/11/07 10 | */ 11 | 12 | // 创建队列 13 | int create_dlink_queue() 14 | { 15 | return create_dlink(); 16 | } 17 | 18 | // 销毁队列 19 | int destroy_dlink_queue() 20 | { 21 | return destroy_dlink(); 22 | } 23 | 24 | // 将val添加到队列的末尾 25 | int add(int val) 26 | { 27 | return dlink_append_last(val); 28 | } 29 | 30 | // 返回“队列开头元素” 31 | int front() 32 | { 33 | return dlink_get_first(); 34 | } 35 | 36 | // 返回并删除“队列开头元素” 37 | int pop() 38 | { 39 | int ret = dlink_get_first(); 40 | dlink_delete_first(); 41 | return ret; 42 | } 43 | 44 | // 返回“队列”的大小 45 | int size() 46 | { 47 | return dlink_size(); 48 | } 49 | 50 | // 返回“队列”是否为空 51 | int is_empty() 52 | { 53 | return dlink_is_empty(); 54 | } 55 | 56 | void main() 57 | { 58 | int tmp=0; 59 | 60 | // 创建“队列” 61 | create_dlink_queue(); 62 | 63 | // 将10, 20, 30 依次队列中 64 | add(10); 65 | add(20); 66 | add(30); 67 | 68 | // 将“队列开头的元素”赋值给tmp,并删除“该元素” 69 | tmp = pop(); 70 | printf("tmp=%d\n", tmp); 71 | 72 | // 只将“队列开头的元素”赋值给tmp,不删除该元素. 73 | tmp = front(); 74 | printf("tmp=%d\n", tmp); 75 | 76 | add(40); 77 | 78 | printf("is_empty()=%d\n", is_empty()); 79 | printf("size()=%d\n", size()); 80 | while (!is_empty()) 81 | { 82 | printf("%d\n", pop()); 83 | } 84 | 85 | // 销毁队列 86 | destroy_dlink_queue(); 87 | } 88 | -------------------------------------------------------------------------------- /source/linear/queue/cplus/self/ArrayQueue.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef ARRAY_QUEUE_HXX 3 | #define ARRAY_QUEUE_HXX 4 | 5 | #include 6 | using namespace std; 7 | 8 | template class ArrayQueue{ 9 | public: 10 | ArrayQueue(); 11 | ~ArrayQueue(); 12 | 13 | void add(T t); 14 | T front(); 15 | T pop(); 16 | int size(); 17 | int is_empty(); 18 | 19 | private: 20 | T *arr; 21 | int count; 22 | }; 23 | 24 | // 创建“队列”,默认大小是12 25 | template 26 | ArrayQueue::ArrayQueue() 27 | { 28 | arr = new T[12]; 29 | if (!arr) 30 | { 31 | cout<<"arr malloc error!"< 37 | ArrayQueue::~ArrayQueue() 38 | { 39 | if (arr) 40 | { 41 | delete[] arr; 42 | arr = NULL; 43 | } 44 | } 45 | 46 | // 将val添加到队列的末尾 47 | template 48 | void ArrayQueue::add(T t) 49 | { 50 | arr[count++] = t; 51 | } 52 | 53 | 54 | // 返回“队列开头元素” 55 | template 56 | T ArrayQueue::front() 57 | { 58 | return arr[0]; 59 | } 60 | 61 | // 返回并删除“队列末尾的元素” 62 | template 63 | T ArrayQueue::pop() 64 | { 65 | int i = 0;; 66 | T ret = arr[0]; 67 | 68 | count--; 69 | while (i++ 77 | int ArrayQueue::size() 78 | { 79 | return count; 80 | } 81 | 82 | // 返回“队列”是否为空 83 | template 84 | int ArrayQueue::is_empty() 85 | { 86 | return count==0; 87 | } 88 | 89 | 90 | #endif 91 | -------------------------------------------------------------------------------- /source/linear/queue/java/util_queue/StackList.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Stack; 3 | 4 | /** 5 | * 用“栈”实现队列 6 | * 7 | * @author skywang 8 | */ 9 | public class StackList { 10 | 11 | // 向队列添加数据时:(01) 将“已有的全部数据”都移到mIn中。 (02) 将“新添加的数据”添加到mIn中。 12 | private Stack mIn = null; 13 | // 从队列获取元素时:(01) 将“已有的全部数据”都移到mOut中。(02) 返回并删除mOut栈顶元素。 14 | private Stack mOut = null; 15 | // 统计计数 16 | private int mCount = 0; 17 | 18 | public StackList() { 19 | mIn = new Stack(); 20 | mOut = new Stack(); 21 | mCount = 0; 22 | } 23 | 24 | private void add(T t) { 25 | // 将“已有的全部数据”都移到mIn中 26 | while (!mOut.empty()) 27 | mIn.push(mOut.pop()); 28 | 29 | // 将“新添加的数据”添加到mIn中 30 | mIn.push(t); 31 | // 统计数+1 32 | mCount++; 33 | } 34 | 35 | private T get() { 36 | // 将“已有的全部数据”都移到mOut中 37 | while (!mIn.empty()) 38 | mOut.push(mIn.pop()); 39 | // 统计数-1 40 | mCount--; 41 | 42 | // 返回并删除mOut栈顶元素 43 | return mOut.pop(); 44 | } 45 | 46 | private int size() { 47 | return mCount; 48 | } 49 | private boolean isEmpty() { 50 | return mCount==0; 51 | } 52 | 53 | public static void main(String[] args) { 54 | StackList slist = new StackList(); 55 | 56 | // 将10, 20, 30 依次推入栈中 57 | slist.add(10); 58 | slist.add(20); 59 | slist.add(30); 60 | 61 | System.out.printf("isEmpty()=%b\n", slist.isEmpty()); 62 | System.out.printf("size()=%d\n", slist.size()); 63 | while(!slist.isEmpty()) { 64 | System.out.printf("%d\n", slist.get()); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /source/heap/fibonacci/c/fibonacci_heap.h: -------------------------------------------------------------------------------- 1 | #ifndef _FIBONACCI_HEAP_H_ 2 | #define _FIBONACCI_HEAP_H_ 3 | 4 | typedef int Type; 5 | 6 | typedef struct _FibonacciNode 7 | { 8 | Type key; // 关键字(键值) 9 | int degree; // 度数 10 | struct _FibonacciNode *left; // 左兄弟 11 | struct _FibonacciNode *right; // 右兄弟 12 | struct _FibonacciNode *child; // 第一个孩子节点 13 | struct _FibonacciNode *parent; // 父节点 14 | int marked; //是否被删除第1个孩子(1表示删除,0表示未删除) 15 | }FibonacciNode, FibNode; 16 | 17 | typedef struct _FibonacciHeap{ 18 | int keyNum; // 堆中节点的总数 19 | int maxDegree; // 最大度 20 | struct _FibonacciNode *min; // 最小节点(某个最小堆的根节点) 21 | struct _FibonacciNode **cons; // 最大度的内存区域 22 | }FibonacciHeap, FibHeap; 23 | 24 | // 创建Fibonacci堆 25 | FibHeap* fib_heap_make(); 26 | // 新建键值为key的节点,并将其插入到斐波那契堆中 27 | void fib_heap_insert_key(FibHeap *heap, Type key); 28 | // 删除键值为key的结点 29 | void fib_heap_delete(FibHeap *heap, Type key); 30 | // 移除最小节点 31 | void fib_heap_extract_min(FibHeap *heap); 32 | // 更新heap的中的oldkey为newkey 33 | void fib_heap_update(FibHeap *heap, Type oldkey, Type newkey); 34 | // 将h1, h2合并成一个堆,并返回合并后的堆 35 | FibHeap* fib_heap_union(FibHeap *h1, FibHeap *h2); 36 | // 在斐波那契堆heap中是否存在键值为key的节点;存在返回1,否则返回0。 37 | int fib_heap_contains(FibHeap *heap, Type key); 38 | // 获取最小节点对应的值(保存在pkey中);成功返回1,失败返回0。 39 | int fib_heap_get_min(FibHeap *heap, Type *pkey); 40 | // 销毁斐波那契堆 41 | void fib_heap_destroy(FibHeap *heap); 42 | // 打印"斐波那契堆" 43 | void fib_print(FibHeap *heap); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /source/linear/stack/c/double_link/dlink_stack.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "double_link.h" 4 | 5 | /** 6 | * C 语言: 双向链表实现栈,只能存储int数据。 7 | * 8 | * @author skywang 9 | * @date 2013/11/07 10 | */ 11 | // 创建栈 12 | int create_dlink_stack() 13 | { 14 | return create_dlink(); 15 | } 16 | 17 | // 销毁栈 18 | int destroy_dlink_stack() 19 | { 20 | return destroy_dlink(); 21 | } 22 | 23 | // 将val添加到栈中 24 | int push(int val) 25 | { 26 | return dlink_insert_first(val); 27 | } 28 | 29 | // 返回“栈顶元素值” 30 | int peek() 31 | { 32 | return dlink_get_first(); 33 | } 34 | 35 | // 返回“栈顶元素值”,并删除“栈顶元素” 36 | int pop() 37 | { 38 | int ret = peek(); 39 | dlink_delete_first(); 40 | return ret; 41 | } 42 | 43 | // 返回“栈”的大小 44 | int size() 45 | { 46 | return dlink_size(); 47 | } 48 | 49 | // 返回“栈”是否为空 50 | int is_empty() 51 | { 52 | return dlink_is_empty(); 53 | } 54 | 55 | // 打印“栈” 56 | void print_dlink_stack() 57 | { 58 | return print_dlink(); 59 | } 60 | 61 | void main() 62 | { 63 | int tmp=0; 64 | 65 | // 创建“栈” 66 | create_dlink_stack(); 67 | 68 | // 将10, 20, 30 依次推入栈中 69 | push(10); 70 | push(20); 71 | push(30); 72 | 73 | //print_dlink_stack(); // 打印栈 74 | 75 | // 将“栈顶元素”赋值给tmp,并删除“栈顶元素” 76 | tmp = pop(); 77 | printf("tmp=%d\n", tmp); 78 | //print_dlink_stack(); // 打印栈 79 | 80 | // 只将“栈顶”赋值给tmp,不删除该元素. 81 | tmp = peek(); 82 | printf("tmp=%d\n", tmp); 83 | //print_dlink_stack(); // 打印栈 84 | 85 | push(40); 86 | print_dlink_stack(); // 打印栈 87 | 88 | // 销毁栈 89 | destroy_dlink_stack(); 90 | } 91 | -------------------------------------------------------------------------------- /source/algrightm/sort/bubble/c/bubble_sort.c: -------------------------------------------------------------------------------- 1 | /** 2 | * 冒泡排序:C 语言 3 | * 4 | * @author skywang 5 | * @date 2014/03/11 6 | */ 7 | 8 | #include 9 | 10 | // 数组长度 11 | #define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) ) 12 | // 交互数值 13 | #define swap(a,b) (a^=b,b^=a,a^=b) 14 | 15 | /* 16 | * 冒泡排序 17 | * 18 | * 参数说明: 19 | * a -- 待排序的数组 20 | * n -- 数组的长度 21 | */ 22 | void bubble_sort1(int a[], int n) 23 | { 24 | int i,j; 25 | 26 | for (i=n-1; i>0; i--) 27 | { 28 | // 将a[0...i]中最大的数据放在末尾 29 | for (j=0; j a[j+1]) 32 | swap(a[j], a[j+1]); 33 | } 34 | } 35 | } 36 | 37 | /* 38 | * 冒泡排序(改进版) 39 | * 40 | * 参数说明: 41 | * a -- 待排序的数组 42 | * n -- 数组的长度 43 | */ 44 | void bubble_sort2(int a[], int n) 45 | { 46 | int i,j; 47 | int flag; // 标记 48 | 49 | for (i=n-1; i>0; i--) 50 | { 51 | flag = 0; // 初始化标记为0 52 | 53 | // 将a[0...i]中最大的数据放在末尾 54 | for (j=0; j a[j+1]) 57 | { 58 | swap(a[j], a[j+1]); 59 | flag = 1; // 若发生交换,则设标记为1 60 | } 61 | } 62 | 63 | if (flag==0) 64 | break; // 若没发生交换,则说明数列已有序。 65 | } 66 | } 67 | 68 | void main() 69 | { 70 | int i; 71 | int a[] = {20,40,30,10,60,50}; 72 | int ilen = LENGTH(a); 73 | 74 | printf("before sort:"); 75 | for (i=0; i 9 | #include "RBTree.h" 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | int a[]= {10, 40, 30, 60, 90, 70, 20, 50, 80}; 15 | int check_insert=0; // "插入"动作的检测开关(0,关闭;1,打开) 16 | int check_remove=0; // "删除"动作的检测开关(0,关闭;1,打开) 17 | int i; 18 | int ilen = (sizeof(a)) / (sizeof(a[0])) ; 19 | RBTree* tree=new RBTree(); 20 | 21 | cout << "== 原始数据: "; 22 | for(i=0; iinsert(a[i]); 29 | // 设置check_insert=1,测试"添加函数" 30 | if(check_insert) 31 | { 32 | cout << "== 添加节点: " << a[i] << endl; 33 | cout << "== 树的详细信息: " << endl; 34 | tree->print(); 35 | cout << endl; 36 | } 37 | 38 | } 39 | 40 | cout << "== 前序遍历: "; 41 | tree->preOrder(); 42 | 43 | cout << "\n== 中序遍历: "; 44 | tree->inOrder(); 45 | 46 | cout << "\n== 后序遍历: "; 47 | tree->postOrder(); 48 | cout << endl; 49 | 50 | cout << "== 最小值: " << tree->minimum() << endl; 51 | cout << "== 最大值: " << tree->maximum() << endl; 52 | cout << "== 树的详细信息: " << endl; 53 | tree->print(); 54 | 55 | // 设置check_remove=1,测试"删除函数" 56 | if(check_remove) 57 | { 58 | for(i=0; iremove(a[i]); 61 | 62 | cout << "== 删除节点: " << a[i] << endl; 63 | cout << "== 树的详细信息: " << endl; 64 | tree->print(); 65 | cout << endl; 66 | } 67 | } 68 | 69 | // 销毁红黑树 70 | tree->destroy(); 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /source/tree/rbtree/c/rbtree_test.c: -------------------------------------------------------------------------------- 1 | /** 2 | * C语言实现的红黑树(Red Black Tree) 3 | * 4 | * @author skywang 5 | * @date 2013/11/18 6 | */ 7 | 8 | #include 9 | #include "rbtree.h" 10 | 11 | #define CHECK_INSERT 0 // "插入"动作的检测开关(0,关闭;1,打开) 12 | #define CHECK_DELETE 0 // "删除"动作的检测开关(0,关闭;1,打开) 13 | #define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) ) 14 | 15 | void main() 16 | { 17 | int a[] = {10, 40, 30, 60, 90, 70, 20, 50, 80}; 18 | int i, ilen=LENGTH(a); 19 | RBRoot *root=NULL; 20 | 21 | root = create_rbtree(); 22 | printf("== 原始数据: "); 23 | for(i=0; i 9 | using namespace std; 10 | 11 | /* 12 | * 冒泡排序 13 | * 14 | * 参数说明: 15 | * a -- 待排序的数组 16 | * n -- 数组的长度 17 | */ 18 | void bubbleSort1(int* a, int n) 19 | { 20 | int i,j,tmp; 21 | 22 | for (i=n-1; i>0; i--) 23 | { 24 | // 将a[0...i]中最大的数据放在末尾 25 | for (j=0; j a[j+1]) 28 | { 29 | // 交换a[j]和a[j+1] 30 | tmp = a[j]; 31 | a[j] = a[j+1]; 32 | a[j+1] = tmp; 33 | } 34 | } 35 | } 36 | } 37 | 38 | /* 39 | * 冒泡排序(改进版) 40 | * 41 | * 参数说明: 42 | * a -- 待排序的数组 43 | * n -- 数组的长度 44 | */ 45 | void bubbleSort2(int* a, int n) 46 | { 47 | int i,j,tmp; 48 | int flag; // 标记 49 | 50 | for (i=n-1; i>0; i--) 51 | { 52 | flag = 0; // 初始化标记为0 53 | 54 | // 将a[0...i]中最大的数据放在末尾 55 | for (j=0; j a[j+1]) 58 | { 59 | // 交换a[j]和a[j+1] 60 | tmp = a[j]; 61 | a[j] = a[j+1]; 62 | a[j+1] = tmp; 63 | 64 | flag = 1; // 若发生交换,则设标记为1 65 | } 66 | } 67 | 68 | if (flag==0) 69 | break; // 若没发生交换,则说明数列已有序。 70 | } 71 | } 72 | 73 | int main() 74 | { 75 | int i; 76 | int a[] = {20,40,30,10,60,50}; 77 | int ilen = (sizeof(a)) / (sizeof(a[0])); 78 | 79 | cout << "before sort:"; 80 | for (i=0; i=0) { 56 | System.out.printf("%d\n", mArray[i]); 57 | i--; 58 | } 59 | } 60 | 61 | public static void main(String[] args) { 62 | int tmp=0; 63 | ArrayStack astack = new ArrayStack(); 64 | 65 | // 将10, 20, 30 依次推入栈中 66 | astack.push(10); 67 | astack.push(20); 68 | astack.push(30); 69 | 70 | // 将“栈顶元素”赋值给tmp,并删除“栈顶元素” 71 | tmp = astack.pop(); 72 | System.out.printf("tmp=%d\n", tmp); 73 | 74 | // 只将“栈顶”赋值给tmp,不删除该元素. 75 | tmp = astack.peek(); 76 | System.out.printf("tmp=%d\n", tmp); 77 | 78 | astack.push(40); 79 | astack.PrintArrayStack(); // 打印栈 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /source/algrightm/sort/bubble/java/BubbleSort.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 冒泡排序:Java 3 | * 4 | * @author skywang 5 | * @date 2014/03/11 6 | */ 7 | 8 | public class BubbleSort { 9 | 10 | /* 11 | * 冒泡排序 12 | * 13 | * 参数说明: 14 | * a -- 待排序的数组 15 | * n -- 数组的长度 16 | */ 17 | public static void bubbleSort1(int[] a, int n) { 18 | int i,j; 19 | 20 | for (i=n-1; i>0; i--) { 21 | // 将a[0...i]中最大的数据放在末尾 22 | for (j=0; j a[j+1]) { 25 | // 交换a[j]和a[j+1] 26 | int tmp = a[j]; 27 | a[j] = a[j+1]; 28 | a[j+1] = tmp; 29 | } 30 | } 31 | } 32 | } 33 | 34 | /* 35 | * 冒泡排序(改进版) 36 | * 37 | * 参数说明: 38 | * a -- 待排序的数组 39 | * n -- 数组的长度 40 | */ 41 | public static void bubbleSort2(int[] a, int n) { 42 | int i,j; 43 | int flag; // 标记 44 | 45 | for (i=n-1; i>0; i--) { 46 | 47 | flag = 0; // 初始化标记为0 48 | // 将a[0...i]中最大的数据放在末尾 49 | for (j=0; j a[j+1]) { 51 | // 交换a[j]和a[j+1] 52 | int tmp = a[j]; 53 | a[j] = a[j+1]; 54 | a[j+1] = tmp; 55 | 56 | flag = 1; // 若发生交换,则设标记为1 57 | } 58 | } 59 | 60 | if (flag==0) 61 | break; // 若没发生交换,则说明数列已有序。 62 | } 63 | } 64 | 65 | public static void main(String[] args) { 66 | int i; 67 | int[] a = {20,40,30,10,60,50}; 68 | 69 | System.out.printf("before sort:"); 70 | for (i=0; i 3 | #include 4 | #include 5 | #include "list.h" 6 | 7 | struct person 8 | { 9 | int age; 10 | char name[20]; 11 | struct list_head list; 12 | }; 13 | 14 | void main(int argc, char* argv[]) 15 | { 16 | struct person *pperson; 17 | struct person person_head; 18 | struct list_head *pos, *next; 19 | int i; 20 | 21 | // 初始化双链表的表头 22 | INIT_LIST_HEAD(&person_head.list); 23 | 24 | // 添加节点 25 | for (i=0; i<5; i++) 26 | { 27 | pperson = (struct person*)malloc(sizeof(struct person)); 28 | pperson->age = (i+1)*10; 29 | sprintf(pperson->name, "%d", i+1); 30 | // 将节点链接到链表的末尾 31 | // 如果想把节点链接到链表的表头后面,则使用 list_add 32 | list_add_tail(&(pperson->list), &(person_head.list)); 33 | } 34 | 35 | // 遍历链表 36 | printf("==== 1st iterator d-link ====\n"); 37 | list_for_each(pos, &person_head.list) 38 | { 39 | pperson = list_entry(pos, struct person, list); 40 | printf("name:%-2s, age:%d\n", pperson->name, pperson->age); 41 | } 42 | 43 | // 删除节点age为20的节点 44 | printf("==== delete node(age:20) ====\n"); 45 | list_for_each_safe(pos, next, &person_head.list) 46 | { 47 | pperson = list_entry(pos, struct person, list); 48 | if(pperson->age == 20) 49 | { 50 | list_del_init(pos); 51 | free(pperson); 52 | } 53 | } 54 | 55 | // 再次遍历链表 56 | printf("==== 2nd iterator d-link ====\n"); 57 | list_for_each(pos, &person_head.list) 58 | { 59 | pperson = list_entry(pos, struct person, list); 60 | printf("name:%-2s, age:%d\n", pperson->name, pperson->age); 61 | } 62 | 63 | // 释放资源 64 | list_for_each_safe(pos, next, &person_head.list) 65 | { 66 | pperson = list_entry(pos, struct person, list); 67 | list_del_init(pos); 68 | free(pperson); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /source/linear/linux_dlink/linux/test.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include "list.h" 6 | 7 | struct person 8 | { 9 | int age; 10 | char name[20]; 11 | struct list_head list; 12 | }; 13 | 14 | void main(int argc, char* argv[]) 15 | { 16 | struct person *pperson; 17 | struct person person_head; 18 | struct list_head *pos, *next; 19 | int i; 20 | 21 | // 初始化双链表的表头 22 | INIT_LIST_HEAD(&person_head.list); 23 | 24 | // 添加节点 25 | for (i=0; i<5; i++) 26 | { 27 | pperson = (struct person*)malloc(sizeof(struct person)); 28 | pperson->age = (i+1)*10; 29 | sprintf(pperson->name, "%d", i+1); 30 | // 将节点链接到链表的末尾 31 | // 如果想把节点链接到链表的表头后面,则使用 list_add 32 | list_add_tail(&(pperson->list), &(person_head.list)); 33 | } 34 | 35 | // 遍历链表 36 | printf("==== 1st iterator d-link ====\n"); 37 | list_for_each(pos, &person_head.list) 38 | { 39 | pperson = list_entry(pos, struct person, list); 40 | printf("name:%-2s, age:%d\n", pperson->name, pperson->age); 41 | } 42 | 43 | // 删除节点age为20的节点 44 | printf("==== delete node(age:20) ====\n"); 45 | list_for_each_safe(pos, next, &person_head.list) 46 | { 47 | pperson = list_entry(pos, struct person, list); 48 | if(pperson->age == 20) 49 | { 50 | list_del_init(pos); 51 | free(pperson); 52 | } 53 | } 54 | 55 | // 再次遍历链表 56 | printf("==== 2nd iterator d-link ====\n"); 57 | list_for_each(pos, &person_head.list) 58 | { 59 | pperson = list_entry(pos, struct person, list); 60 | printf("name:%-2s, age:%d\n", pperson->name, pperson->age); 61 | } 62 | 63 | // 释放资源 64 | list_for_each_safe(pos, next, &person_head.list) 65 | { 66 | pperson = list_entry(pos, struct person, list); 67 | list_del_init(pos); 68 | free(pperson); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /source/tree/rbtree/java/RBTreeTest.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Java 语言: 二叉查找树 4 | * 5 | * @author skywang 6 | * @date 2013/11/07 7 | */ 8 | public class RBTreeTest { 9 | 10 | private static final int a[] = {10, 40, 30, 60, 90, 70, 20, 50, 80}; 11 | private static final boolean mDebugInsert = false; // "插入"动作的检测开关(false,关闭;true,打开) 12 | private static final boolean mDebugDelete = false; // "删除"动作的检测开关(false,关闭;true,打开) 13 | 14 | public static void main(String[] args) { 15 | int i, ilen = a.length; 16 | RBTree tree=new RBTree(); 17 | 18 | System.out.printf("== 原始数据: "); 19 | for(i=0; i 2 | #include 3 | 4 | /** 5 | * C 语言: 数组实现的队列,只能存储int数据。 6 | * 7 | * @author skywang 8 | * @date 2013/11/07 9 | */ 10 | 11 | // 保存数据的数组 12 | static int *arr=NULL; 13 | // 队列的实际大小 14 | static int count; 15 | 16 | // 创建“队列” 17 | int create_array_queue(int sz) 18 | { 19 | arr = (int *)malloc(sz*sizeof(int)); 20 | if (!arr) 21 | { 22 | printf("arr malloc error!"); 23 | return -1; 24 | } 25 | count = 0; 26 | 27 | return 0; 28 | } 29 | 30 | // 销毁“队列” 31 | int destroy_array_queue() 32 | { 33 | if (arr) 34 | { 35 | free(arr); 36 | arr = NULL; 37 | } 38 | 39 | return 0; 40 | } 41 | 42 | // 将val添加到队列的末尾 43 | void add(int val) 44 | { 45 | arr[count++] = val; 46 | } 47 | 48 | // 返回“队列开头元素” 49 | int front() 50 | { 51 | return arr[0]; 52 | } 53 | 54 | // 返回并删除“队列开头元素” 55 | int pop() 56 | { 57 | int i = 0;; 58 | int ret = arr[0]; 59 | 60 | count--; 61 | while (i++ 3 | #include "list.h" 4 | #include "double_link.h" 5 | 6 | typedef struct tag_stu 7 | { 8 | int id; 9 | char name[20]; 10 | }stu; 11 | 12 | static stu arr_stu[] = 13 | { 14 | {10, "sky"}, 15 | {20, "jody"}, 16 | {30, "vic"}, 17 | {40, "dan"}, 18 | }; 19 | #define ARR_STU_SIZE ( (sizeof(arr_stu)) / (sizeof(arr_stu[0])) ) 20 | 21 | 22 | /** 23 | * C 语言: 双向链表实现栈,只能存储int数据。 24 | * 25 | * @author skywang 26 | * @date 2013/11/07 27 | */ 28 | // 创建栈 29 | int create_dlink_stack() 30 | { 31 | return create_dlink(); 32 | } 33 | 34 | // 销毁栈 35 | int destroy_dlink_stack() 36 | { 37 | return destroy_dlink(); 38 | } 39 | 40 | // 将val添加到栈中 41 | int push(int val) 42 | { 43 | return dlink_insert_first(val); 44 | } 45 | 46 | // 返回“栈顶元素值” 47 | int peek() 48 | { 49 | return dlink_get_first(); 50 | } 51 | 52 | // 返回“栈顶元素值”,并删除“栈顶元素” 53 | int pop() 54 | { 55 | int ret = peek(); 56 | dlink_delete_first(); 57 | return ret; 58 | } 59 | 60 | // 返回“栈”的大小 61 | int size() 62 | { 63 | return dlink_size(); 64 | } 65 | 66 | // 返回“栈”是否为空 67 | int is_empty() 68 | { 69 | return dlink_is_empty(); 70 | } 71 | 72 | // 打印“栈” 73 | void print_dlink_stack() 74 | { 75 | return print_dlink(); 76 | } 77 | 78 | void main() 79 | { 80 | stu *pval=NULL; 81 | INIT_LIST_HEAD(); 82 | // 创建“栈” 83 | create_dlink_stack(); 84 | 85 | // 将10, 20, 30 依次推入栈中 86 | push(10); 87 | push(20); 88 | push(30); 89 | 90 | //print_dlink_stack(); // 打印栈 91 | 92 | // 将“栈顶元素”赋值给tmp,并删除“栈顶元素” 93 | tmp = pop(); 94 | printf("tmp=%d\n", tmp); 95 | //print_dlink_stack(); // 打印栈 96 | 97 | // 只将“栈顶”赋值给tmp,不删除该元素. 98 | tmp = peek(); 99 | printf("tmp=%d\n", tmp); 100 | //print_dlink_stack(); // 打印栈 101 | 102 | push(40); 103 | print_dlink_stack(); // 打印栈 104 | 105 | // 销毁栈 106 | destroy_dlink_stack(); 107 | } 108 | -------------------------------------------------------------------------------- /source/linear/linux_dlink/linux/dlink_stack.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "list.h" 4 | #include "double_link.h" 5 | 6 | typedef struct tag_stu 7 | { 8 | int id; 9 | char name[20]; 10 | }stu; 11 | 12 | static stu arr_stu[] = 13 | { 14 | {10, "sky"}, 15 | {20, "jody"}, 16 | {30, "vic"}, 17 | {40, "dan"}, 18 | }; 19 | #define ARR_STU_SIZE ( (sizeof(arr_stu)) / (sizeof(arr_stu[0])) ) 20 | 21 | 22 | /** 23 | * C 语言: 双向链表实现栈,只能存储int数据。 24 | * 25 | * @author skywang 26 | * @date 2013/11/07 27 | */ 28 | // 创建栈 29 | int create_dlink_stack() 30 | { 31 | return create_dlink(); 32 | } 33 | 34 | // 销毁栈 35 | int destroy_dlink_stack() 36 | { 37 | return destroy_dlink(); 38 | } 39 | 40 | // 将val添加到栈中 41 | int push(int val) 42 | { 43 | return dlink_insert_first(val); 44 | } 45 | 46 | // 返回“栈顶元素值” 47 | int peek() 48 | { 49 | return dlink_get_first(); 50 | } 51 | 52 | // 返回“栈顶元素值”,并删除“栈顶元素” 53 | int pop() 54 | { 55 | int ret = peek(); 56 | dlink_delete_first(); 57 | return ret; 58 | } 59 | 60 | // 返回“栈”的大小 61 | int size() 62 | { 63 | return dlink_size(); 64 | } 65 | 66 | // 返回“栈”是否为空 67 | int is_empty() 68 | { 69 | return dlink_is_empty(); 70 | } 71 | 72 | // 打印“栈” 73 | void print_dlink_stack() 74 | { 75 | return print_dlink(); 76 | } 77 | 78 | void main() 79 | { 80 | stu *pval=NULL; 81 | INIT_LIST_HEAD(); 82 | // 创建“栈” 83 | create_dlink_stack(); 84 | 85 | // 将10, 20, 30 依次推入栈中 86 | push(10); 87 | push(20); 88 | push(30); 89 | 90 | //print_dlink_stack(); // 打印栈 91 | 92 | // 将“栈顶元素”赋值给tmp,并删除“栈顶元素” 93 | tmp = pop(); 94 | printf("tmp=%d\n", tmp); 95 | //print_dlink_stack(); // 打印栈 96 | 97 | // 只将“栈顶”赋值给tmp,不删除该元素. 98 | tmp = peek(); 99 | printf("tmp=%d\n", tmp); 100 | //print_dlink_stack(); // 打印栈 101 | 102 | push(40); 103 | print_dlink_stack(); // 打印栈 104 | 105 | // 销毁栈 106 | destroy_dlink_stack(); 107 | } 108 | -------------------------------------------------------------------------------- /source/linear/stack/java/generical/GeneralArrayStack.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Java : 数组实现的栈,能存储任意类型的数据 4 | * 5 | * @author skywang 6 | * @date 2013/11/07 7 | */ 8 | import java.lang.reflect.Array; 9 | 10 | public class GeneralArrayStack { 11 | 12 | private static final int DEFAULT_SIZE = 12; 13 | private T[] mArray; 14 | private int count; 15 | 16 | public GeneralArrayStack(Class type) { 17 | this(type, DEFAULT_SIZE); 18 | } 19 | 20 | public GeneralArrayStack(Class type, int size) { 21 | // 不能直接使用mArray = new T[DEFAULT_SIZE]; 22 | mArray = (T[]) Array.newInstance(type, size); 23 | count = 0; 24 | } 25 | 26 | // 将val添加到栈中 27 | public void push(T val) { 28 | mArray[count++] = val; 29 | } 30 | 31 | // 返回“栈顶元素值” 32 | public T peek() { 33 | return mArray[count-1]; 34 | } 35 | 36 | // 返回“栈顶元素值”,并删除“栈顶元素” 37 | public T pop() { 38 | T ret = mArray[count-1]; 39 | count--; 40 | return ret; 41 | } 42 | 43 | // 返回“栈”的大小 44 | public int size() { 45 | return count; 46 | } 47 | 48 | // 返回“栈”是否为空 49 | public boolean isEmpty() { 50 | return size()==0; 51 | } 52 | 53 | // 打印“栈” 54 | public void PrintArrayStack() { 55 | if (isEmpty()) { 56 | System.out.printf("stack is Empty\n"); 57 | } 58 | 59 | System.out.printf("stack size()=%d\n", size()); 60 | 61 | int i=size()-1; 62 | while (i>=0) { 63 | System.out.println(mArray[i]); 64 | i--; 65 | } 66 | } 67 | 68 | public static void main(String[] args) { 69 | String tmp; 70 | GeneralArrayStack astack = new GeneralArrayStack(String.class); 71 | 72 | // 将10, 20, 30 依次推入栈中 73 | astack.push("10"); 74 | astack.push("20"); 75 | astack.push("30"); 76 | 77 | // 将“栈顶元素”赋值给tmp,并删除“栈顶元素” 78 | tmp = astack.pop(); 79 | System.out.println("tmp="+tmp); 80 | 81 | // 只将“栈顶”赋值给tmp,不删除该元素. 82 | tmp = astack.peek(); 83 | System.out.println("tmp="+tmp); 84 | 85 | astack.push("40"); 86 | astack.PrintArrayStack(); // 打印栈 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /source/linear/stack/c/var_dlink/dlink_stack.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "double_link.h" 4 | 5 | /** 6 | * C 语言: 双向链表实现栈,能存储任意数据。 7 | * 8 | * @author skywang 9 | * @date 2013/11/07 10 | */ 11 | // 创建栈 12 | int create_dlink_stack() 13 | { 14 | return create_dlink(); 15 | } 16 | 17 | // 销毁栈 18 | int destroy_dlink_stack() 19 | { 20 | return destroy_dlink(); 21 | } 22 | 23 | // 将val添加到栈中 24 | int push(void *p) 25 | { 26 | return dlink_insert_first(p); 27 | } 28 | 29 | // 返回“栈顶元素值” 30 | void* peek() 31 | { 32 | return dlink_get_first(); 33 | } 34 | 35 | // 返回“栈顶元素值”,并删除“栈顶元素” 36 | void* pop() 37 | { 38 | void *p = peek(); 39 | dlink_delete_first(); 40 | return p; 41 | } 42 | 43 | // 返回“栈”的大小 44 | int size() 45 | { 46 | return dlink_size(); 47 | } 48 | 49 | // 返回“栈”是否为空 50 | int is_empty() 51 | { 52 | return dlink_is_empty(); 53 | } 54 | 55 | 56 | typedef struct tag_stu 57 | { 58 | int id; 59 | char name[20]; 60 | }stu; 61 | 62 | static stu arr_stu[] = 63 | { 64 | {10, "sky"}, 65 | {20, "jody"}, 66 | {30, "vic"}, 67 | {40, "dan"}, 68 | }; 69 | #define ARR_STU_SIZE ( (sizeof(arr_stu)) / (sizeof(arr_stu[0])) ) 70 | 71 | static void print_stu(stu *p) 72 | { 73 | if (!p) 74 | return ; 75 | 76 | printf("id=%d, name=%s\n", p->id, p->name); 77 | } 78 | 79 | void main() 80 | { 81 | stu *pval=NULL; 82 | 83 | // 创建“栈” 84 | create_dlink_stack(); 85 | 86 | // 将10, 20, 30 依次推入栈中 87 | int i=0; 88 | for (i=0; i { 11 | 12 | // 向队列添加数据时:(01) 将“已有的全部数据”都移到mIn中。 (02) 将“新添加的数据”添加到mIn中。 13 | private Stack mIn = null; 14 | // 从队列获取元素时:(01) 将“已有的全部数据”都移到mOut中。(02) 返回并删除mOut栈顶元素。 15 | private Stack mOut = null; 16 | // 统计计数 17 | private int mCount = 0; 18 | 19 | public StackList() { 20 | mIn = new Stack(); 21 | mOut = new Stack(); 22 | mCount = 0; 23 | } 24 | 25 | // 将t添加到队列中 26 | public void add(T t) { 27 | // 将“已有的全部数据”都移到mIn中 28 | while (!mOut.empty()) 29 | mIn.push(mOut.pop()); 30 | 31 | // 将“新添加的数据”添加到mIn中 32 | mIn.push(t); 33 | // 统计数+1 34 | mCount++; 35 | } 36 | 37 | // 返回队列开头的元素 38 | public T front() { 39 | // 将“已有的全部数据”都移到mOut中 40 | while (!mIn.empty()) 41 | mOut.push(mIn.pop()); 42 | 43 | // 返回mOut栈顶元素 44 | return mOut.peek(); 45 | } 46 | 47 | // 删除并返回队列开头的元素 48 | public T pop() { 49 | // 将“已有的全部数据”都移到mOut中 50 | while (!mIn.empty()) 51 | mOut.push(mIn.pop()); 52 | // 统计数-1 53 | mCount--; 54 | 55 | // 返回并删除mOut栈顶元素 56 | return mOut.pop(); 57 | } 58 | 59 | public int size() { 60 | return mCount; 61 | } 62 | public boolean isEmpty() { 63 | return mCount==0; 64 | } 65 | 66 | public static void main(String[] args) { 67 | int tmp; 68 | StackList slist = new StackList(); 69 | 70 | // 将10, 20, 30 依次推入栈中 71 | slist.add(10); 72 | slist.add(20); 73 | slist.add(30); 74 | 75 | // 将“队列开头的元素”赋值给tmp,并删除“该元素” 76 | tmp = slist.pop(); 77 | System.out.printf("tmp()=%d\n", tmp); 78 | 79 | // 只将“队列开头的元素”赋值给tmp,不删除该元素. 80 | tmp = slist.front(); 81 | System.out.printf("tmp()=%d\n", tmp); 82 | 83 | slist.add(40); 84 | 85 | System.out.printf("isEmpty()=%b\n", slist.isEmpty()); 86 | System.out.printf("size()=%d\n", slist.size()); 87 | while(!slist.isEmpty()) { 88 | System.out.printf("%d\n", slist.pop()); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /source/linear/stack/c/array/array_stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * C 语言: 数组实现的栈,只能存储int数据。 6 | * 7 | * @author skywang 8 | * @date 2013/11/07 9 | */ 10 | 11 | // 保存数据的数组 12 | static int *arr=NULL; 13 | // 栈的实际大小 14 | static int count; 15 | 16 | // 创建“栈”,默认大小是12 17 | int create_array_stack(int sz) 18 | { 19 | arr = (int *)malloc(sz*sizeof(int)); 20 | if (!arr) 21 | { 22 | printf("arr malloc error!"); 23 | return -1; 24 | } 25 | 26 | return 0; 27 | } 28 | 29 | // 销毁“栈” 30 | int destroy_array_stack() 31 | { 32 | if (arr) 33 | { 34 | free(arr); 35 | arr = NULL; 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | // 将val添加到栈中 42 | void push(int val) 43 | { 44 | arr[count++] = val; 45 | } 46 | 47 | // 返回“栈顶元素值” 48 | int peek() 49 | { 50 | return arr[count-1]; 51 | } 52 | 53 | // 返回“栈顶元素值”,并删除“栈顶元素” 54 | int pop() 55 | { 56 | int ret = arr[count-1]; 57 | count--; 58 | return ret; 59 | } 60 | 61 | // 返回“栈”的大小 62 | int size() 63 | { 64 | return count; 65 | } 66 | 67 | // 返回“栈”是否为空 68 | int is_empty() 69 | { 70 | return size()==0; 71 | } 72 | 73 | // 打印“栈” 74 | void print_array_stack() 75 | { 76 | if (is_empty()) 77 | { 78 | printf("stack is Empty\n"); 79 | return ; 80 | } 81 | 82 | printf("stack size()=%d\n", size()); 83 | 84 | int i=size()-1; 85 | while (i>=0) 86 | { 87 | printf("%d\n", arr[i]); 88 | i--; 89 | } 90 | } 91 | 92 | 93 | void main() 94 | { 95 | int tmp=0; 96 | 97 | // 创建“栈” 98 | create_array_stack(12); 99 | 100 | // 将10, 20, 30 依次推入栈中 101 | push(10); 102 | push(20); 103 | push(30); 104 | 105 | //print_array_stack(); // 打印栈 106 | 107 | // 将“栈顶元素”赋值给tmp,并删除“栈顶元素” 108 | tmp = pop(); 109 | printf("tmp=%d\n", tmp); 110 | //print_array_stack(); // 打印栈 111 | 112 | // 只将“栈顶”赋值给tmp,不删除该元素. 113 | tmp = peek(); 114 | printf("tmp=%d\n", tmp); 115 | //print_array_stack(); // 打印栈 116 | 117 | push(40); 118 | print_array_stack(); // 打印栈 119 | 120 | // 销毁栈 121 | destroy_array_stack(); 122 | } 123 | 124 | -------------------------------------------------------------------------------- /source/linear/queue/c/var_dlink/dlink_queue.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "double_link.h" 4 | 5 | /** 6 | * C 语言: 双向链表实现“队列”,能存储任意数据。 7 | * 8 | * @author skywang 9 | * @date 2013/11/07 10 | */ 11 | 12 | // 创建队列 13 | int create_dlink_queue() 14 | { 15 | return create_dlink(); 16 | } 17 | 18 | // 销毁队列 19 | int destroy_dlink_queue() 20 | { 21 | return destroy_dlink(); 22 | } 23 | 24 | // 将p添加到队列的末尾 25 | int add(void *p) 26 | { 27 | return dlink_append_last(p); 28 | } 29 | 30 | // 返回“队列开头元素” 31 | void* front() 32 | { 33 | return dlink_get_first(); 34 | } 35 | 36 | // 返回“队列开头的元素”,并删除“该元素” 37 | void* pop() 38 | { 39 | void *p = dlink_get_first(); 40 | dlink_delete_first(); 41 | return p; 42 | } 43 | 44 | // 返回“队列”的大小 45 | int size() 46 | { 47 | return dlink_size(); 48 | } 49 | 50 | // 返回“队列”是否为空 51 | int is_empty() 52 | { 53 | return dlink_is_empty(); 54 | } 55 | 56 | 57 | typedef struct tag_stu 58 | { 59 | int id; 60 | char name[20]; 61 | }stu; 62 | 63 | static stu arr_stu[] = 64 | { 65 | {10, "sky"}, 66 | {20, "jody"}, 67 | {30, "vic"}, 68 | {40, "dan"}, 69 | }; 70 | #define ARR_STU_SIZE ( (sizeof(arr_stu)) / (sizeof(arr_stu[0])) ) 71 | 72 | static void print_stu(stu *p) 73 | { 74 | if (!p) 75 | return ; 76 | 77 | printf("id=%d, name=%s\n", p->id, p->name); 78 | } 79 | 80 | void main() 81 | { 82 | stu *pval=NULL; 83 | 84 | // 创建“队列” 85 | create_dlink_queue(); 86 | 87 | // 将10, 20, 30 依次推入队列中 88 | int i=0; 89 | for (i=0; i 3 | using namespace std; 4 | 5 | #include "StackList.h" 6 | 7 | /** 8 | * c++语言: 用2个“栈”实现队列 9 | * 10 | * @author skywang 11 | * @date 2013/11/07 12 | */ 13 | 14 | template 15 | StackList::StackList() 16 | { 17 | count=0; 18 | } 19 | 20 | // 将t添加到队列中 21 | template 22 | void StackList::add(T t) 23 | { 24 | // 将“已有的全部数据”都移到sin中 25 | while (!sout.empty()) 26 | { 27 | sin.push(sout.top()); 28 | sout.pop(); 29 | } 30 | 31 | // 将“新添加的数据”添加到sin中。 32 | sin.push(t); 33 | // 统计数+1 34 | count++; 35 | } 36 | 37 | // 返回队列开头的元素 38 | template 39 | T StackList::front() 40 | { 41 | // 将“已有的全部数据”都移到sout中 42 | while (!sin.empty()) 43 | { 44 | sout.push(sin.top()); 45 | sin.pop(); 46 | } 47 | 48 | // 返回sout栈顶元素 49 | return sout.top(); 50 | } 51 | 52 | 53 | // 删除并返回队列开头的元素 54 | template 55 | T StackList::pop() 56 | { 57 | // 将“已有的全部数据”都移到sout中 58 | while (!sin.empty()) 59 | { 60 | sout.push(sin.top()); 61 | sin.pop(); 62 | } 63 | 64 | // 返回并删除sout栈顶元素 65 | T t = sout.top(); 66 | sout.pop(); 67 | // 统计数-1 68 | count--; 69 | 70 | return t; 71 | } 72 | 73 | template 74 | int StackList::size() 75 | { 76 | return count; 77 | } 78 | 79 | template 80 | int StackList::is_empty() 81 | { 82 | return count==0; 83 | } 84 | 85 | 86 | int main() 87 | { 88 | int tmp; 89 | 90 | // 创建“队列” 91 | StackList* plist = new StackList(); 92 | 93 | // 将10, 20, 30 依次推入队列中 94 | plist->add(10); 95 | plist->add(20); 96 | plist->add(30); 97 | 98 | // 将“队列开头的元素”赋值给tmp,并删除“该元素” 99 | tmp = plist->pop(); 100 | cout << "tmp=" << tmp <front(); 104 | cout << "tmp=" << tmp <add(40); 107 | 108 | // 打印队列 109 | cout << "is_empty()=" << plist->is_empty() <= 0; i--) 58 | { 59 | output[buckets[ (a[i]/exp)%10 ] - 1] = a[i]; 60 | buckets[ (a[i]/exp)%10 ]--; 61 | } 62 | 63 | // 将排序好的数据赋值给a[] 64 | for (i = 0; i < n; i++) 65 | a[i] = output[i]; 66 | } 67 | 68 | /* 69 | * 基数排序 70 | * 71 | * 参数说明: 72 | * a -- 数组 73 | * n -- 数组长度 74 | */ 75 | void radixSort(int a[], int n) 76 | { 77 | int exp; // 指数。当对数组按各位进行排序时,exp=1;按十位进行排序时,exp=10;... 78 | int max = getMax(a, n); // 数组a中的最大值 79 | 80 | // 从个位开始,对数组a按"指数"进行排序 81 | for (exp = 1; max/exp > 0; exp *= 10) 82 | countSort(a, n, exp); 83 | } 84 | 85 | int main() 86 | { 87 | int i; 88 | int a[] = {53, 3, 542, 748, 14, 214, 154, 63, 616}; 89 | int ilen = (sizeof(a)) / (sizeof(a[0])); 90 | 91 | cout << "before sort:"; 92 | for (i=0; i 9 | 10 | // 数组长度 11 | #define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) ) 12 | 13 | /* 14 | * 获取数组a中最大值 15 | * 16 | * 参数说明: 17 | * a -- 数组 18 | * n -- 数组长度 19 | */ 20 | int get_max(int a[], int n) 21 | { 22 | int i, max; 23 | 24 | max = a[0]; 25 | for (i = 1; i < n; i++) 26 | if (a[i] > max) 27 | max = a[i]; 28 | return max; 29 | } 30 | 31 | /* 32 | * 对数组按照"某个位数"进行排序(桶排序) 33 | * 34 | * 参数说明: 35 | * a -- 数组 36 | * n -- 数组长度 37 | * exp -- 指数。对数组a按照该指数进行排序。 38 | * 39 | * 例如,对于数组a={50, 3, 542, 745, 2014, 154, 63, 616}; 40 | * (01) 当exp=1表示按照"个位"对数组a进行排序 41 | * (02) 当exp=10表示按照"十位"对数组a进行排序 42 | * (03) 当exp=100表示按照"百位"对数组a进行排序 43 | * ... 44 | */ 45 | void count_sort(int a[], int n, int exp) 46 | { 47 | int output[n]; // 存储"被排序数据"的临时数组 48 | int i, buckets[10] = {0}; 49 | 50 | // 将数据出现的次数存储在buckets[]中 51 | for (i = 0; i < n; i++) 52 | buckets[ (a[i]/exp)%10 ]++; 53 | 54 | // 更改buckets[i]。目的是让更改后的buckets[i]的值,是该数据在output[]中的位置。 55 | for (i = 1; i < 10; i++) 56 | buckets[i] += buckets[i - 1]; 57 | 58 | // 将数据存储到临时数组output[]中 59 | for (i = n - 1; i >= 0; i--) 60 | { 61 | output[buckets[ (a[i]/exp)%10 ] - 1] = a[i]; 62 | buckets[ (a[i]/exp)%10 ]--; 63 | } 64 | 65 | // 将排序好的数据赋值给a[] 66 | for (i = 0; i < n; i++) 67 | a[i] = output[i]; 68 | } 69 | 70 | /* 71 | * 基数排序 72 | * 73 | * 参数说明: 74 | * a -- 数组 75 | * n -- 数组长度 76 | */ 77 | void radix_sort(int a[], int n) 78 | { 79 | int exp; // 指数。当对数组按各位进行排序时,exp=1;按十位进行排序时,exp=10;... 80 | int max = get_max(a, n); // 数组a中的最大值 81 | 82 | // 从个位开始,对数组a按"指数"进行排序 83 | for (exp = 1; max/exp > 0; exp *= 10) 84 | count_sort(a, n, exp); 85 | } 86 | 87 | void main() 88 | { 89 | int i; 90 | int a[] = {53, 3, 542, 748, 14, 214, 154, 63, 616}; 91 | int ilen = LENGTH(a); 92 | 93 | printf("before sort:"); 94 | for (i=0; i 3 | #include 4 | 5 | /** 6 | * C 语言: 单链表实现“队列”,只能存储int数据。 7 | * 8 | * @author skywang 9 | * @date 2013/11/07 10 | */ 11 | 12 | // 单链表节点 13 | struct node { 14 | int val; 15 | struct node* next; 16 | }; 17 | 18 | // 表头 19 | static struct node *phead=NULL; 20 | 21 | // 创建节点,val为节点值 22 | static struct node* create_node(val) 23 | { 24 | struct node *pnode=NULL; 25 | pnode = (struct node*)malloc(sizeof(struct node)); 26 | if (!pnode) 27 | return NULL; 28 | pnode->val = val; 29 | pnode->next = NULL; 30 | 31 | return pnode; 32 | } 33 | 34 | // 销毁单向链表 35 | static int destroy_single_link() 36 | { 37 | struct node *pnode=NULL; 38 | 39 | while (phead != NULL) 40 | { 41 | pnode = phead; 42 | phead = phead->next; 43 | free(pnode); 44 | } 45 | return 0; 46 | } 47 | 48 | // 将val添加到队列的末尾 49 | static void add(int val) 50 | { 51 | if (!phead) 52 | { 53 | phead = create_node(val); 54 | return ; 55 | } 56 | 57 | struct node *pnode = create_node(val); 58 | struct node *pend = phead; 59 | while (pend->next) 60 | pend = pend->next; 61 | 62 | pend->next = pnode; 63 | } 64 | 65 | // 返回“队列开头元素” 66 | int front() 67 | { 68 | return phead->val; 69 | } 70 | 71 | // 返回并删除“队列开头元素” 72 | static int pop() 73 | { 74 | int ret = phead->val; 75 | struct node *pnode = phead; 76 | 77 | phead = phead->next; 78 | free(pnode); 79 | 80 | return ret; 81 | } 82 | 83 | // 返回链表中节点的个数 84 | static int size() 85 | { 86 | int count=0; 87 | struct node *pend = phead; 88 | 89 | while (pend) 90 | { 91 | pend = pend->next; 92 | count++; 93 | } 94 | 95 | return count; 96 | } 97 | 98 | // 链表是否为空 99 | static int is_empty() 100 | { 101 | return size()==0; 102 | } 103 | 104 | void main() 105 | { 106 | int tmp=0; 107 | 108 | // 将10, 20, 30 依次加入到队列中 109 | add(10); 110 | add(20); 111 | add(30); 112 | 113 | // 将“队列开头元素”赋值给tmp,并删除“该元素” 114 | tmp = pop(); 115 | printf("tmp=%d\n", tmp); 116 | 117 | // 只将“队列开头的元素”赋值给tmp,不删除该元素. 118 | tmp = front(); 119 | printf("tmp=%d\n", tmp); 120 | 121 | add(40); 122 | 123 | printf("is_empty()=%d\n", is_empty()); 124 | printf("size()=%d\n", size()); 125 | while (!is_empty()) 126 | { 127 | printf("%d\n", pop()); 128 | } 129 | 130 | // 销毁队列 131 | destroy_single_link(); 132 | } 133 | -------------------------------------------------------------------------------- /source/algrightm/sort/shell_sort/cplus/ShellSort.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * 希尔排序:C++ 3 | * 4 | * @author skywang 5 | * @date 2014/03/11 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | /* 12 | * 希尔排序 13 | * 14 | * 参数说明: 15 | * a -- 待排序的数组 16 | * n -- 数组的长度 17 | */ 18 | void shellSort1(int* a, int n) 19 | { 20 | int i,j,gap; 21 | 22 | // gap为步长,每次减为原来的一半。 23 | for (gap = n / 2; gap > 0; gap /= 2) 24 | { 25 | // 共gap个组,对每一组都执行直接插入排序 26 | for (i = 0 ;i < gap; i++) 27 | { 28 | for (j = i + gap; j < n; j += gap) 29 | { 30 | // 如果a[j] < a[j-gap],则寻找a[j]位置,并将后面数据的位置都后移。 31 | if (a[j] < a[j - gap]) 32 | { 33 | int tmp = a[j]; 34 | int k = j - gap; 35 | while (k >= 0 && a[k] > tmp) 36 | { 37 | a[k + gap] = a[k]; 38 | k -= gap; 39 | } 40 | a[k + gap] = tmp; 41 | } 42 | } 43 | } 44 | 45 | } 46 | } 47 | 48 | /* 49 | * 对希尔排序中的单个组进行排序 50 | * 51 | * 参数说明: 52 | * a -- 待排序的数组 53 | * n -- 数组总的长度 54 | * i -- 组的起始位置 55 | * gap -- 组的步长 56 | * 57 | * 组是"从i开始,将相隔gap长度的数都取出"所组成的! 58 | */ 59 | void groupSort(int* a, int n, int i,int gap) 60 | { 61 | int j; 62 | 63 | for (j = i + gap; j < n; j += gap) 64 | { 65 | // 如果a[j] < a[j-gap],则寻找a[j]位置,并将后面数据的位置都后移。 66 | if (a[j] < a[j - gap]) 67 | { 68 | int tmp = a[j]; 69 | int k = j - gap; 70 | while (k >= 0 && a[k] > tmp) 71 | { 72 | a[k + gap] = a[k]; 73 | k -= gap; 74 | } 75 | a[k + gap] = tmp; 76 | } 77 | } 78 | } 79 | 80 | /* 81 | * 希尔排序 82 | * 83 | * 参数说明: 84 | * a -- 待排序的数组 85 | * n -- 数组的长度 86 | */ 87 | void shellSort2(int* a, int n) 88 | { 89 | int i,gap; 90 | 91 | // gap为步长,每次减为原来的一半。 92 | for (gap = n / 2; gap > 0; gap /= 2) 93 | { 94 | // 共gap个组,对每一组都执行直接插入排序 95 | for (i = 0 ;i < gap; i++) 96 | groupSort(a, n, i, gap); 97 | } 98 | } 99 | 100 | int main() 101 | { 102 | int i; 103 | int a[] = {80,30,60,40,20,10,50,70}; 104 | int ilen = (sizeof(a)) / (sizeof(a[0])); 105 | 106 | cout << "before sort:"; 107 | for (i=0; i 0; gap /= 2) { 21 | 22 | // 共gap个组,对每一组都执行直接插入排序 23 | for (int i = 0 ;i < gap; i++) { 24 | 25 | for (int j = i + gap; j < n; j += gap) { 26 | 27 | // 如果a[j] < a[j-gap],则寻找a[j]位置,并将后面数据的位置都后移。 28 | if (a[j] < a[j - gap]) { 29 | 30 | int tmp = a[j]; 31 | int k = j - gap; 32 | while (k >= 0 && a[k] > tmp) { 33 | a[k + gap] = a[k]; 34 | k -= gap; 35 | } 36 | a[k + gap] = tmp; 37 | } 38 | } 39 | } 40 | } 41 | } 42 | 43 | /** 44 | * 对希尔排序中的单个组进行排序 45 | * 46 | * 参数说明: 47 | * a -- 待排序的数组 48 | * n -- 数组总的长度 49 | * i -- 组的起始位置 50 | * gap -- 组的步长 51 | * 52 | * 组是"从i开始,将相隔gap长度的数都取出"所组成的! 53 | */ 54 | public static void groupSort(int[] a, int n, int i,int gap) { 55 | 56 | for (int j = i + gap; j < n; j += gap) { 57 | 58 | // 如果a[j] < a[j-gap],则寻找a[j]位置,并将后面数据的位置都后移。 59 | if (a[j] < a[j - gap]) { 60 | 61 | int tmp = a[j]; 62 | int k = j - gap; 63 | while (k >= 0 && a[k] > tmp) { 64 | a[k + gap] = a[k]; 65 | k -= gap; 66 | } 67 | a[k + gap] = tmp; 68 | } 69 | } 70 | } 71 | 72 | /** 73 | * 希尔排序 74 | * 75 | * 参数说明: 76 | * a -- 待排序的数组 77 | * n -- 数组的长度 78 | */ 79 | public static void shellSort2(int[] a, int n) { 80 | // gap为步长,每次减为原来的一半。 81 | for (int gap = n / 2; gap > 0; gap /= 2) { 82 | // 共gap个组,对每一组都执行直接插入排序 83 | for (int i = 0 ;i < gap; i++) 84 | groupSort(a, n, i, gap); 85 | } 86 | } 87 | 88 | public static void main(String[] args) { 89 | int i; 90 | int a[] = {80,30,60,40,20,10,50,70}; 91 | 92 | System.out.printf("before sort:"); 93 | for (i=0; i 9 | 10 | // 数组长度 11 | #define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) ) 12 | 13 | /* 14 | * 希尔排序 15 | * 16 | * 参数说明: 17 | * a -- 待排序的数组 18 | * n -- 数组的长度 19 | */ 20 | void shell_sort1(int a[], int n) 21 | { 22 | int i,j,gap; 23 | 24 | // gap为步长,每次减为原来的一半。 25 | for (gap = n / 2; gap > 0; gap /= 2) 26 | { 27 | // 共gap个组,对每一组都执行直接插入排序 28 | for (i = 0 ;i < gap; i++) 29 | { 30 | for (j = i + gap; j < n; j += gap) 31 | { 32 | // 如果a[j] < a[j-gap],则寻找a[j]位置,并将后面数据的位置都后移。 33 | if (a[j] < a[j - gap]) 34 | { 35 | int tmp = a[j]; 36 | int k = j - gap; 37 | while (k >= 0 && a[k] > tmp) 38 | { 39 | a[k + gap] = a[k]; 40 | k -= gap; 41 | } 42 | a[k + gap] = tmp; 43 | } 44 | } 45 | } 46 | 47 | } 48 | } 49 | 50 | /* 51 | * 对希尔排序中的单个组进行排序 52 | * 53 | * 参数说明: 54 | * a -- 待排序的数组 55 | * n -- 数组总的长度 56 | * i -- 组的起始位置 57 | * gap -- 组的步长 58 | * 59 | * 组是"从i开始,将相隔gap长度的数都取出"所组成的! 60 | */ 61 | void group_sort(int a[], int n, int i,int gap) 62 | { 63 | int j; 64 | 65 | for (j = i + gap; j < n; j += gap) 66 | { 67 | // 如果a[j] < a[j-gap],则寻找a[j]位置,并将后面数据的位置都后移。 68 | if (a[j] < a[j - gap]) 69 | { 70 | int tmp = a[j]; 71 | int k = j - gap; 72 | while (k >= 0 && a[k] > tmp) 73 | { 74 | a[k + gap] = a[k]; 75 | k -= gap; 76 | } 77 | a[k + gap] = tmp; 78 | } 79 | } 80 | } 81 | 82 | /* 83 | * 希尔排序 84 | * 85 | * 参数说明: 86 | * a -- 待排序的数组 87 | * n -- 数组的长度 88 | */ 89 | void shell_sort2(int a[], int n) 90 | { 91 | int i,gap; 92 | 93 | // gap为步长,每次减为原来的一半。 94 | for (gap = n / 2; gap > 0; gap /= 2) 95 | { 96 | // 共gap个组,对每一组都执行直接插入排序 97 | for (i = 0 ;i < gap; i++) 98 | group_sort(a, n, i, gap); 99 | } 100 | } 101 | 102 | void main() 103 | { 104 | int i; 105 | int a[] = {80,30,60,40,20,10,50,70}; 106 | int ilen = LENGTH(a); 107 | 108 | printf("before sort:"); 109 | for (i=0; i max) 23 | max = a[i]; 24 | 25 | return max; 26 | } 27 | 28 | /* 29 | * 对数组按照"某个位数"进行排序(桶排序) 30 | * 31 | * 参数说明: 32 | * a -- 数组 33 | * exp -- 指数。对数组a按照该指数进行排序。 34 | * 35 | * 例如,对于数组a={50, 3, 542, 745, 2014, 154, 63, 616}; 36 | * (01) 当exp=1表示按照"个位"对数组a进行排序 37 | * (02) 当exp=10表示按照"十位"对数组a进行排序 38 | * (03) 当exp=100表示按照"百位"对数组a进行排序 39 | * ... 40 | */ 41 | private static void countSort(int[] a, int exp) { 42 | //int output[a.length]; // 存储"被排序数据"的临时数组 43 | int[] output = new int[a.length]; // 存储"被排序数据"的临时数组 44 | int[] buckets = new int[10]; 45 | 46 | // 将数据出现的次数存储在buckets[]中 47 | for (int i = 0; i < a.length; i++) 48 | buckets[ (a[i]/exp)%10 ]++; 49 | 50 | // 更改buckets[i]。目的是让更改后的buckets[i]的值,是该数据在output[]中的位置。 51 | for (int i = 1; i < 10; i++) 52 | buckets[i] += buckets[i - 1]; 53 | 54 | // 将数据存储到临时数组output[]中 55 | for (int i = a.length - 1; i >= 0; i--) { 56 | output[buckets[ (a[i]/exp)%10 ] - 1] = a[i]; 57 | buckets[ (a[i]/exp)%10 ]--; 58 | } 59 | 60 | // 将排序好的数据赋值给a[] 61 | for (int i = 0; i < a.length; i++) 62 | a[i] = output[i]; 63 | 64 | output = null; 65 | buckets = null; 66 | } 67 | 68 | /* 69 | * 基数排序 70 | * 71 | * 参数说明: 72 | * a -- 数组 73 | */ 74 | public static void radixSort(int[] a) { 75 | int exp; // 指数。当对数组按各位进行排序时,exp=1;按十位进行排序时,exp=10;... 76 | int max = getMax(a); // 数组a中的最大值 77 | 78 | // 从个位开始,对数组a按"指数"进行排序 79 | for (exp = 1; max/exp > 0; exp *= 10) 80 | countSort(a, exp); 81 | } 82 | 83 | public static void main(String[] args) { 84 | int i; 85 | int a[] = {53, 3, 542, 748, 14, 214, 154, 63, 616}; 86 | 87 | System.out.printf("before sort:"); 88 | for (i=0; i 3 | #include "DoubleLink.h" 4 | using namespace std; 5 | 6 | // 双向链表操作int数据 7 | void int_test() 8 | { 9 | int iarr[4] = {10, 20, 30, 40}; 10 | 11 | cout << "\n----int_test----" << endl; 12 | // 创建双向链表 13 | DoubleLink* pdlink = new DoubleLink(); 14 | 15 | pdlink->insert(0, 20); // 将 20 插入到第一个位置 16 | pdlink->append_last(10); // 将 10 追加到链表末尾 17 | pdlink->insert_first(30); // 将 30 插入到第一个位置 18 | 19 | // 双向链表是否为空 20 | cout << "is_empty()=" << pdlink->is_empty() <* pdlink = new DoubleLink(); 37 | 38 | pdlink->insert(0, sarr[1]); // 将 sarr中第2个元素 插入到第一个位置 39 | pdlink->append_last(sarr[0]); // 将 sarr中第1个元素 追加到链表末尾 40 | pdlink->insert_first(sarr[2]); // 将 sarr中第3个元素 插入到第一个位置 41 | 42 | // 双向链表是否为空 43 | cout << "is_empty()=" << pdlink->is_empty() <* pdlink = new DoubleLink(); 73 | 74 | pdlink->insert(0, arr_stu[1]); // 将 arr_stu中第2个元素 插入到第一个位置 75 | pdlink->append_last(arr_stu[0]); // 将 arr_stu中第1个元素 追加到链表末尾 76 | pdlink->insert_first(arr_stu[2]); // 将 arr_stu中第3个元素 插入到第一个位置 77 | 78 | // 双向链表是否为空 79 | cout << "is_empty()=" << pdlink->is_empty() < 3 | #include 4 | 5 | /** 6 | * C 语言: 单向链表 7 | * 8 | * @author skywang 9 | * @date 2013/11/07 10 | */ 11 | 12 | // 单向链表的“节点” 13 | struct node { 14 | void* p; 15 | struct node* next; 16 | }; 17 | 18 | // 单向链表的“表头” 19 | static struct node *phead=NULL; 20 | 21 | // 创建节点,p为节点值 22 | static struct node* create_node(void *pval) 23 | { 24 | struct node *pnode=NULL; 25 | pnode = (struct node*)malloc(sizeof(struct node)); 26 | if (!pnode) 27 | return NULL; 28 | pnode->p = pval; 29 | pnode->next = NULL; 30 | 31 | return pnode; 32 | } 33 | 34 | // 销毁单向链表 35 | static void destroy_single_link() 36 | { 37 | struct node *pnode=NULL; 38 | 39 | while (phead != NULL) { 40 | pnode = phead; 41 | phead = phead->next; 42 | free(pnode); 43 | } 44 | } 45 | 46 | // 将pval插入到链表的表头位置 47 | static struct node* push(void *pval) 48 | { 49 | struct node *pnode = NULL; 50 | 51 | pnode = create_node(pval); 52 | pnode->next = phead; 53 | phead = pnode; 54 | 55 | return phead; 56 | } 57 | 58 | // 删除链表的表头 59 | static void* pop() 60 | { 61 | if (!phead) 62 | { 63 | printf("remove failed! link is empty!"); 64 | return -1; 65 | } 66 | 67 | void* pret; 68 | struct node *pnode; 69 | pret = phead->p; 70 | pnode = phead; 71 | phead = phead->next; 72 | free(pnode); 73 | 74 | return pret; 75 | } 76 | 77 | // 返回链表的表头节点的值 78 | static void* peek() 79 | { 80 | if (!phead) 81 | { 82 | printf("peek failed! link is empty!"); 83 | return NULL; 84 | } 85 | 86 | return phead->p; 87 | } 88 | 89 | // 返回链表中节点的个数 90 | static int size() 91 | { 92 | int count=0; 93 | struct node *pnode=phead; 94 | 95 | while (pnode != NULL) { 96 | pnode = pnode->next; 97 | count++; 98 | } 99 | return count; 100 | } 101 | 102 | // 链表是否为空 103 | static int is_empty() 104 | { 105 | return phead==NULL; 106 | } 107 | 108 | // 打印“栈” 109 | static void print_single_link() 110 | { 111 | if (is_empty()) 112 | { 113 | printf("stack is Empty\n"); 114 | return ; 115 | } 116 | 117 | printf("stack size()=%d\n", size()); 118 | 119 | struct node *pnode=NULL; 120 | 121 | while (phead != NULL) { 122 | printf("%d\n", phead->p); 123 | pnode = phead; 124 | phead = phead->next; 125 | free(pnode); 126 | } 127 | } 128 | 129 | void main() 130 | { 131 | int tmp=0; 132 | 133 | // 将10, 20, 30 依次推入栈中 134 | push(10); 135 | push(20); 136 | push(30); 137 | 138 | //print_single_link(); // 打印栈 139 | 140 | // 将“栈顶元素”赋值给tmp,并删除“栈顶元素” 141 | tmp = pop(); 142 | printf("tmp=%d\n", tmp); 143 | //print_single_link(); // 打印栈 144 | 145 | // 只将“栈顶”赋值给tmp,不删除该元素. 146 | tmp = peek(); 147 | printf("tmp=%d\n", tmp); 148 | //print_single_link(); // 打印栈 149 | 150 | push(40); 151 | print_single_link(); // 打印栈 152 | 153 | // 销毁栈 154 | destroy_single_link(); 155 | } 156 | -------------------------------------------------------------------------------- /source/linear/stack/c/single_link/slink_stack.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | /** 6 | * C 语言: 单向链表实现的栈,只能存储int数据。 7 | * 8 | * @author skywang 9 | * @date 2013/11/07 10 | */ 11 | 12 | // 单向链表的“节点” 13 | struct node { 14 | int val; 15 | struct node* next; 16 | }; 17 | 18 | // 单向链表的“表头” 19 | static struct node *phead=NULL; 20 | 21 | // 创建节点,val为节点值 22 | static struct node* create_node(int val) 23 | { 24 | struct node *pnode=NULL; 25 | pnode = (struct node*)malloc(sizeof(struct node)); 26 | if (!pnode) 27 | return NULL; 28 | pnode->val = val; 29 | pnode->next = NULL; 30 | 31 | return pnode; 32 | } 33 | 34 | // 销毁单向链表 35 | static int destroy_single_link() 36 | { 37 | struct node *pnode=NULL; 38 | 39 | while (phead != NULL) { 40 | pnode = phead; 41 | phead = phead->next; 42 | free(pnode); 43 | } 44 | return 0; 45 | } 46 | 47 | // 将val插入到链表的表头位置 48 | static struct node* push(int val) 49 | { 50 | struct node *pnode = NULL; 51 | 52 | pnode = create_node(val); 53 | pnode->next = phead; 54 | phead = pnode; 55 | 56 | return phead; 57 | } 58 | 59 | // 删除链表的表头 60 | static int pop() 61 | { 62 | if (!phead) 63 | { 64 | printf("remove failed! link is empty!"); 65 | return -1; 66 | } 67 | 68 | int ret; 69 | struct node *pnode; 70 | ret = phead->val; 71 | pnode = phead; 72 | phead = phead->next; 73 | free(pnode); 74 | 75 | return ret; 76 | } 77 | 78 | // 返回链表的表头节点的值 79 | static int peek() 80 | { 81 | if (!phead) 82 | { 83 | printf("peek failed! link is empty!"); 84 | return -1; 85 | } 86 | 87 | return phead->val; 88 | } 89 | 90 | // 返回链表中节点的个数 91 | static int size() 92 | { 93 | int count=0; 94 | struct node *pnode=phead; 95 | 96 | while (pnode != NULL) { 97 | pnode = pnode->next; 98 | count++; 99 | } 100 | return count; 101 | } 102 | 103 | // 链表是否为空 104 | static int is_empty() 105 | { 106 | return phead==NULL; 107 | } 108 | 109 | // 打印“栈” 110 | static void print_single_link() 111 | { 112 | if (is_empty()) 113 | { 114 | printf("stack is Empty\n"); 115 | return 0; 116 | } 117 | 118 | printf("stack size()=%d\n", size()); 119 | 120 | struct node *pnode=NULL; 121 | 122 | while (phead != NULL) { 123 | printf("%d\n", phead->val); 124 | pnode = phead; 125 | phead = phead->next; 126 | free(pnode); 127 | } 128 | } 129 | 130 | void main() 131 | { 132 | int tmp=0; 133 | 134 | // 将10, 20, 30 依次推入栈中 135 | push(10); 136 | push(20); 137 | push(30); 138 | 139 | //print_single_link(); // 打印栈 140 | 141 | // 将“栈顶元素”赋值给tmp,并删除“栈顶元素” 142 | tmp = pop(); 143 | printf("tmp=%d\n", tmp); 144 | //print_single_link(); // 打印栈 145 | 146 | // 只将“栈顶”赋值给tmp,不删除该元素. 147 | tmp = peek(); 148 | printf("tmp=%d\n", tmp); 149 | //print_single_link(); // 打印栈 150 | 151 | push(40); 152 | print_single_link(); // 打印栈 153 | 154 | // 销毁栈 155 | destroy_single_link(); 156 | } 157 | -------------------------------------------------------------------------------- /source/linear/basic/double_link_list/c/var_dlink/dlink_test.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "double_link.h" 4 | 5 | /** 6 | * C 语言实现的双向链表的测试程序。 7 | * 8 | * (01) int_test() 9 | * 演示向双向链表操作“int数据”。 10 | * (02) string_test() 11 | * 演示向双向链表操作“字符串数据”。 12 | * (03) object_test() 13 | * 演示向双向链表操作“对象”。 14 | * 15 | * @author skywang 16 | * @date 2013/11/07 17 | */ 18 | 19 | // 双向链表操作int数据 20 | void int_test() 21 | { 22 | int iarr[4] = {10, 20, 30, 40}; 23 | 24 | printf("\n----%s----\n", __func__); 25 | create_dlink(); // 创建双向链表 26 | 27 | dlink_insert(0, &iarr[0]); // 向双向链表的表头插入数据 28 | dlink_insert(0, &iarr[1]); // 向双向链表的表头插入数据 29 | dlink_insert(0, &iarr[2]); // 向双向链表的表头插入数据 30 | 31 | printf("dlink_is_empty()=%d\n", dlink_is_empty()); // 双向链表是否为空 32 | printf("dlink_size()=%d\n", dlink_size()); // 双向链表的大小 33 | 34 | // 打印双向链表中的全部数据 35 | int i; 36 | int *p; 37 | int sz = dlink_size(); 38 | for (i=0; iid, p->name); 109 | } 110 | 111 | destroy_dlink(); 112 | } 113 | 114 | int main() 115 | { 116 | int_test(); // 演示向双向链表操作“int数据”。 117 | string_test(); // 演示向双向链表操作“字符串数据”。 118 | object_test(); // 演示向双向链表操作“对象”。 119 | 120 | return 0; 121 | } 122 | 123 | -------------------------------------------------------------------------------- /source/linear/basic/single_link_list/c/var_slink/slink_test.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "double_link.h" 4 | 5 | /** 6 | * C 语言实现的双向链表的测试程序。 7 | * 8 | * (01) int_test() 9 | * 演示向双向链表操作“int数据”。 10 | * (02) string_test() 11 | * 演示向双向链表操作“字符串数据”。 12 | * (03) object_test() 13 | * 演示向双向链表操作“对象”。 14 | * 15 | * @author skywang 16 | * @date 2013/11/07 17 | */ 18 | 19 | // 双向链表操作int数据 20 | void int_test() 21 | { 22 | int iarr[4] = {10, 20, 30, 40}; 23 | 24 | printf("\n----%s----\n", __func__); 25 | create_dlink(); // 创建双向链表 26 | 27 | dlink_insert(0, &iarr[0]); // 向双向链表的表头插入数据 28 | dlink_insert(0, &iarr[1]); // 向双向链表的表头插入数据 29 | dlink_insert(0, &iarr[2]); // 向双向链表的表头插入数据 30 | 31 | printf("dlink_is_empty()=%d\n", dlink_is_empty()); // 双向链表是否为空 32 | printf("dlink_size()=%d\n", dlink_size()); // 双向链表的大小 33 | 34 | // 打印双向链表中的全部数据 35 | int i; 36 | int *p; 37 | int sz = dlink_size(); 38 | for (i=0; iid, p->name); 109 | } 110 | 111 | destroy_dlink(); 112 | } 113 | 114 | int main() 115 | { 116 | int_test(); // 演示向双向链表操作“int数据”。 117 | string_test(); // 演示向双向链表操作“字符串数据”。 118 | object_test(); // 演示向双向链表操作“对象”。 119 | 120 | return 0; 121 | } 122 | 123 | -------------------------------------------------------------------------------- /source/linear/basic/double_link_list/java/DoubleLink.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Java 实现的双向链表。 4 | * 注:java自带的集合包中有实现双向链表,路径是:java.util.LinkedList 5 | * 6 | * @author skywang 7 | * @date 2013/11/07 8 | */ 9 | public class DoubleLink { 10 | 11 | // 表头 12 | private DNode mHead; 13 | // 节点个数 14 | private int mCount; 15 | 16 | // 双向链表“节点”对应的结构体 17 | private class DNode { 18 | public DNode prev; 19 | public DNode next; 20 | public T value; 21 | 22 | public DNode(T value, DNode prev, DNode next) { 23 | this.value = value; 24 | this.prev = prev; 25 | this.next = next; 26 | } 27 | } 28 | 29 | // 构造函数 30 | public DoubleLink() { 31 | // 创建“表头”。注意:表头没有存储数据! 32 | mHead = new DNode(null, null, null); 33 | mHead.prev = mHead.next = mHead; 34 | // 初始化“节点个数”为0 35 | mCount = 0; 36 | } 37 | 38 | // 返回节点数目 39 | public int size() { 40 | return mCount; 41 | } 42 | 43 | // 返回链表是否为空 44 | public boolean isEmpty() { 45 | return mCount==0; 46 | } 47 | 48 | // 获取第index位置的节点 49 | private DNode getNode(int index) { 50 | if (index<0 || index>=mCount) 51 | throw new IndexOutOfBoundsException(); 52 | 53 | // 正向查找 54 | if (index <= mCount/2) { 55 | DNode node = mHead.next; 56 | for (int i=0; i rnode = mHead.prev; 64 | int rindex = mCount - index -1; 65 | for (int j=0; j node = new DNode(t, mHead, mHead.next); 90 | mHead.next.prev = node; 91 | mHead.next = node; 92 | mCount++; 93 | return ; 94 | } 95 | 96 | DNode inode = getNode(index); 97 | DNode tnode = new DNode(t, inode.prev, inode); 98 | inode.prev.next = tnode; 99 | inode.next = tnode; 100 | mCount++; 101 | return ; 102 | } 103 | 104 | // 将节点插入第一个节点处。 105 | public void insertFirst(T t) { 106 | insert(0, t); 107 | } 108 | 109 | // 将节点追加到链表的末尾 110 | public void appendLast(T t) { 111 | DNode node = new DNode(t, mHead.prev, mHead); 112 | mHead.prev.next = node; 113 | mHead.prev = node; 114 | mCount++; 115 | } 116 | 117 | // 删除index位置的节点 118 | public void del(int index) { 119 | DNode inode = getNode(index); 120 | inode.prev.next = inode.next; 121 | inode.next.prev = inode.prev; 122 | inode = null; 123 | mCount--; 124 | } 125 | 126 | // 删除第一个节点 127 | public void deleteFirst() { 128 | del(0); 129 | } 130 | 131 | // 删除最后一个节点 132 | public void deleteLast() { 133 | del(mCount-1); 134 | } 135 | } 136 | 137 | -------------------------------------------------------------------------------- /source/linear/basic/double_link_list/java/DlinkTest.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Java 实现的双向链表。 4 | * 注:java自带的集合包中有实现双向链表,路径是:java.util.LinkedList 5 | * 6 | * @author skywang 7 | * @date 2013/11/07 8 | */ 9 | 10 | public class DlinkTest { 11 | 12 | // 双向链表操作int数据 13 | private static void int_test() { 14 | int[] iarr = {10, 20, 30, 40}; 15 | 16 | System.out.println("\n----int_test----"); 17 | // 创建双向链表 18 | DoubleLink dlink = new DoubleLink(); 19 | 20 | dlink.insert(0, 20); // 将 20 插入到第一个位置 21 | dlink.appendLast(10); // 将 10 追加到链表末尾 22 | dlink.insertFirst(30); // 将 30 插入到第一个位置 23 | 24 | // 双向链表是否为空 25 | System.out.printf("isEmpty()=%b\n", dlink.isEmpty()); 26 | // 双向链表的大小 27 | System.out.printf("size()=%d\n", dlink.size()); 28 | 29 | // 打印出全部的节点 30 | for (int i=0; i dlink = new DoubleLink(); 41 | 42 | dlink.insert(0, sarr[1]); // 将 sarr中第2个元素 插入到第一个位置 43 | dlink.appendLast(sarr[0]); // 将 sarr中第1个元素 追加到链表末尾 44 | dlink.insertFirst(sarr[2]); // 将 sarr中第3个元素 插入到第一个位置 45 | 46 | // 双向链表是否为空 47 | System.out.printf("isEmpty()=%b\n", dlink.isEmpty()); 48 | // 双向链表的大小 49 | System.out.printf("size()=%d\n", dlink.size()); 50 | 51 | // 打印出全部的节点 52 | for (int i=0; i dlink = new DoubleLink(); 84 | 85 | dlink.insert(0, students[1]); // 将 students中第2个元素 插入到第一个位置 86 | dlink.appendLast(students[0]); // 将 students中第1个元素 追加到链表末尾 87 | dlink.insertFirst(students[2]); // 将 students中第3个元素 插入到第一个位置 88 | 89 | // 双向链表是否为空 90 | System.out.printf("isEmpty()=%b\n", dlink.isEmpty()); 91 | // 双向链表的大小 92 | System.out.printf("size()=%d\n", dlink.size()); 93 | 94 | // 打印出全部的节点 95 | for (int i=0; i 9 | using namespace std; 10 | 11 | /* 12 | * 将一个数组中的两个相邻有序区间合并成一个 13 | * 14 | * 参数说明: 15 | * a -- 包含两个有序区间的数组 16 | * start -- 第1个有序区间的起始地址。 17 | * mid -- 第1个有序区间的结束地址。也是第2个有序区间的起始地址。 18 | * end -- 第2个有序区间的结束地址。 19 | */ 20 | void merge(int* a, int start, int mid, int end) 21 | { 22 | int *tmp = new int[end-start+1]; // tmp是汇总2个有序区的临时区域 23 | int i = start; // 第1个有序区的索引 24 | int j = mid + 1; // 第2个有序区的索引 25 | int k = 0; // 临时区域的索引 26 | 27 | while(i <= mid && j <= end) 28 | { 29 | if (a[i] <= a[j]) 30 | tmp[k++] = a[i++]; 31 | else 32 | tmp[k++] = a[j++]; 33 | } 34 | 35 | while(i <= mid) 36 | tmp[k++] = a[i++]; 37 | 38 | while(j <= end) 39 | tmp[k++] = a[j++]; 40 | 41 | // 将排序后的元素,全部都整合到数组a中。 42 | for (i = 0; i < k; i++) 43 | a[start + i] = tmp[i]; 44 | 45 | delete[] tmp; 46 | } 47 | 48 | /* 49 | * 归并排序(从上往下) 50 | * 51 | * 参数说明: 52 | * a -- 待排序的数组 53 | * start -- 数组的起始地址 54 | * endi -- 数组的结束地址 55 | */ 56 | void mergeSortUp2Down(int* a, int start, int end) 57 | { 58 | if(a==NULL || start >= end) 59 | return ; 60 | 61 | int mid = (end + start)/2; 62 | mergeSortUp2Down(a, start, mid); // 递归排序a[start...mid] 63 | mergeSortUp2Down(a, mid+1, end); // 递归排序a[mid+1...end] 64 | 65 | // a[start...mid] 和 a[mid...end]是两个有序空间, 66 | // 将它们排序成一个有序空间a[start...end] 67 | merge(a, start, mid, end); 68 | } 69 | 70 | 71 | /* 72 | * 对数组a做若干次合并:数组a的总长度为len,将它分为若干个长度为gap的子数组; 73 | * 将"每2个相邻的子数组" 进行合并排序。 74 | * 75 | * 参数说明: 76 | * a -- 待排序的数组 77 | * len -- 数组的长度 78 | * gap -- 子数组的长度 79 | */ 80 | void mergeGroups(int* a, int len, int gap) 81 | { 82 | int i; 83 | int twolen = 2 * gap; // 两个相邻的子数组的长度 84 | 85 | // 将"每2个相邻的子数组" 进行合并排序。 86 | for(i = 0; i+2*gap-1 < len; i+=(2*gap)) 87 | { 88 | merge(a, i, i+gap-1, i+2*gap-1); 89 | } 90 | 91 | // 若 i+gap-1 < len-1,则剩余一个子数组没有配对。 92 | // 将该子数组合并到已排序的数组中。 93 | if ( i+gap-1 < len-1) 94 | { 95 | merge(a, i, i + gap - 1, len - 1); 96 | } 97 | } 98 | 99 | /* 100 | * 归并排序(从下往上) 101 | * 102 | * 参数说明: 103 | * a -- 待排序的数组 104 | * len -- 数组的长度 105 | */ 106 | void mergeSortDown2Up(int* a, int len) 107 | { 108 | int n; 109 | 110 | if (a==NULL || len<=0) 111 | return ; 112 | 113 | for(n = 1; n < len; n*=2) 114 | mergeGroups(a, len, n); 115 | } 116 | 117 | int main() 118 | { 119 | int i; 120 | int a[] = {80,30,60,40,20,10,50,70}; 121 | int ilen = (sizeof(a)) / (sizeof(a[0])); 122 | 123 | cout << "before sort:"; 124 | for (i=0; inext = list; 21 | list->prev = list; 22 | } 23 | 24 | // 添加节点:将new插入到prev和next之间。 25 | static inline void __list_add(struct list_head *new, 26 | struct list_head *prev, 27 | struct list_head *next) 28 | { 29 | next->prev = new; 30 | new->next = next; 31 | new->prev = prev; 32 | prev->next = new; 33 | } 34 | 35 | // 添加new节点:将new添加到head之后,是new称为head的后继节点。 36 | static inline void list_add(struct list_head *new, struct list_head *head) 37 | { 38 | __list_add(new, head, head->next); 39 | } 40 | 41 | // 添加new节点:将new添加到head之前,即将new添加到双链表的末尾。 42 | static inline void list_add_tail(struct list_head *new, struct list_head *head) 43 | { 44 | __list_add(new, head->prev, head); 45 | } 46 | 47 | // 从双链表中删除entry节点。 48 | static inline void __list_del(struct list_head * prev, struct list_head * next) 49 | { 50 | next->prev = prev; 51 | prev->next = next; 52 | } 53 | 54 | // 从双链表中删除entry节点。 55 | static inline void list_del(struct list_head *entry) 56 | { 57 | __list_del(entry->prev, entry->next); 58 | } 59 | 60 | // 从双链表中删除entry节点。 61 | static inline void __list_del_entry(struct list_head *entry) 62 | { 63 | __list_del(entry->prev, entry->next); 64 | } 65 | 66 | // 从双链表中删除entry节点,并将entry节点的前继节点和后继节点都指向entry本身。 67 | static inline void list_del_init(struct list_head *entry) 68 | { 69 | __list_del_entry(entry); 70 | INIT_LIST_HEAD(entry); 71 | } 72 | 73 | // 用new节点取代old节点 74 | static inline void list_replace(struct list_head *old, 75 | struct list_head *new) 76 | { 77 | new->next = old->next; 78 | new->next->prev = new; 79 | new->prev = old->prev; 80 | new->prev->next = new; 81 | } 82 | 83 | // 双链表是否为空 84 | static inline int list_empty(const struct list_head *head) 85 | { 86 | return head->next == head; 87 | } 88 | 89 | // 获取"MEMBER成员"在"结构体TYPE"中的位置偏移 90 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 91 | 92 | // 根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针 93 | #define container_of(ptr, type, member) ({ \ 94 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 95 | (type *)( (char *)__mptr - offsetof(type,member) );}) 96 | 97 | // 遍历双向链表 98 | #define list_for_each(pos, head) \ 99 | for (pos = (head)->next; pos != (head); pos = pos->next) 100 | 101 | #define list_for_each_safe(pos, n, head) \ 102 | for (pos = (head)->next, n = pos->next; pos != (head); \ 103 | pos = n, n = pos->next) 104 | 105 | #define list_entry(ptr, type, member) \ 106 | container_of(ptr, type, member) 107 | 108 | #endif 109 | 110 | -------------------------------------------------------------------------------- /source/tree/huffman/java/Huffman.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Huffman树 3 | * 4 | * @author skywang 5 | * @date 2014/03/27 6 | */ 7 | 8 | import java.util.List; 9 | import java.util.ArrayList; 10 | import java.util.Collections; 11 | 12 | public class Huffman { 13 | 14 | private HuffmanNode mRoot; // 根结点 15 | 16 | /* 17 | * 创建Huffman树 18 | * 19 | * @param 权值数组 20 | */ 21 | public Huffman(int a[]) { 22 | HuffmanNode parent = null; 23 | MinHeap heap; 24 | 25 | // 建立数组a对应的最小堆 26 | heap = new MinHeap(a); 27 | 28 | for(int i=0; inext = list; 21 | list->prev = list; 22 | } 23 | 24 | // 添加节点:将new插入到prev和next之间。 25 | static inline void __list_add(struct list_head *new, 26 | struct list_head *prev, 27 | struct list_head *next) 28 | { 29 | next->prev = new; 30 | new->next = next; 31 | new->prev = prev; 32 | prev->next = new; 33 | } 34 | 35 | // 添加new节点:将new添加到head之后,是new称为head的后继节点。 36 | static inline void list_add(struct list_head *new, struct list_head *head) 37 | { 38 | __list_add(new, head, head->next); 39 | } 40 | 41 | // 添加new节点:将new添加到head之前,即将new添加到双链表的末尾。 42 | static inline void list_add_tail(struct list_head *new, struct list_head *head) 43 | { 44 | __list_add(new, head->prev, head); 45 | } 46 | 47 | // 从双链表中删除entry节点。 48 | static inline void __list_del(struct list_head * prev, struct list_head * next) 49 | { 50 | next->prev = prev; 51 | prev->next = next; 52 | } 53 | 54 | // 从双链表中删除entry节点。 55 | static inline void list_del(struct list_head *entry) 56 | { 57 | __list_del(entry->prev, entry->next); 58 | } 59 | 60 | // 从双链表中删除entry节点。 61 | static inline void __list_del_entry(struct list_head *entry) 62 | { 63 | __list_del(entry->prev, entry->next); 64 | } 65 | 66 | // 从双链表中删除entry节点,并将entry节点的前继节点和后继节点都指向entry本身。 67 | static inline void list_del_init(struct list_head *entry) 68 | { 69 | __list_del_entry(entry); 70 | INIT_LIST_HEAD(entry); 71 | } 72 | 73 | // 用new节点取代old节点 74 | static inline void list_replace(struct list_head *old, 75 | struct list_head *new) 76 | { 77 | new->next = old->next; 78 | new->next->prev = new; 79 | new->prev = old->prev; 80 | new->prev->next = new; 81 | } 82 | 83 | // 双链表是否为空 84 | static inline int list_empty(const struct list_head *head) 85 | { 86 | return head->next == head; 87 | } 88 | 89 | // 获取"MEMBER成员"在"结构体TYPE"中的位置偏移 90 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 91 | 92 | // 根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针 93 | #define container_of(ptr, type, member) ({ \ 94 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 95 | (type *)( (char *)__mptr - offsetof(type,member) );}) 96 | 97 | // 遍历双向链表 98 | #define list_for_each(pos, head) \ 99 | for (pos = (head)->next; pos != (head); pos = pos->next) 100 | 101 | #define list_for_each_safe(pos, n, head) \ 102 | for (pos = (head)->next, n = pos->next; pos != (head); \ 103 | pos = n, n = pos->next) 104 | 105 | #define list_entry(ptr, type, member) \ 106 | container_of(ptr, type, member) 107 | 108 | #endif 109 | 110 | -------------------------------------------------------------------------------- /source/algrightm/sort/merge_sort/c/merge_sort.c: -------------------------------------------------------------------------------- 1 | /** 2 | * 归并排序:C 语言 3 | * 4 | * @author skywang 5 | * @date 2014/03/12 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | // 数组长度 12 | #define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) ) 13 | 14 | /* 15 | * 将一个数组中的两个相邻有序区间合并成一个 16 | * 17 | * 参数说明: 18 | * a -- 包含两个有序区间的数组 19 | * start -- 第1个有序区间的起始地址。 20 | * mid -- 第1个有序区间的结束地址。也是第2个有序区间的起始地址。 21 | * end -- 第2个有序区间的结束地址。 22 | */ 23 | void merge(int a[], int start, int mid, int end) 24 | { 25 | int *tmp = (int *)malloc((end-start+1)*sizeof(int)); // tmp是汇总2个有序区的临时区域 26 | int i = start; // 第1个有序区的索引 27 | int j = mid + 1; // 第2个有序区的索引 28 | int k = 0; // 临时区域的索引 29 | 30 | while(i <= mid && j <= end) 31 | { 32 | if (a[i] <= a[j]) 33 | tmp[k++] = a[i++]; 34 | else 35 | tmp[k++] = a[j++]; 36 | } 37 | 38 | while(i <= mid) 39 | tmp[k++] = a[i++]; 40 | 41 | while(j <= end) 42 | tmp[k++] = a[j++]; 43 | 44 | // 将排序后的元素,全部都整合到数组a中。 45 | for (i = 0; i < k; i++) 46 | a[start + i] = tmp[i]; 47 | 48 | free(tmp); 49 | } 50 | 51 | /* 52 | * 归并排序(从上往下) 53 | * 54 | * 参数说明: 55 | * a -- 待排序的数组 56 | * start -- 数组的起始地址 57 | * endi -- 数组的结束地址 58 | */ 59 | void merge_sort_up2down(int a[], int start, int end) 60 | { 61 | if(a==NULL || start >= end) 62 | return ; 63 | 64 | int mid = (end + start)/2; 65 | merge_sort_up2down(a, start, mid); // 递归排序a[start...mid] 66 | merge_sort_up2down(a, mid+1, end); // 递归排序a[mid+1...end] 67 | 68 | // a[start...mid] 和 a[mid...end]是两个有序空间, 69 | // 将它们排序成一个有序空间a[start...end] 70 | merge(a, start, mid, end); 71 | } 72 | 73 | 74 | /* 75 | * 对数组a做若干次合并:数组a的总长度为len,将它分为若干个长度为gap的子数组; 76 | * 将"每2个相邻的子数组" 进行合并排序。 77 | * 78 | * 参数说明: 79 | * a -- 待排序的数组 80 | * len -- 数组的长度 81 | * gap -- 子数组的长度 82 | */ 83 | void merge_groups(int a[], int len, int gap) 84 | { 85 | int i; 86 | int twolen = 2 * gap; // 两个相邻的子数组的长度 87 | 88 | // 将"每2个相邻的子数组" 进行合并排序。 89 | for(i = 0; i+2*gap-1 < len; i+=(2*gap)) 90 | { 91 | merge(a, i, i+gap-1, i+2*gap-1); 92 | } 93 | 94 | // 若 i+gap-1 < len-1,则剩余一个子数组没有配对。 95 | // 将该子数组合并到已排序的数组中。 96 | if ( i+gap-1 < len-1) 97 | { 98 | merge(a, i, i + gap - 1, len - 1); 99 | } 100 | } 101 | 102 | /* 103 | * 归并排序(从下往上) 104 | * 105 | * 参数说明: 106 | * a -- 待排序的数组 107 | * len -- 数组的长度 108 | */ 109 | void merge_sort_down2up(int a[], int len) 110 | { 111 | int n; 112 | 113 | if (a==NULL || len<=0) 114 | return ; 115 | 116 | for(n = 1; n < len; n*=2) 117 | merge_groups(a, len, n); 118 | } 119 | 120 | void main() 121 | { 122 | int i; 123 | int a[] = {80,30,60,40,20,10,50,70}; 124 | int ilen = LENGTH(a); 125 | 126 | printf("before sort:"); 127 | for (i=0; i mHeap; // 存放堆的数组 14 | 15 | /* 16 | * 创建最小堆 17 | * 18 | * 参数说明: 19 | * a -- 数据所在的数组 20 | */ 21 | protected MinHeap(int a[]) { 22 | mHeap = new ArrayList(); 23 | // 初始化数组 24 | for(int i=0; i 0逐次遍历。遍历之后,得到的数组实际上是一个最小堆。 30 | for (int i = a.length / 2 - 1; i >= 0; i--) 31 | filterdown(i, a.length-1); 32 | } 33 | 34 | /* 35 | * 最小堆的向下调整算法 36 | * 37 | * 注:数组实现的堆中,第N个节点的左孩子的索引值是(2N+1),右孩子的索引是(2N+2)。 38 | * 39 | * 参数说明: 40 | * start -- 被下调节点的起始位置(一般为0,表示从第1个开始) 41 | * end -- 截至范围(一般为数组中最后一个元素的索引) 42 | */ 43 | protected void filterdown(int start, int end) { 44 | int c = start; // 当前(current)节点的位置 45 | int l = 2*c + 1; // 左(left)孩子的位置 46 | HuffmanNode tmp = mHeap.get(c); // 当前(current)节点 47 | 48 | while(l <= end) { 49 | // "l"是左孩子,"l+1"是右孩子 50 | if(l < end && (mHeap.get(l).compareTo(mHeap.get(l+1))>0)) 51 | l++; // 左右两孩子中选择较小者,即mHeap[l+1] 52 | 53 | int cmp = tmp.compareTo(mHeap.get(l)); 54 | if(cmp <= 0) 55 | break; //调整结束 56 | else { 57 | mHeap.set(c, mHeap.get(l)); 58 | c = l; 59 | l = 2*l + 1; 60 | } 61 | } 62 | mHeap.set(c, tmp); 63 | } 64 | 65 | /* 66 | * 最小堆的向上调整算法(从start开始向上直到0,调整堆) 67 | * 68 | * 注:数组实现的堆中,第N个节点的左孩子的索引值是(2N+1),右孩子的索引是(2N+2)。 69 | * 70 | * 参数说明: 71 | * start -- 被上调节点的起始位置(一般为数组中最后一个元素的索引) 72 | */ 73 | protected void filterup(int start) { 74 | int c = start; // 当前节点(current)的位置 75 | int p = (c-1)/2; // 父(parent)结点的位置 76 | HuffmanNode tmp = mHeap.get(c); // 当前(current)节点 77 | 78 | while(c > 0) { 79 | int cmp = mHeap.get(p).compareTo(tmp); 80 | if(cmp <= 0) 81 | break; 82 | else { 83 | mHeap.set(c, mHeap.get(p)); 84 | c = p; 85 | p = (p-1)/2; 86 | } 87 | } 88 | mHeap.set(c, tmp); 89 | } 90 | 91 | /* 92 | * 将node插入到二叉堆中 93 | */ 94 | protected void insert(HuffmanNode node) { 95 | int size = mHeap.size(); 96 | 97 | mHeap.add(node); // 将"数组"插在表尾 98 | filterup(size); // 向上调整堆 99 | } 100 | 101 | /* 102 | * 交换两个HuffmanNode节点的全部数据 103 | */ 104 | private void swapNode(int i, int j) { 105 | HuffmanNode tmp = mHeap.get(i); 106 | mHeap.set(i, mHeap.get(j)); 107 | mHeap.set(j, tmp); 108 | } 109 | 110 | /* 111 | * 新建一个节点,并将最小堆中最小节点的数据复制给该节点。 112 | * 然后除最小节点之外的数据重新构造成最小堆。 113 | * 114 | * 返回值: 115 | * 失败返回null。 116 | */ 117 | protected HuffmanNode dumpFromMinimum() { 118 | int size = mHeap.size(); 119 | 120 | // 如果"堆"已空,则返回 121 | if(size == 0) 122 | return null; 123 | 124 | // 将"最小节点"克隆一份,将克隆得到的对象赋值给node 125 | HuffmanNode node = (HuffmanNode)mHeap.get(0).clone(); 126 | 127 | // 交换"最小节点"和"最后一个节点" 128 | mHeap.set(0, mHeap.get(size-1)); 129 | // 删除最后的元素 130 | mHeap.remove(size-1); 131 | 132 | if (mHeap.size() > 1) 133 | filterdown(0, mHeap.size()-1); 134 | 135 | return node; 136 | } 137 | 138 | // 销毁最小堆 139 | protected void destroy() { 140 | mHeap.clear(); 141 | mHeap = null; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /source/heap/binomial/cplus/Main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * C 语言: 二项堆 3 | * 4 | * @author skywang 5 | * @date 2014/04/02 6 | */ 7 | 8 | #include 9 | #include "BinomialHeap.h" 10 | using namespace std; 11 | 12 | #define DEBUG 0 13 | 14 | // 共7个 = 1+2+4 15 | int a[] = {12, 7, 25, 15, 28, 16 | 33, 41}; 17 | // 共13个 = 1+4+8 18 | int b[] = {18, 35, 20, 42, 9, 19 | 31, 23, 6, 48, 11, 20 | 24, 52, 13 }; 21 | 22 | // 验证"二项堆的插入操作" 23 | void testInsert() 24 | { 25 | int i; 26 | int alen=sizeof(a)/sizeof(a[0]); 27 | BinomialHeap* ha=new BinomialHeap(); 28 | 29 | cout << "== 二项堆(ha)中依次添加: "; 30 | for(i=0; iinsert(a[i]); 34 | } 35 | cout << endl; 36 | cout << "== 二项堆(ha)的详细信息: " << endl; 37 | ha->print(); 38 | } 39 | 40 | // 验证"二项堆的合并操作" 41 | void testUnion() 42 | { 43 | int i; 44 | int alen=sizeof(a)/sizeof(a[0]); 45 | int blen=sizeof(b)/sizeof(b[0]); 46 | BinomialHeap* ha=new BinomialHeap(); 47 | BinomialHeap* hb=new BinomialHeap(); 48 | 49 | cout << "== 二项堆(ha)中依次添加: "; 50 | for(i=0; iinsert(a[i]); 54 | } 55 | cout << endl; 56 | cout << "== 二项堆(ha)的详细信息: " << endl; 57 | ha->print(); 58 | 59 | 60 | cout << "== 二项堆(hb)中依次添加: "; 61 | for(i=0; iinsert(b[i]); 65 | } 66 | cout << endl; 67 | cout << "== 二项堆(hb)的详细信息: " << endl; 68 | hb->print(); 69 | 70 | 71 | // 将"二项堆hb"合并到"二项堆ha"中。 72 | ha->combine(hb); 73 | cout << "== 合并ha和hb后的详细信息: " << endl; 74 | ha->print(); 75 | } 76 | 77 | // 验证"二项堆的删除操作" 78 | void testDelete() 79 | { 80 | int i; 81 | int blen=sizeof(b)/sizeof(b[0]); 82 | BinomialHeap* hb=new BinomialHeap(); 83 | 84 | cout << "== 二项堆(hb)中依次添加: "; 85 | for(i=0; iinsert(b[i]); 89 | } 90 | cout << endl; 91 | cout << "== 二项堆(hb)的详细信息: " << endl; 92 | hb->print(); 93 | 94 | 95 | // 将"二项堆hb"合并到"二项堆ha"中。 96 | hb->remove(20); 97 | cout << "== 删除节点20后的详细信息: " << endl; 98 | hb->print(); 99 | } 100 | 101 | // 验证"二项堆的更新(减少)操作" 102 | void testDecrease() 103 | { 104 | int i; 105 | int blen=sizeof(b)/sizeof(b[0]); 106 | BinomialHeap* hb=new BinomialHeap(); 107 | 108 | cout << "== 二项堆(hb)中依次添加: "; 109 | for(i=0; iinsert(b[i]); 113 | } 114 | cout << endl; 115 | cout << "== 二项堆(hb)的详细信息: " << endl; 116 | hb->print(); 117 | 118 | 119 | // 将节点20更新为2 120 | hb->update(20, 2); 121 | cout << "== 更新节点20->2后的详细信息: " << endl; 122 | hb->print(); 123 | } 124 | 125 | // 验证"二项堆的更新(增加)操作" 126 | void testIncrease() 127 | { 128 | int i; 129 | int blen=sizeof(b)/sizeof(b[0]); 130 | BinomialHeap* hb=new BinomialHeap(); 131 | 132 | cout << "== 二项堆(hb)中依次添加: "; 133 | for(i=0; iinsert(b[i]); 137 | } 138 | cout << endl; 139 | cout << "== 二项堆(hb)的详细信息: " << endl; 140 | hb->print(); 141 | 142 | 143 | // 将节点6更新为60 144 | hb->update(6, 60); 145 | cout << "== 更新节点6->60后的详细信息: " << endl; 146 | hb->print(); 147 | } 148 | 149 | int main() 150 | { 151 | // 1. 验证"二项堆的插入操作" 152 | testInsert(); 153 | // 2. 验证"二项堆的合并操作" 154 | //testUnion(); 155 | // 3. 验证"二项堆的删除操作" 156 | //testDelete(); 157 | // 4. 验证"二项堆的更新(减少)操作" 158 | //testDecrease(); 159 | // 5. 验证"二项堆的更新(增加)操作" 160 | //testIncrease(); 161 | 162 | return 0; 163 | } 164 | -------------------------------------------------------------------------------- /source/algrightm/sort/heap_sort/java/HeapSort.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 堆排序:Java 3 | * 4 | * @author skywang 5 | * @date 2014/03/11 6 | */ 7 | 8 | public class HeapSort { 9 | 10 | /* 11 | * (最大)堆的向下调整算法 12 | * 13 | * 注:数组实现的堆中,第N个节点的左孩子的索引值是(2N+1),右孩子的索引是(2N+2)。 14 | * 其中,N为数组下标索引值,如数组中第1个数对应的N为0。 15 | * 16 | * 参数说明: 17 | * a -- 待排序的数组 18 | * start -- 被下调节点的起始位置(一般为0,表示从第1个开始) 19 | * end -- 截至范围(一般为数组中最后一个元素的索引) 20 | */ 21 | public static void maxHeapDown(int[] a, int start, int end) { 22 | int c = start; // 当前(current)节点的位置 23 | int l = 2*c + 1; // 左(left)孩子的位置 24 | int tmp = a[c]; // 当前(current)节点的大小 25 | 26 | for (; l <= end; c=l,l=2*l+1) { 27 | // "l"是左孩子,"l+1"是右孩子 28 | if ( l < end && a[l] < a[l+1]) 29 | l++; // 左右两孩子中选择较大者,即m_heap[l+1] 30 | if (tmp >= a[l]) 31 | break; // 调整结束 32 | else { // 交换值 33 | a[c] = a[l]; 34 | a[l]= tmp; 35 | } 36 | } 37 | } 38 | 39 | /* 40 | * 堆排序(从小到大) 41 | * 42 | * 参数说明: 43 | * a -- 待排序的数组 44 | * n -- 数组的长度 45 | */ 46 | public static void heapSortAsc(int[] a, int n) { 47 | int i,tmp; 48 | 49 | // 从(n/2-1) --> 0逐次遍历。遍历之后,得到的数组实际上是一个(最大)二叉堆。 50 | for (i = n / 2 - 1; i >= 0; i--) 51 | maxHeapDown(a, i, n-1); 52 | 53 | // 从最后一个元素开始对序列进行调整,不断的缩小调整的范围直到第一个元素 54 | for (i = n - 1; i > 0; i--) { 55 | // 交换a[0]和a[i]。交换后,a[i]是a[0...i]中最大的。 56 | tmp = a[0]; 57 | a[0] = a[i]; 58 | a[i] = tmp; 59 | // 调整a[0...i-1],使得a[0...i-1]仍然是一个最大堆。 60 | // 即,保证a[i-1]是a[0...i-1]中的最大值。 61 | maxHeapDown(a, 0, i-1); 62 | } 63 | } 64 | 65 | /* 66 | * (最小)堆的向下调整算法 67 | * 68 | * 注:数组实现的堆中,第N个节点的左孩子的索引值是(2N+1),右孩子的索引是(2N+2)。 69 | * 其中,N为数组下标索引值,如数组中第1个数对应的N为0。 70 | * 71 | * 参数说明: 72 | * a -- 待排序的数组 73 | * start -- 被下调节点的起始位置(一般为0,表示从第1个开始) 74 | * end -- 截至范围(一般为数组中最后一个元素的索引) 75 | */ 76 | public static void minHeapDown(int[] a, int start, int end) { 77 | int c = start; // 当前(current)节点的位置 78 | int l = 2*c + 1; // 左(left)孩子的位置 79 | int tmp = a[c]; // 当前(current)节点的大小 80 | 81 | for (; l <= end; c=l,l=2*l+1) { 82 | // "l"是左孩子,"l+1"是右孩子 83 | if ( l < end && a[l] > a[l+1]) 84 | l++; // 左右两孩子中选择较小者 85 | if (tmp <= a[l]) 86 | break; // 调整结束 87 | else { // 交换值 88 | a[c] = a[l]; 89 | a[l]= tmp; 90 | } 91 | } 92 | } 93 | 94 | /* 95 | * 堆排序(从大到小) 96 | * 97 | * 参数说明: 98 | * a -- 待排序的数组 99 | * n -- 数组的长度 100 | */ 101 | public static void heapSortDesc(int[] a, int n) { 102 | int i,tmp; 103 | 104 | // 从(n/2-1) --> 0逐次遍历每。遍历之后,得到的数组实际上是一个最小堆。 105 | for (i = n / 2 - 1; i >= 0; i--) 106 | minHeapDown(a, i, n-1); 107 | 108 | // 从最后一个元素开始对序列进行调整,不断的缩小调整的范围直到第一个元素 109 | for (i = n - 1; i > 0; i--) { 110 | // 交换a[0]和a[i]。交换后,a[i]是a[0...i]中最小的。 111 | tmp = a[0]; 112 | a[0] = a[i]; 113 | a[i] = tmp; 114 | // 调整a[0...i-1],使得a[0...i-1]仍然是一个最小堆。 115 | // 即,保证a[i-1]是a[0...i-1]中的最小值。 116 | minHeapDown(a, 0, i-1); 117 | } 118 | } 119 | 120 | public static void main(String[] args) { 121 | int i; 122 | //int a[] = {20,30,90,40,70,110,60,10,100,50,80}; 123 | int a[]= {5,6,8,7,15}; 124 | 125 | System.out.printf("before sort:"); 126 | for (i=0; i