二叉树模板!

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <cstring>
 5 #define N 30
 6 using namespace std;
 7 
 8 struct tree
 9 {
10     int data;
11     tree *parent;
12     tree *left;
13     tree *right;
14 };
15 
16 tree* creat(int data)
17 {
18     tree *p=(tree *)malloc(sizeof(tree));
19     p->data=data;
20     return p;
21 }
22 
23 tree* find(const tree *p,int data)
24 {
25     if(p==NULL) return 0;
26     if(data==p->data) return (tree *) p;
27     else if(data<p->data) return find(p->left,data);
28     else return find(p->right , data);
29 
30 }
31 
32 int sumnode(const tree* p)
33 {
34     if(p==NULL) return 0 ;
35     return 1+sumnode(p->left)+sumnode(p->right);
36 }
37 
38 int high(const tree* p)
39 {
40     int left ,right;
41     if(p==NULL)
42         return 0 ;
43     left=high(p->left);
44     right=high(p->right);
45     return (left>right) ? (left+1) : (right+1);
46 }
47 
48 void print(const tree *p)
49 {
50     if(p!=NULL)
51     {
52         print(p->left);
53         cout<<p->data<<endl;
54         print (p->right );
55     }
56 }
57 
58 int main()
59 {
60     //freopen("ACM.txt","r",stdin);
61 
62 
63 }
View Code

 

posted on 2014-10-14 20:04  M.D.LUFFI  阅读(111)  评论(0编辑  收藏  举报

导航