Shell排序和二叉樹排序

來源:互聯網
上載者:User

標籤:列印   logs   根據   void   lis   pen   輸出   tar   node   

Shell排序

#include<iostream>using namespace std;void Print(int *list, int len);void ShellSort(int *list, int *list2, int len, int gap);void ShellSort(int *list, int *list2, int len, int gap) {int cc = len / gap + (len%gap != 0);int l = 0;for (int i = 0; i < gap; i++) {for (int c = 0; c < cc; c++) {for (int j = i; j + gap < len; j += gap) {if (list[j] > list[j + gap]) {int t = list[j];list[j] = list[j + gap];list[j + gap] = t;}}}for (int k = i; k < len; l++, k += gap) {list2[l] = list[k];}}Print(list, len);}void Print(int *list, int len) {for (int i = 0; i < len; i++) {cout << list[i];}cout << endl;}int main(){int list[] = { 3,7,1,9,4,6,8,5,2,0 };int list2[10] = { 0 };ShellSort(list, list2, 10, 5);ShellSort(list2, list, 10, 2);ShellSort(list, list2, 10, 1);return 0;}

 

代碼寫的可以說有一點點醜= =

二叉樹排序

//需要一個棧,用數組寫了= =#include "stdafx.h"#include<iostream>using namespace std;class Node {private:int data;Node *left;Node *right;public:Node():data(0),left(NULL),right(NULL){}Node(int d):data(d), left(NULL), right(NULL) {}void SetData(int d) { data = d; }void SetLeft(Node *l) { left = l; }void SetRight(Node *r) { right = r; }int GetData() { return data; }Node *GetLeft() { return left; }Node *GetRight() { return right; }};class Tree {private:Node *root;Node *stack[20];int pstack;void AppendNode(Node* root, int d);void PrintTree(Node *root, int isFromStack);public:Tree() :root(NULL) { pstack = -1; }Tree(int d) { root = new Node(d); pstack = -1; }~Tree(){}void AppendNode(int d) { AppendNode(root, d); }//重載一下方便外部調用void PrintTree() { PrintTree(root, 0); }void Push(Node *n) { if (++pstack < 20)stack[pstack] = n;else pstack--; }Node *Pop() { //cout << endl << "Stack: " << pstack << endl;if (pstack == -1) return NULL;pstack--;return stack[pstack + 1];}};void Tree::PrintTree(Node *root, int isFromStack) {if (root == NULL) { //走到盡頭了,出棧Node *t = Pop();if (t != NULL) {PrintTree(t, 1);}// else { return; }//我也不知道這樣寫為啥不對,寫在外面就好了,單步了發現沒跳出去,很奇怪return;}if (root->GetLeft() != NULL && isFromStack == 0) { //左子樹不空,當前節點壓棧,往左走,若是彈棧得到的,則不管左面Push(root);PrintTree(root->GetLeft(), 0);} else { //左子樹為空白,輸出當前節點,判斷右面cout << root->GetData();PrintTree(root->GetRight(), 0);}}void Tree::AppendNode(Node* root, int d) {if (root == NULL) { return; } else {if (d == root->GetData()) { return; }if (d > root->GetData()) { //新值比當前大if (root->GetRight() == NULL) { //右面沒有節點,建新節點,建新連結Node *newnode = new Node(d);root->SetRight(newnode);} else { //右面有節點,往右走AppendNode(root->GetRight(), d);}}if (d < root->GetData()) {if (root->GetLeft() == NULL) {Node *newnode = new Node(d);root->SetLeft(newnode);} else {AppendNode(root->GetLeft(), d);}}}}int main(){Tree t = Tree(3);t.AppendNode(7);t.AppendNode(1);t.AppendNode(9);t.AppendNode(4);t.AppendNode(6);t.AppendNode(8);t.AppendNode(5);t.AppendNode(2);t.AppendNode(0);t.PrintTree();return 0;}

 

這個還是有點麻煩的,寫了一個二叉樹類和一個節點類,二叉樹的話包括插入節點(中序)和列印樹(中序),兩個都是用遞迴寫的,遞迴真厲害。

缺點是建樹的時候必須建立根節點,也就是那個無參建構函式不能用,否則會出錯(指標用的還不夠熟啊)

還有沒寫解構函式,懶得寫了

還有啊,棧是用數組寫的,最大二十(這麼個東西我再拿類寫個棧真的是要死了)

節點的SetData()方法根據需要決定是否去掉

 

 

原文地址:http://www.cnblogs.com/ippfcox/p/7401820.html 轉載請註明

Shell排序和二叉樹排序

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.