二叉树的构造和遍历

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 typedef struct _NODE//节点结构
 5 {
 6     struct _NODE* leftChild;
 7     int value;
 8     struct _NODE* rightChild;
 9 } NODE, *PNODE;
10 
11 PNODE createNode(int value){//创建一个新节点
12     PNODE n = (PNODE)malloc(sizeof(NODE));
13     n->value = value;
14     n->leftChild = NULL;
15     n->rightChild = NULL;
16     return n;
17 }
18 
19 PNODE insertLeftChild(PNODE parent, int value){//在指定节点上插入左节点
20     return (parent->leftChild = createNode(value));
21 }
22 
23 PNODE insertRightChild(PNODE parent, int value){//在指定节点上插入左节点
24     return (parent->rightChild = createNode(value));
25 }
26 
27 void createBTree(PNODE root, int i){//向树中插入一些元素        
28     if (i == 0)                                                             
29     {                                                        
30         return;                                                
31     }                                                       
32     else{
33         PNODE l = insertLeftChild(root, i * 10 + 1);
34         PNODE r = insertRightChild(root, i * 10 + 2);
35         createBTree(l, --i);
36         createBTree(r, i);
37     }
38 }
39 
40 void printDLR(PNODE root){//先序遍历:对每一刻子树都是根->左->右的顺序
41     if (root == NULL)
42     {
43         return;
44     }
45     printf("%-4d", root->value);
46     printDLR(root->leftChild);
47     printDLR(root->rightChild);
48 }
49 
50 void printLDR(PNODE root){//中序遍历:
51     if (root == NULL)
52     {
53         return;
54     }
55     printLDR(root->leftChild);
56     printf("%-4d", root->value);
57     printLDR(root->rightChild);
58 }
59 
60 void printLRD(PNODE root){//后序遍历
61     if (root == NULL)
62     {
63         return;
64     }
65     printLRD(root->leftChild);
66     printLRD(root->rightChild);
67     printf("%-4d", root->value);
68 }
69 
70 void main(){
71     PNODE root = createNode(0);//创建根节点
72     createBTree(root, 3);
73     
74     printf("先序遍历: ");
75     printDLR(root);//遍历
76     printf("\n中序遍历: ");
77     
78     printLDR(root);
79     printf("\n后序遍历: ");
80     
81     printLRD(root);
82     printf("\n");
83 }

执行结果:

 先序遍历:

中序遍历:

后序遍历:

C++中可以使用类模板,从而使节点值的类型可以不止限定在整型:

 

  1 #include <iostream.h>
  2 
  3 template <class T> class Node//节点类模板
  4 {
  5 public:
  6     Node(T value):value(value)//构造方法
  7     {
  8         leftChild = 0; 
  9         rightChild = 0;
 10     }
 11     Node* insertLeftChild(T value);//插入左孩子,返回新节点指针
 12     Node* insertRightChild(T vallue);//插入右孩子
 13     void deleteLeftChild();//删左孩子
 14     void deleteRightChild();//删右孩子
 15     void showDLR();//先序遍历
 16     void showLDR();//中序遍历
 17     void showLRD();//后序遍历
 18 protected:
 19     T value;//节点值
 20     Node* leftChild;//左孩子指针
 21     Node* rightChild;//右孩子指针
 22 private:
 23 };
 24 
 25 template <class T> Node<T>* Node<T>::insertLeftChild(T value){//插入左孩子
 26     return (this->leftChild = new Node(value));
 27 }
 28 
 29 template <class T> Node<T>* Node<T>::insertRightChild(T value){//插入右孩子
 30     return (this->rightChild = new Node(value));
 31 }
 32 
 33 template <class T> void Node<T>::deleteLeftChild(){//删除左孩子
 34     delete this->leftChild;
 35     this->leftChild = 0;
 36 }
 37 
 38 template <class T> void Node<T>::deleteRightChild(){//删除右孩子
 39     delete this->rightChild;
 40     this->rightChild = 0;
 41 }
 42 
 43 template <class T> void Node<T>::showDLR(){//先序遍历
 44     cout<<this->value<<" ";
 45     if (leftChild)
 46     {
 47         leftChild->showDLR();
 48     }
 49     if (rightChild)
 50     {
 51         rightChild->showDLR();
 52     }
 53 }
 54 
 55 template <class T> void Node<T>::showLDR(){//中序遍历
 56     if (leftChild)
 57     {
 58         leftChild->showLDR();
 59     }
 60     cout<<this->value<<" ";
 61     if (rightChild)
 62     {
 63         rightChild->showLDR();
 64     }
 65 }
 66 
 67 template <class T> void Node<T>::showLRD(){//后序遍历
 68     if (leftChild)
 69     {
 70         leftChild->showLRD();
 71     }
 72     if (rightChild)
 73     {
 74         rightChild->showLRD();
 75     }
 76     cout<<this->value<<" ";
 77 }
 78 
 79 template <class T> void createSomeNodes(Node<T>* root, int i, T base){//构建一个二叉树
 80     if (i == 0)
 81     {
 82         return;
 83     }
 84     Node<T>* l = root->insertLeftChild(i + base);
 85     Node<T>* r = root->insertRightChild(i + base);
 86     createSomeNodes(l, --i, base);
 87     createSomeNodes(r, i, base);
 88 }
 89 
 90 template <class T> void showTest(Node<T>* root){//显示各种遍历方式结果
 91     cout<<"先序遍历: ";
 92     root->showDLR();
 93     cout<<endl<<"中序遍历: ";
 94     root->showLDR();
 95     cout<<endl<<"后序遍历: ";
 96     root->showLRD();
 97     cout<<endl;
 98 }
 99 
100 void main(){
101     Node<int> *root1 = new Node<int>(0);
102     createSomeNodes(root1, 3, 0);
103     cout<<"整型:"<<endl;
104     showTest(root1);
105 
106     Node<char> *root2 = new Node<char>('a');
107     createSomeNodes(root2, 3, 'a');
108     cout<<"字符型:"<<endl;
109     showTest(root2);
110 
111     Node<float> *root3 = new Node<float>(0.1f);
112     createSomeNodes(root3, 3, 0.1f);
113     cout<<"浮点型:"<<endl;
114     showTest(root3);
115 }

 

 

posted @ 2012-10-06 14:33  丛林听雨  阅读(352)  评论(0编辑  收藏  举报