二叉树创建、遍历(递归和非递归)、查找、高度、宽度、节点个数、叶子个数、找最近公共父节点,镜像树等基本方法
#include<iostream>
using namespace std;
#include<queue>
#include<stack>
#include<deque>
class Node
{
public:
char value;
Node *left;
Node *right;
Node():left(NULL),right(NULL){}
Node(char ch):value(ch),left(NULL),right(NULL){}
};
class Tree
{
public:
Node *root;
Tree():root(NULL){}
void create(Node *&node,char *&str) //树的创建
{
if(*str=='#' || *str=='\0')
node = NULL;
else
{
node = new Node(*str);
create(node->left,++str);
create(node->right,++str);
}
}
//前序遍历(递归)
void preOrder(Node *root)
{
if(root != NULL)
{
cout<<root->value;
preOrder(root->left);
preOrder(root->right);
}
}
//中序遍历(递归)
void inOrder(Node *root)
{
if(root!=NULL)
{
inOrder(root->left);
cout<<root->value;
inOrder(root->right);
}
}
//后序遍历(递归)
void postOrder(Node *root)
{
if(root != NULL)
{
postOrder(root->left);
postOrder(root->right);
cout<<root->value;
}
}
//层次遍历(队列)
void floor(Node *root)
{
queue<Node*> que;
if(root != NULL)
que.push(root);
while(!que.empty())
{
Node *p = que.front();
cout<<p->value;
que.pop();
if(p->left != NULL)
que.push(p->left);
if(p->right !=NULL)
que.push(p->right);
}
}
//求节点个数(递归)
int size(Node *root)
{
if(root==NULL)
return 0;
else
return size(root->left) + size(root->right) + 1 ;
}
//求树高度(递归)
int height(Node *root)
{
if(root == NULL)
return 0;
else
{
int m = height(root->left);
int n = height(root->right);
return (m>n?m:n)+1;
}
}
//搜索树节点(递归)
Node *search(Node *root,char k)
{
Node *p = NULL;
if(root == NULL)
return NULL;
if(root->value == k)
return root;
p = search(root->left,k);
if(p != NULL)
return p;
return search(root->right,k);
}
//找父节点(递归)
Node *parent(Node *root,char k)
{
Node *q = NULL;
if(root == NULL || root->value==k)
return NULL;
if(root->left!=NULL&&root->left->value == k || root->right!=NULL&&root->right->value == k)
return root;
else
{
q = parent(root->left,k);
if(q!=NULL)
return q;
return parent(root->right,k);
}
}
//找左孩子
Node *leftChild(Node *node)
{
if(node !=NULL)
return node->left;
return NULL;
}
//找右孩子
Node *rightChild(Node *node)
{
if(node != NULL)
return node->left;
return NULL;
}
//前序遍历(非递归 栈)
void PreOrder(Node *root)
{
stack<Node*> s;
s.push(root);
while(!s.empty())
{
Node *p = s.top();
cout<<p->value<< " ";
s.pop();
if(p->right != NULL)
s.push(p->right);
if(p->left != NULL)
s.push(p->left);
}
}
//中序遍历(非递归 栈)
void InOrder(Node *root)
{
stack<Node*> s;
Node *p = root;
while(p || !s.empty())
{
if(p != NULL)
{
s.push(p);
p = p->left;
}
else
{
p = s.top();
cout<<p->value;
s.pop();
p = p->right;
}
}
}
//后序遍历(非递归 栈)
void PostOrder(Node *root)
{
stack<Node*> s;
Node *pre = NULL; //前一次访问的节点
s.push(root);
while(!s.empty())
{
Node *cur = s.top();
if((cur->left==NULL && cur->right==NULL) || (pre!=NULL &&(pre==cur->left||pre==cur->right)))
{
cout<<cur->value;
s.pop();
pre=cur;
}
else
{
if(cur->right!=NULL)
s.push(cur->right);
if(cur->left!=NULL)
s.push(cur->left);
}
}
}
//求叶子节点个数 (递归)
int leafSize(Node *root)
{
if(root == NULL)
return 0;
if(root->left==NULL && root->right==NULL)
return 1;
else
return leafSize(root->left) + leafSize(root->right);
}
//交换二叉树的左右儿子(镜像树)(递归)
void SwapLeftRightChild(Node *root)
{
if(root==NULL ||(root->left==NULL && root->right==NULL))
return;
Node *tmp = root->left;
root->left = root->right;
root->right = tmp;
SwapLeftRightChild(root->left);
SwapLeftRightChild(root->right);
}
//最近公共父节点
Node* lowestAncestor(Node *root,char a,char b)
{
if(root==NULL || root->value==a || root->value==b)
return root;
Node *p = lowestAncestor(root->left,a,b);
Node *q = lowestAncestor(root->right,a,b);
if(p != NULL && q !=NULL)
return root;
return p != NULL ? p:q;
}
//求宽度(双端队列)
int width(Node *root)
{
if(root==NULL)
return 0;
deque<Node*> Q1,Q2;
Q1.push_back(root);
int tmp_width;
int max_width = 1;
while(!Q1.empty())
{
while(!Q1.empty())
{
Node *p = Q1.front();
Q1.pop_front();
if(p->left != NULL)
Q2.push_back(p->left);
if(p->right != NULL)
Q2.push_back(p->right);
}
tmp_width = Q2.size();
if(tmp_width > max_width)
max_width = tmp_width;
Q1 = Q2;
Q2.clear();
}
return max_width;
}
};
void main()
{
char *str = "ABC##DE##F##G#H##";
Tree t;
t.create(t.root,str);
t.preOrder(t.root);
cout<<endl;
t.floor(t.root);
cout<<endl;
t.postOrder(t.root);
cout<<endl;
t.PostOrder(t.root);
cout<<endl;
cout<<"size = "<<t.size(t.root)<<endl;
cout<<"height = "<<t.height(t.root)<<endl;
cout<<t.parent(t.root,'H')->value<<endl;
cout<<t.leftChild(t.root)->value<<endl;
cout<<"leafSize = "<<t.leafSize(t.root)<<endl;
t.SwapLeftRightChild(t.root);
t.preOrder(t.root);
cout<<endl;
cout<<t.lowestAncestor(t.root,'B','G')->value<<endl;
cout<<"width = "<<t.width(t.root)<<endl;
}