第一次學二叉樹啊,基本的功能為:建立,求葉子,求樹高,前序(遞迴&&非遞迴),中序(遞迴&&非遞迴),後序(遞迴&&非遞迴),輸出.....
雖然有很多成分是書上的,不過辛苦了很久,安慰一下自己吧
#include
#include "head.h"
using namespace std;
int main()
//析構函式
void push(const t& x); //如果isfull(),則溢位;否則把x插入到棧的棧頂
bool pop(t& x); //如果isempty(),則不執行退棧,返回false,否則推掉棧頂元素,返回true,退出的值通過引用形參x返回
bool isempty() const //判斷棧是否為空
bool isfull() const //判斷棧是否滿
bool gettop(t &x); //得到棧頂元素
private:
t *elements; //存放棧中元素的棧陣列
int top; //棧頂指標
int maxsize; //棧最大可容納元素個數
void overflowprocess(); //棧的溢位處理
};//-----------------------------------建構函式-----------------------------------
template
stack::stack(int sz)
//-----------------------------------進棧函式-----------------------------------
template
void stack::push(const t& x)
else
}//-----------------------------------出棧函式-----------------------------------
template
bool stack::pop(t& x)
else
}//-----------------------------------擴充棧空間函式-----------------------------------
template
void stack::overflowprocess()
maxsize=maxsize+stackincreament;
delete elements;
elements=newarray;
}//-----------------------------------得到棧頂元素-----------------------------------
template
bool stack::gettop(t &x)
}#include
#include "stack.cpp"
using namespace std;
template //定義乙個結點
struct bintreenode
bintreenode(t x, bintreenode*l=null,bintreenode*r=null):data(x),leftchild(l),rightchild(r) {}
};template //二叉樹類的定義
class binarytree
binarytree(t value):refvalue(value),root(null) {}
bintreenode*getroot()const //取根
int height(bintreenode*subtree); //求樹高
int leafsize(bintreenode*subtree); //求葉子總數
void preorder(bintreenode*subtree); //前序遍歷的遞迴
void no_preorder(bintreenode*subtree); //前序遍歷的非遞迴
void inorder(bintreenode*subtree); //中序遍歷的遞迴
void no_inorder(bintreenode*subtree); //中序遍歷的非遞迴
void postorder(bintreenode*subtree); //後序遍歷的遞迴
void no_postorder(bintreenode*subtree); //後序遍歷的非遞迴
void print(bintreenode*subtree); //輸出二叉樹
void create(bintreenode*&subtree); //以廣義表的形式建立二叉樹
private:
bintreenode*root;
t refvalue;
};//----------------------------------求樹高--------------------------------
template
int binarytree::height(bintreenode*subtree)
else
leafsize(subtree->rightchild);
}return count;
}//----------------------------------前序遍歷的遞迴 --------------------------------
template
void binarytree::preorder(bintreenode*subtree)
}//----------------------------------前序遍歷的非遞迴 --------------------------------
template
void binarytree::no_preorder(bintreenode*subtree)
if(p->leftchild)
else }}
//----------------------------------中序遍歷的遞迴 --------------------------------
template
void binarytree::inorder(bintreenode*subtree)
}//----------------------------------中序遍歷的非遞迴 --------------------------------
template
void binarytree::no_inorder(bintreenode*subtree)
if(!s.isempty())
}while(p!=null||!s.isempty());
}//----------------------------------後序遍歷的遞迴 --------------------------------
template
void binarytree::postorder(bintreenode*subtree)
}//----------------------------------後序遍歷的非遞迴 --------------------------------
template
struct stknode
;stknode(bintreenode*n=null):ptr(n),tag(l) {}
};template
void binarytree::no_postorder(bintreenode*subtree)
int continue1=1;
while(continue1&&!s.isempty())
}}while(!s.isempty());
cout<
void binarytree::print(bintreenode*subtree)
else if(k==1)
else
}cin>>ch;}}
PHP實現排序二叉樹的基本功能
本文實現了 排序二叉樹節點的插入,中序遍歷,極值的查詢和特定值的查詢的功能.基本沒有提供什麼概念和定義.建議先簡單了解一下本文提供的幾個概念在來看本文.實際上,只是簡單的提供了 注釋也很少,各位辛苦了.二叉樹 在電腦科學中,二叉樹是每個節點最多有兩個子樹的樹結構。排序二叉樹 左孩子節點的值小於父節點...
用C 遞迴的方法實現二叉樹的基本功能
各功能的實現主要是運用遞迴的方法 運用遞迴的方法特點就是 簡短且易於理解 首先是標頭檔案的引入 引入了佇列 include includeusing namespace std 然後是樹的實現部分 用類的方法實現 函式實現方法均在類內實現 運用遞迴實現各項功能 class node 節點 char ...
PHP排序二叉樹基本功能實現方法示例
這裡演示了排序二叉樹節點的插入,中序遍歷,極值的查詢和特定值的查詢的功能.基本沒有提供什麼概念和定義.建議先簡單了解一下本文提供的幾個概念在來看本文.實際上,只是簡單的提供了 注釋也很少,各位辛苦了.二叉樹 在電腦科學中,二叉樹是每個節點最多有兩個子樹的樹結構。排序二叉樹 左孩子節點的值小於父節點的...