C++实现一颗二叉树

Binary_Tree.h

#pragma once
#include<iostream>
#include<stack>
#include<queue>
//节点
struct Node
{
 char data;
 struct Node *Lchild,*Rchild;
};
template <class T>
class Binary_Tree
{
private:
 Node *root;
public:
 Binary_Tree(const T *arr);
 Node *Create_Tree(const T *arr);
 ~Binary_Tree();
 void Clear();
 void clear(Node* &cur);
 void Empty();
 void PreOrder();
 void preorder(Node *cur);
 void InOrder();
 void inorder(Node *cur);
 void PostOrder();
 void postorder(Node *cur);
 void PreOrder_Non();
 void InOrder_Non();
 void PostOrder_Non();
 void LevelOrder();
 size_t Find_K_Level(size_t K);
 size_t find_K_level(Node *cur,size_t K);
 T Find(const T e);
 Node *find(Node *cur,const T e);
 size_t Size();
 size_t size(Node *cur);
 size_t Depth();
 size_t depth(Node *cur);
};
//构造函数
template <class T>
Binary_Tree<T>::Binary_Tree(const T *arr)
{
 root = Create_Tree(arr);
}
//创建一颗二叉树
template <class T>
Node *Binary_Tree<T>::Create_Tree(const T *arr)
{
 Node *cur = NULL;
 static int i = 0;
 T ch = arr[i++];
 if (ch == '#')
  cur = NULL;
 else
 {
  cur = new Node;
  if(!cur)
  {
   cout<<"error";
   return NULL;
  }
  cur->data = ch;
  cur->Lchild = Create_Tree(arr);
  cur->Rchild = Create_Tree(arr);
 }
 return cur;
}
//析构函数
template <class T>
Binary_Tree<T>::~Binary_Tree()
{
 Clear();
}
//清空一颗二叉树
template <class T>
void Binary_Tree<T>::Clear()
{
 clear(root);
}
template <class T>
void Binary_Tree<T>::clear(Node* &cur)
{
 if (cur)
 {
  if (cur->Lchild)
   clear(cur->Lchild);
  if (cur->Rchild)
   clear(cur->Rchild);
  delete cur;
  cur = NULL;
 }
}
//判断是否为空树
template <class T>
void Binary_Tree<T>::Empty()
{
 if (root)
  cout<<"false";
 else
  cout<<"ture";
 cout<<endl;
}
//前序遍历(递归)
template <class T>
void Binary_Tree<T>::PreOrder()
{
 preorder(root);
 cout<<endl;
}
template <class T>
void Binary_Tree<T>::preorder(Node *cur)
{
 if (cur == NULL)
  return;
 else
 {
  cout<<cur->data;
  preorder(cur->Lchild);
  preorder(cur->Rchild);
 }
}
//中序遍历(递归)
template <class T>
void Binary_Tree<T>::InOrder()
{
 inorder(root);
 cout<<endl;
}
template <class T>
void Binary_Tree<T>::inorder(Node *cur)
{
 if (cur == NULL)
  return;
 else
 {
  inorder(cur->Lchild);
  cout<<cur->data;
  inorder(cur->Rchild);
 }
}
//后序遍历(递归)
template <class T>
void Binary_Tree<T>::PostOrder()
{
 postorder(root);
 cout<<endl;
}
template <class T>
void Binary_Tree<T>::postorder(Node *cur)
{
 if (cur == NULL)
  return;
 else
 {
  postorder(cur->Lchild);
  postorder(cur->Rchild);
  cout<<cur->data;
 }
}
//前序遍历(非递归)
template <class T>
void Binary_Tree<T>::PreOrder_Non()
{
 Node *cur = root;
 if (cur == NULL)
  return;
 stack<Node *> s;
 while (cur || !s.empty())
 {
  while (cur)
  {
   s.push(cur);
   cout<<cur->data;
   cur = cur->Lchild;
  }
  Node *top = s.top();
  s.pop();
  cur = top->Rchild;
 }
 cout<<endl;
}
//中序遍历(非递归)
template <class T>
void Binary_Tree<T>::InOrder_Non()
{
 Node *cur = root;
 if (cur == NULL)
  return;
 stack<Node *> s;
 while (cur || !s.empty())
 {
  while (cur)
  {
   s.push(cur);
   cur = cur->Lchild;
  }
  Node *top = s.top();
  cout<<top->data;
  s.pop();
  cur = top->Rchild;
 }
 cout<<endl;
}
//后序遍历(非递归)
template <class T>
void Binary_Tree<T>::PostOrder_Non()
{
 Node *prev = NULL;
 Node *cur = root;
 if (cur == NULL)
  return;
 stack<Node *> s;
 while (cur || !s.empty())
 {
  while (cur)
  {
   s.push(cur);
   cur = cur->Lchild;
  }
  Node *top = s.top();
  if (top->Rchild == NULL || top->Rchild == prev)
  {
   cout<<top->data;
   prev = top;
   s.pop();
  }
  else
   cur = top->Rchild;
 }
 cout<<endl;
}
//层序遍历
template <class T>
void Binary_Tree<T>::LevelOrder()
{
 Node *cur = root;
 if (cur == NULL)
  return;
 queue<Node *> q;
 q.push(cur);
 while (!q.empty())
 {
  Node *top = q.front();
  q.pop();
  cout<<top->data;
  if (top->Lchild)
   q.push(top->Lchild);
  if (top->Rchild)
   q.push(top->Rchild);
 }
 cout<<endl;
}
//第K层的节点数
template <class T>
size_t Binary_Tree<T>::Find_K_Level(size_t K)
{
 return find_K_level(root,K);
}
template <class T>
size_t Binary_Tree<T>::find_K_level(Node *cur,size_t K)
{
 if (cur == NULL || K < 1)
  return 0;
 if (K == 1)
  return 1;
 size_t left = find_K_level(cur->Lchild,K-1);
 size_t right = find_K_level(cur->Rchild,K-1);
 return left+right;
}
//查找e是否在里面并返回
template <class T>
T Binary_Tree<T>::Find(const T e)
{
 if (find(root,e) == NULL)
  return '#';
 else
  return find(root,e)->data;
}
template <class T>
Node *Binary_Tree<T>::find(Node *cur,const T e)
{
 if (cur == NULL)
  return cur;
 Node *ret = NULL;
 if (cur->data == e)
  return cur;
 else
 {
  ret = find(cur->Lchild,e);
  if (ret == NULL)
   ret = find(cur->Rchild,e);
 }
 return ret;
}
//查找二叉树的总结点
template <class T>
size_t Binary_Tree<T>::Size()
{
 return size(root);
}
template <class T>
size_t Binary_Tree<T>::size(Node *cur)
{
 size_t ret = 0;
 if (cur == NULL)
  return ret;
 else
 {
  ret++;
  ret += size(cur->Lchild);
  ret += size(cur->Rchild);
 }
 return ret;
}
//返回二叉树的深度
template <class T>
size_t Binary_Tree<T>::Depth()
{
 return depth(root);
}
template <class T>
size_t Binary_Tree<T>::depth(Node *cur)
{
 if (cur == NULL)
  return 0;
 int left = depth(cur->Lchild)+1;
 int right = depth(cur->Rchild)+1;
 return left > right ? left : right;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值