資料結構第二個實驗要求能成功演示二叉樹的有關運算,運算完畢後能成功釋放二叉樹所有結點占用的系統記憶體。其中構造二叉樹修改二叉樹,構造指定順序的二叉樹是本實驗的重點與難點。
btnode.h
#include
using namespace std;
template t>
struct btnode
btnode(const t &x,btnode *l,btnode *r)
};
binarytree.h
#include "btnode.h"
template
t>
class
binarytree
~binarytree
(){}
bool root
(t &x);
void maketree
(const
t &x,binarytree
&left,binarytree
&right);
void breaktree
(t &x,binarytree
&left,binarytree
&right);
void preorder
(void (*visit)
(t &x));
void inorder
(void (*visit)
(t &x));
void postorder
(void (*visit)
(t &x));
};template t>
void visit
(t &x)
else
}template t>
void binarytree
::maketree
(const
t &x,binarytree
&left,binarytree
&right)
template t>
void binarytree
::breaktree
(t &x,binarytree
&left,binarytree
&right)
template t>
void binarytree
::preorder
(void (*visit)
(t &x))
template t>
void binarytree
::inorder
(void (*visit)
(t &x))
template t>
void binarytree
::postorder
(void (*visit)
(t &x))
template t>
void binarytree
::preorder
(void (*visit)
(t &x),btnode
* t)
}template t>
void binarytree
::inorder
(void (*visit)
(t &x),btnode
* t)
}template t>
void binarytree
::postorder
(void (*visit)
(t &x),btnode
* t)
}
main.cpp
#include "binarytree.h"
void main(void)
以上實現了基本的樹的構造,觀察**除錯執行可以較深入的理解二叉樹的構造原理
下面是特殊的樹結構,這類樹結構會為後面構造哈夫曼樹打下基礎
1.構造優先權佇列樣例原始碼
#include
using namespace std;
template class prioqueue
; bool isempty()const
bool isfull()const
void show();
private:
void adjustup(int j);
t *q;
int n,maxsize;
};template prioqueue::prioqueue(int msize)
template void prioqueue::adjustup(int j)
q[i]=temp;
}template void prioqueue::show()
cout<優先權佇列測試main原始碼
#include "prioqueue.h"
enum resultcode;
void main()
q1.show();
}
2.堆的構造樣例原始碼
#include
using
namespace
std;
template
void adjustdown(t heap,int r,int j)
if(temp<=heap[child])
heap[(child-1)/2]=heap[child];
child=child*2+1;
}heap[(child-1)/2]=temp;
}template
void createheap(t heap,int n)
}void main()
; createheap(heap,8);
for(int i=0;i<8;i++)
cout
<}
資料結構C 實現 二叉樹
adt btree btnode const t x btnode const t x,btnode l,btnode r 三個資料成員 t element btnode lchild,rchild 二叉樹類包含唯一的資料成員,它是指向乙個二叉鍊錶根結點的指標root 二叉樹類 先建立二叉樹結點類 ...
資料結構的C實現 二叉樹
二叉樹及其遞迴和非遞迴遍歷 include include define maxkey 100 define ok 1 typedef char elemtype typedef struct node node 二叉樹節點 遞迴先序遍歷 void preorder r node tree 遞迴中序...
資料結構 二叉樹的實現(C )
mi tree測試資料 mi tree實現 include stdafx.h include using namespace std 二叉樹 遞迴實現的二叉樹。萬能的遞迴。class mi tree element pleft element pright char data public mi t...