實現二叉樹的基本操作:建立、遍歷、計算深度、結點數、葉子數等。
輸入c,先序建立二叉樹,#表示空節點;
輸入h:計算二叉樹的高度;
輸入l:計算二叉樹的葉子個數;
輸入n:計算二叉樹節點總個數;
輸入1:先序遍歷二叉樹;
輸入2:中序遍歷二叉樹;
輸入3:後續遍歷二叉樹;
輸入f:查詢值=x的節點的個數;
輸入p:以縮格文字形式輸出所有節點
#include
//#include "bintree.h"
#include
//#include "cstdio"
#include
using namespace std;
class tree;
class treenode;
treenode(char data1):data(data1),left(null),right(null){}
};class tree
tree(char end1,treenode *p=
null) :root(p),endtag(end1){}
~tree()
bool isempty()
treenode *left(treenode *subtree)
treenode *right(treenode *subtree)
treenode *getroot()
void createtree()
void inorder()
void preorder()
void postorder()
treenode *
parent(treenode *current)
int size ()
int height()
int leaf()
int find1(char &x)
void print()
};int tree::size(treenode *subtree)
void tree::createtree(treenode *&subtree)
}void tree::destroy(treenode *&subtree)
}void tree::inorder(treenode *subtree)
}void tree::preorder(treenode *subtree)
}void tree::postorder(treenode *subtree)
}treenode *tree::parent(treenode *subtree,treenode *current)
else
return
parent(subtree->right,current);
}int tree::height(treenode *subtree)
int tree::leaf(treenode *subtree)
int tree::find1(char &x,treenode *subtree)
void tree::print(treenode *subtree,int layer)
}using namespace std;
int main()
else
if(a==
'h')
cout<<
"height="
<"."
if(a==
'l')
cout<<
"leaf="
<"."
if(a==
'n')
cout<<
"nodes="
<"."
if(a==
'1')
else
if(a==
'2')
else
if(a==
'3')
else
if(a==
'f')
else
if(a==
'p')
}return
0;}
二叉樹 排序二叉樹的簡單實現
二叉樹 排序二叉樹 include using namespace std 二叉樹的節點 date 資料 left 指向二叉樹的左子樹 right 指向二叉樹的右子樹 template struct node template class btree public btree root null c...
二叉樹實現
include include include include define maxsize 100 define ok 1 define error 0 define true 1 define false 0 typedef int status typedef int telemtype ty...
二叉樹實現
課內最近學了二叉樹,參考書上的 做了二叉樹的實現,尚不完善,還有很多地方不明白。二叉樹實現。define maxsize 100 include using namespace std 定義二叉樹節點 class btnode void createbt btnode bt,char str voi...