1. 程式人生 > >Shell排序和二叉樹排序

Shell排序和二叉樹排序

pan turn 決定 最大 blog std gap span rom

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排序和二叉樹排序