C++ 136

C++ Fundamentals of Data Structures(C++ 자료구조론) 5.6 MaxHeap, MinHeap(최대 힙, 최소 힙) 예제

[MaxHeap] /*최대힙, MaxHeap*/#include #include #include using namespace std; template class MaxPQ{public: virtual bool IsEmpty() = 0; virtual void Push(const T &item) = 0; virtual void Pop() = 0; virtual T &Top() const = 0;}; template class MaxHeap :public MaxPQ{private: T *heap; int capacity; int heapSize; //원소 개수public: MaxHeap(int theCapacity = 10) { if (theCapacity < 1) { cout

C++ Fundamentals of Data Structures(C++ 자료구조론) 5.5 Threaded Binary Tree(쓰레드 이진 트리) 예제

/*쓰레드 이진 트리, Threaded Binary Trees*/#include using namespace std; class ThreadedTree; class ThreadedNode{private: //기본적으로 쓰레드는 말단노드(leaf node)에만 해당 bool leftThread; //중위순회할 때 다음에 올 데이터 ThreadedNode *leftChild; char data; ThreadedNode *rightChild; bool rightThread; //중위순회할 때 그 전에 온 데이터public: ThreadedNode() :leftChild(NULL), rightThread(NULL) { } ThreadedNode(char temp, ThreadedNode *left, Thread..

C++ Fundamentals of Data Structures(C++ 자료구조론) 5.4 Additional Binary Tree Operations(이진트리 추가함수)

[Exercises 1]/*Write a C++ function to count the leaf nodes in a binary tree.이진트리에서 단말노드를 세는 함수를 작성한다*/#include using namespace std; class Tree; class TreeNode{private: TreeNode *leftChild; int data; TreeNode *rightChild;public: TreeNode() :leftChild(NULL), data(0), rightChild(NULL) { } TreeNode(int temp, TreeNode *left, TreeNode *right) :leftChild(left), data(temp), rightChild(right) { } friend..

C++ Fundamentals of Data Structures(C++ 자료구조론) 5.4 Additional Binary Tree Operations(이진트리 추가함수) 예제

/*기존 이진트리에 살을 좀 붙여봤습니다.*/#include using namespace std; class Tree; class TreeNode{private: TreeNode *leftChild; int data; TreeNode *rightChild;public: TreeNode():leftChild(NULL), data(0), rightChild(NULL) { } TreeNode(int temp, TreeNode *left, TreeNode *right) :leftChild(left), data(temp), rightChild(right) { } friend class Tree; friend int Equal(TreeNode *tn1, TreeNode *tn2);}; class Tree{priva..

C++ Fundamentals of Data Structures(C++ 자료구조론) 5.3 Binary Tree Traversal and Tree iterators(트리 순회와 트리 반복자) 1~9

[Exercises 1]/*Write out the inorder, preorder, postorder, and level-order traversals for the binary trees of Figure 5.105.10의 이진트리를 중위, 전위, 후위, 그리고 레벨-표기법으로 작성해본다*/ inorder(중위): H->D->I->B->E->A->F->C->Gpreorder(전위): A->B->D->H->I->E->C->F->Gpostorder(후위): H->I->D->E->B->F->G->C->Alevel: A->B->C->D->E->F->G->H->I [Exercises 2]/*Do Exercise 1 for the binary tree of Figure 5.115.11의 이진트리에 대해서도 전 ..

C++ Fundamentals of Data Structures(C++ 자료구조론) 5.2 BinaryTree(이진트리 이론)

[Exercises 1]/*For the binary tree of Figure 5.15, list the leaf nodes, the nonleaf nodes, and the level of each node5.15에서 제시하는 이진트리에서 단말노드, 단말노드가 아닌 노드들, 그리고 각 노드의 높이를 명시한다*/ leaf nodes(단말노드) :D, Enon-leaf nodes(그 외 노드들): A, B, CA(level 1)B(level 2)C(level 3)D(level 3) E(level 4)[Figure 5.15] [Exercises 2]/*What is the maximum number of nodes in a k-ary tree of height h?h 높이의 k진 트리의 최대 노드의 수는?..

C++ Fundamentals of Data Structures(C++ 자료구조론) 5.1 Tree(트리)

/*5.1절에 나온 내용을 토대로 매우 허술한 트리를 작성해보았습니다*/#include #include #include using namespace std; class Tree; class TreeNode{private: TreeNode *left; //왼쪽 int data; TreeNode *right; //오른쪽public: TreeNode(int temp) :left(NULL), data(temp), right(NULL) { } int Data() { return data; } friend class Tree;}; class Tree{public: Tree() { } void Insert(TreeNode **root, int temp) { if (*root == 0) *root = new TreeN..

C++ Fundamentals of Data Structures(C++ 자료구조론) 4.11 일반화된 연결리스트(Generalized Lists)

/*일반화된 리스트 Generalized Lists*/#include using namespace std; template class GenList; enum GenListNodeType { HEAD, DATA, LIST }; //타입: dummy, 데이터, 리스트 template class GenListNode{public: GenListNodeType type; //타입 T data; GenList *list; GenListNode *next; GenListNode() { type = HEAD; data = 0; list = NULL; next = NULL; } ~GenListNode() { if (list) delete list; }}; template class GenList{private: Ge..

C++ Fundamentals of Data Structures(C++ 자료구조론) 4.10 이중연결리스트(Doubly Linked List)

/*이중 연결리스트 Doubly LinkedList*/#include using namespace std; class DblList; class DblListNode{private: int data; DblListNode *left, *right;public: DblListNode(int element = 0, DblListNode *lNode = NULL, DblListNode *rNode = NULL) :data(element), left(lNode), right(rNode) { } friend class DblList; friend ostream &operatorright = first; //더미노드 } ~DblList() { DblListNode *delNode = first; //위치를 기억해두..

C++ Fundamentals of Data Structures(C++ 자료구조론) 4.9 희소행렬(Sparse Matrix)

/*희소행렬 SparseMatrix*/#include using namespace std; class Matrix; struct Triple{ int row; int col; int val;}; class MatrixNode{private: MatrixNode *right; //행 MatrixNode *down; //열 bool isHead; union { MatrixNode *next; Triple triple; //row, col, val };public: MatrixNode() //디폴트 생성자 { } MatrixNode(bool b, Triple *t) { isHead = b; if (isHead) { right = down = next = this; } else { triple = *t; } }..