// author: sky
// date: 2011.11.8
// content: 樹的基本程式
///#include using namespace std;
#define treenodemaxnum 20;
typedef char elementtype;
typedef struct node //樹中的節點
*treenode;
struct bittree //樹
;bittree::bittree()
bool bittree::initialize(treenode &node,elementtype data)
else
return true;
}void bittree::preorder(treenode &node)
}void bittree::levelorder()
if (p->rchild!=null)
}while(front!=last);
}void bittree::preorderbynorecursive()
treenode treestack[20];
treenode p=root;
int top=-1;
do p=treestack[top];
top--;
p=p->rchild;
}while(top!=-1||p!=null);
}void bittree::inorderbynorecursive()
treenode treestack[20];
treenode p=root;
int top=-1;
do p=treestack[top];
top--;
coutp=p->rchild;
}while(top!=-1||p!=null);
}void bittree::postorderbynorecursive()
treenode treestack[20];
elementtype outputchar[20];//存放的是後續遍歷 的結果
treenode p;
int top=-1,count=-1;
top++;
treestack[top]=root;
while(top!=-1)
if (p->rchild!=null)
}for (int i=count;i>=0;i--)
int bittree::getleafnum(treenode &node)
if (node->lchild==null&&node->rchild==null)
else }
int main(int argc,char* argv)
cout<<"preorder :";
bt.preorder(bt.root);
cout<<"\n\n";
cout<<"levelorder :";
bt.levelorder();
cout<<"\n\n";
cout<<"preorderbynorecursive :";
bt.preorderbynorecursive();
cout<<"\n\n";
cout<<"inorderbynorecursive :";
bt.inorderbynorecursive();
cout<<"\n\n";
cout<<"inorderbynorecursive :";
bt.postorderbynorecursive();
cout<<"\n\n";
cout<<"the leaf number of tree is "<
樹的基本概述
乙個結點的度是指該結點的子樹個數,而樹的度是樹中所有結點的度的最大值,數的層規定根結點為第一層,其他所有結點的層都是其父結點的層號加1,樹的高度或深度是樹中所有結點的最大層號。二叉樹的性質 滿二叉樹定義 乙個深度為k且具有 2k 1個結點的二叉樹。完全二叉樹定義 乙個深度為k具有n個結點的二叉樹且其...
樹的基本定義
定義 n n 0 個結點的有限集合。當n 0時,稱為空樹 任意一棵非空樹滿足以下條件 有且僅有乙個特定的稱為根的結點 當n 1時,除根結點之外的其餘結點被分成m m 0 個互不相交的有限集合t1,t2,tm,其中每個集合又是一棵樹,並稱為這個根結點的子樹。結點的度 結點所擁有的子樹的個數。樹的度 樹...
樹的基本操作
輸入輸入某二叉樹的前序遍歷和中序遍歷的結果,請重構該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不包含重複的數字。例如,輸入前序遍歷序列和中序遍歷序列,重建該樹。樹的結點類如下 class treenode 通過前序遍歷獲取根節點的值,然後遍歷中序遍歷序列,找到根節點,將中序遍歷陣列分成左右兩個樹...