class List {
void clear();bool isEmpty();
bool append(const T value);
Boolinsert(^);Booldelete(^);
bool getValue(^);setValue(^);
boolgetPos(int &p, const T value);
返回 value 元素内容位置};
classarrList:public List^
{T* aList; int maxSzie;
int curLen; int position;}
顺序表:读取 O(1)检索插删 O(n)
class Link{public :T data;
Link<T> *next;//链表节点}
class lnkList : public List<T> {
Link<T> *head, *tail;O(n)
Link<T> *setPos(const int p);}
class Stack {
void clear();bool push(T item);
bool pop(T& item);
bool top(T& item);
BoolisEmpty();bool isFull();};
class arrStack:public Stack^{
int mSize;int top;T *st; ^^}
class Queue {
Void clear();bool enQueue(T va);
bool deQueue(T& item);
bool getFront(T& item);
bool isEmpty();bool isFull();};
class lnkQueue : public Queue^{
Int size;Link^*front;Link^*rear;}
class arrQueue:public Queue {
int mSize, front, rear;T *qu;}
String:substr()swap()copy()
Assign()’=’insert()’+=’append()
‘+’find()replace( )clear( )size( )
length( )max_size( )
满二叉树:结点或是树叶,或恰有两
棵非空子树 完全二叉树:最下面
的两层结点度数可以小于 2;最下
层结点集中在最左边 扩充二叉树:
度数为 1-加一个空树叶;树叶-加
两个空树叶 高度=深度+1 ;n0 =
n2 + 1 ;非空满二树叶数等于分
支结点数加 1
有 n 个结点的完全二叉树的高度
log2 (n+1),深度 log2 (n+1)-1
class BinaryTreeNode ($) {
$ *leftchild, *rightchild ; T info;
T value() const;
$<T>* leftchild() const;
void setLeftchild($<T>*);
void setValue(const T& val);
bool isLeft() const;
$^ & operator=(const $^& v);}
class BinaryTree {
BinaryTreeNode<T> * root;
bool isEmpty() const;
$^* Root() { return root; }
$^* Parent($^* current);
$^* LeftSibling($^* current);
$^* RightSibling($^* current);
void CreateTree(const T& info,
$^& leftTree, $^& rightTree);
void PreOrder($^* root);
void InOrder($^* root);
void PostOrder($^* root);
void LevelOrder($^* root);
void DeleteBinaryTree( $^ * root); }
void $^::PreOrder($^* root ){
if ( root != NULL) {
Visit(root->value());
PreOrder(root->leftchild());
PreOrder(root->rightchild()); }
void $^::PreOrder($^* root){
using std::stack;stack<$^*> aStack;
$^ * pointer = root;
aStack.push(NULL); 栈底监视哨
while (pointer) {
Visit(pointer->value());
if (pointer->rightchild() != NULL)
aStack.push(pointer->rightchild());
if (pointer->leftchild() != NULL)
aStack.push(pointer->leftchild());
pointer=aStack.top();
aStack.pop(); }}
void BinaryTree<T>::
LevelOrder($^* root) {
using std::queue;
queue<$^ *> aQueue;
$^*pointer=root;
if (pointer) aQueue.push(pointer);
while (!aQuene.empty()) {
pointer=aQueue.front();
aQueue.pop();
Visit(pointer->value());
if(pointer->leftchild())
aQueue.push(pointer->leftchild());
if(pointer->rightchild())
aQue.push(pointer->rightchild()); }}
class MinHeap {
T* heapArray;int CurrentSize;
int MaxSize;void BuildHeap();
bool isLeaf(int pos) const;
int leftchild(int pos) const;
int rightchild(int pos) const;
int parent(int pos) const;
bool Remove(int pos, T& node);
bool Insert(const T& newNode);
T& RemoveMin();
void SiftUp(int position);
void SiftDown(int left);}
class TreeNode (@) {
T m_Value;TreeNode<T>*
pChild; TreeNode<T>* pSibling;
TreeNode(const T&);
virtual ~TreeNode(){};
bool isLeaf();T Value();
TreeNode<T>* LeftMostChild();
TreeNode<T>* RightSibling();
void setValue(T&);
void setChild(@<T>* pointer);
void setSibling(@<T>* pointer);
void InsertFirst(@<T>* node);以第
一个子结点的身份插入结点
void InsertNext(@<T>* node);以右
兄弟的身份插入结点};
class Tree{
TreeNode<T>* root;
Tree(); virtual ~Tree();
TreeNode<T>* getRoot();
void CreateRoot(const T& rtValue);
bool isEmpty();
@<T>* Parent(@<T>* current);
@<T>*PrevSibling(@<T>* curnt);
返回 current 结点前一个邻居结点
void DeleteSubTree(@<T>* subrt);
void RootFirstTraverse(@<T>* rt);
void RootLastTraverse(@<T>* rt);
void WidthTraverse( @ <T>* rt); };
void Tree<T>::
RootFirstTraverse(@<T>* root)
{
while (root != NULL) {
Visit(root->Value( ));
RootFirstTraverse(root-
>LeftMostChild( ));
root = root->RightSibling( );}}
先根 -- 先序 ; 后根 -- 中序
void Tree<T>::
WidthTraverse2(@<T>* root){
using std::queue;
queue<TreeNode<T>*> aQueue;
TreeNode<T>* pointer=root;
while (pointer != NULL) {
aQueue.push(pointer);
pointer = pointer->RightSibling();}
while (!aQueue.empty()) {
pointer=aQueue.front();
aQueue.pop();
Visit(pointer->Value());
pointer = pointer->LeftMostChild();
while ( pointer != NULL) {
aQueue.push(pointer);
pointer = pointer->R gh tSibl n g(); }}}
树的链式存储结构
1 子结点表表示法<在数组中存储
树的结点。每个结点包括结点值
一个父指针以及一个指向子结点
链表(索引值+指向右兄弟的指针)
的指针> 2 静态左子/右兄表示法
<数组:左子 p-值-父-右兄 p> 3
动态结点表示法<每个结点分配可
变的存储空间,存储子结点指针链
表---值+子节点个数+各个指针 p>
4 动态”左子/右兄”二叉链表表示
法<左子 p+值+右兄 p>
5 父指针表示法<每个结点只保存
一个指针域指向其父结点>
重量权衡合并规则:将结点较少树
的根结点指向结点较多树的根结
点。树的整体深度限制在O(log
n)树的顺序存储
1 带右链的先根次序表示法(结点
按先根次序顺序存储在一片连续
的存储单元中 ltag-info-rlink 无子
ltag=1)
2 带双标记位的先根次序表示法
( ltag-info-rtag 0-1 1-1 )
class Graph{
int numVertex; int numEdge;
int *Mark; int *Indegree;
int VerticesNum();
int EdgesNum();
Edge FirstEdge(int oneVertex);
Edge NextEdge(Edge preEdge);
bool setEdge(int fV,int tV,int wt);
bool delEdge(int fromV,int toV);
bool IsEdge(Edge oneEdge);
int FromVertex(Edge oneEdge);
int ToVertex(Edge oneEdge);
int Weight(Edge oneEdge); };
存储结构(相邻矩阵--Θ(|V|2)
/邻接表--Θ(|V|+|E|))
class Edge {int from, to, weight;
Edge();Edge(^);};
class Graphm:public Graph {
int **matrix; ^^ }
class Link {
Elem element;Link* next; ^^};
struct listUnit {
int vertex; int weight; };
class LList {
Link<Elem> *head; LList();};
class Graphl : public Graph {
LList<listUnit> *graList;
Graphl(int nV):Graph(nV) {
graList=new LList<listUn i t>[ n V];}
Edge Graphl::
NextEdge(Edge preEdge) {
Edge myEdge;
myEdge.from = preEdge.from;
Link<listUnit>
*temp=graList[preEdge.from].head;
while (temp->next != NULL) &&
temp->next-
>element.vertex<=preEdge.to)
temp=temp->next;
if (temp->next != NULL ) {
myEdge.to=temp->next-
>element.vertex;
myEdge.weight=temp->next-
>elemnt.weight;}
return myEdge;}