二叉树类

 

  1 struct Node {
  2     int data;
  3     Node* left;
  4     Node* right;
  5     Node(int d) : data(d),left(0),right(0) {
  6     }
  7 };
  8 
  9 class Tree {
 10 private:
 11     Node* root;
 12     int count;
 13     int recursiveHeight(Node* subTree);
 14     void recursivePreOrder(Node* subTree);
 15     void recursiveInsert(const int& value, Node*& subTree);
 16     void postOrderRemove(Node*& subTree);
 17 public:
 18     Tree();
 19     ~Tree();
 20     int height() const;
 21     void insert(const int& value);
 22     void preOrder();
 23     void bfs();
 24     void dfs(Node* subTree) const;
 25     Node* getRoot() const { return root; }
 26     void clear();
 27 };
 28 
 29 Tree::Tree() 
 30     :root(0), count(0){
 31 
 32 }
 33 
 34 Tree::~Tree() {
 35     clear();
 36 }
 37 
 38 int Tree::recursiveHeight(Node* subTree) {
 39     if (subTree == NULL)
 40         return 0;
 41     int left = recursiveHeight(subTree->left);
 42     int right = recursiveHeight(subTree->right);
 43     if (left > right) {
 44         return left + 1;
 45     } else {
 46         return right + 1;
 47     }
 48 }
 49 
 50 void Tree::recursiveInsert(const int& value, Node*& subTree) {
 51     if (subTree == NULL) {
 52         Node *node = new Node(value);
 53         subTree = node;
 54         count++;
 55     } else {
 56         if (recursiveHeight(subTree->left) <= recursiveHeight(subTree->right)) {
 57             recursiveInsert(value, subTree->left);
 58         } else {
 59             recursiveInsert(value, subTree->right);
 60         }
 61     }
 62 }
 63 
 64 void Tree::insert(const int& value) {
 65     recursiveInsert(value, root);
 66 }
 67 
 68 void Tree::recursivePreOrder(Node* subTree) {
 69     if (subTree != NULL) {
 70         printf("%d ", subTree->data);
 71         recursivePreOrder(subTree->left);
 72         recursivePreOrder(subTree->right);
 73     }
 74 }
 75 
 76 void Tree::preOrder() {
 77     recursivePreOrder(root);
 78     printf("\n");
 79 }
 80 
 81 void Tree::bfs() {
 82     queue<Node*> q;
 83     if (root != NULL) {
 84         q.push(root);
 85         while (!q.empty()) {
 86             Node *node = q.front();
 87             printf("%d ", node->data);
 88             if (node->left != NULL) {
 89                 q.push(node->left);
 90             }
 91             if (node->right != NULL) {
 92                 q.push(node->right);
 93             }
 94             q.pop();
 95         }
 96         printf("\n");
 97     }
 98 }
 99 
100 void Tree::dfs(Node* subTree) const {
101     if (subTree != NULL) {
102         printf("%d ", subTree->data);
103         dfs(subTree->left);
104         dfs(subTree->right);
105     }
106 }
107 
108 void Tree::clear() {
109     postOrderRemove(root);
110 }
111 
112 void Tree::postOrderRemove(Node*& subTree) {
113     if (subTree != NULL) {
114         postOrderRemove(subTree->left);
115         postOrderRemove(subTree->right);
116         delete subTree;
117         subTree = NULL;
118         count--;
119     }
120 }

 

转载于:https://www.cnblogs.com/strikew/p/3309518.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值