struct BNode {
char data;
BNode *lChild, *rChild;
BNode(char a) {
data = a;
}
};
复制代码
1.使用带终止符的前序遍历
形式:ABC##DE#G##F###html
///前序遍历建立二叉树(带停止标记)
void CreateBinTree_UsePreFlag(){
cout << "please input preOrder number with refuse value: " << endl;
CreateBinTree_UsePreFlag(root);
}
void CreateBinTree_UsePreFlag(BNode* &subTree) { //传入的指针必定要是引用,不然没法修改传入指针的指向位置
char item;
if (cin >> item)
{
if (item != refuseValue)
{
subTree = new BNode(item);
CreateBinTree_UsePreFlag(subTree->lChild);
CreateBinTree_UsePreFlag(subTree->rChild);
}
else {
subTree = NULL;
}
}
}
复制代码
2.使用广义表建立
广义表形式:A(B(D,E(G,)),C(,F))#函数
///使用广义表建立
void CreateBinTree_UseGenTable() {
cout << "please input Generalized table: " << endl;
CreateBinTree_UseGenTable(root);
}
///使用广义表建立二叉树函数,这里以“字符”建立二叉树,以'#'字符表明结束
void CreateBinTree_UseGenTable(BNode* &BT) {
stack<BNode*> s;
BT = NULL;
BNode *p, *t; //p用来记住当前建立的节点,t用来记住栈顶的元素
int k; //k是处理左、右子树的标记
char ch;
while (1)
{
cin >> ch;
if (ch == refuseValue)
break;
switch (ch)
{
case '(': //对(作处理
s.push(p);
k = 1;
break;
case ')': //对)作处理
s.pop();
break;
case ',': //对,作处理
k = 2;
break;
default:
p = new BNode(ch); //构造一个结点,!注意要给新节点的左右孩子指针赋空值!
p->lChild = NULL;
p->rChild = NULL;
if (BT == NULL) //若是头节点是空
{
BT = p;
}
else {
if (k == 1) //链入*t的左孩子
{
t = s.top();
t->lChild = p;
}
else //链入*t的右孩子
{
t = s.top();
t->rChild = p;
}
}
}
}
}
复制代码
3.使用前序遍历和中序遍历建立post
///使用先序遍历和中序遍历建立
void CreateBinTree_Pre_mid() {
char pre[50];
char mid[50];
cout << "please input preOrder: " << endl;
cin >> pre;
cout << "please input midOrder: " << endl;
cin >> mid;
string s1(pre);
string s2(mid);
if (s1.length() != s2.length()) {
cout << "error input!" << endl;
return;
}
int n = s1.length();
CreateBinTree_Pre_mid(root,pre,mid,n);
}
void CreateBinTree_Pre_mid(BNode *&cur, const char *pre, const char *mid, int n) {
if (n <= 0)
{
cur = NULL;
return;
}
int k = 0;
while (pre[0] != mid[k]) {
k++;
}
cur = new BNode(mid[k]); //建立结点
CreateBinTree_Pre_mid(cur->lChild, pre + 1, mid,k);
CreateBinTree_Pre_mid(cur->rChild, pre + k + 1, mid + k + 1,n-k-1);
}
复制代码
4.使用后续遍历和中序遍历建立ui
///使用后序遍历和中序遍历建立(与上方法相似)
void CreateBinTree_post_mid() {
char post[50];
char mid[50];
cout << "please input postOrder: " << endl;
cin >> post;
cout << "please input midOrder: " << endl;
cin >> mid;
string s1(post);
string s2(mid);
if (s1.length() != s2.length()) {
cout << "error input!" << endl;
return;
}
int n = s1.length();
CreateBinTree_post_mid(root, post, mid, n);
}
///后序遍历和中序遍历建立二叉树
void CreateBinTree_post_mid(BNode* &cur, const char *post, const char *mid, int n) {
if (n <= 0)
{
cur = NULL;
return;
}
int k = 0;
while (post[n - 1] != mid[k]) {
k++;
}
cur = new BNode(mid[k]);
CreateBinTree_post_mid(cur->lChild, post, mid, k);
CreateBinTree_post_mid(cur->rChild, post + k, mid + k+1,n-k-1);
}
复制代码
1.先序遍历spa
///先序遍历
void preOrder() {
cout << "preOrder num: ";
//递归遍历
// preOrderPrint_UseRecursion(root);
//使用栈遍历
// preOrderPrint_UseStack1();
preOrderPrint_UseStack2();
cout << endl;
}
//1 递归遍历
void preOrderPrint_UseRecursion(BNode* subTree) {
if (subTree != NULL) {
cout << subTree->data << " ";
preOrderPrint_UseRecursion(subTree->lChild);
preOrderPrint_UseRecursion(subTree->rChild);
}
}
//2.1 利用栈实现前序遍历的过程。每次访问一个结点后,在向左子树遍历下去以前,利用这个栈记录该结点的右子女(若是有的话)结点的地址,
//以便在左子树退回时能够直接从栈顶取得右子树的根结点,继续其右子树的前序遍历。
void preOrderPrint_UseStack1() {
stack<BNode*> s;
BNode* p=root;
s.push(NULL);
while (p != NULL) {
cout << p->data << " ";
if (p->rChild != NULL)
s.push(p->rChild);
if (p->lChild != NULL)
{
p = p->lChild; //把当前还未处理的右孩子指针存起来
}
else {
p = s.top();
s.pop();
}
}
cout << endl;
}
//2.2 为了保证先左子树后右子树的顺序,在进栈时是先进右子女结点地址,后进左子女结点地址,出栈时正好相反。
void preOrderPrint_UseStack2() {
BNode* p = root;
stack<BNode*> s;
s.push(p);
while (!s.empty()) {
p = s.top();
s.pop();
cout << p->data << " ";
if (p->rChild != NULL)
s.push(p->rChild);
if(p->lChild!=NULL)
s.push(p->lChild);
}
cout << endl;
}
复制代码
2.中序遍历指针
void midOrder() {
cout << "midOrder num: ";
///递归遍历
midOrderPrint_UseRecursion(root);
///使用栈遍历
// midOrderPrint_UseStack();
cout << endl;
}
//1.递归进行遍历
void midOrderPrint_UseRecursion(BNode* subTree) {
if (subTree != NULL) {
midOrderPrint_UseRecursion(subTree->lChild);
cout << subTree->data << " ";
midOrderPrint_UseRecursion(subTree->rChild);
}
}
//2.使用栈进行遍历
void midOrderPrint_UseStack() {
BNode* p = root;
stack<BNode*> s;
do{
while (p != NULL) {
s.push(p);
p = p->lChild;
}
if (!s.empty()) {
p = s.top();
s.pop();
cout << p->data << " ";
p = p->rChild;
}
} while (p != NULL || !s.empty());
}
复制代码
3.后续遍历code
///后序遍历
void postOrder() {
cout << "postOrder num: ";
//递归遍历
// postOrderPrint_UseRecursion(root);
//使用栈遍历
postOrderPrint_UseStack();
cout << endl;
}
//1.递归进行遍历
void postOrderPrint_UseRecursion(BNode* subTree) {
if (subTree != NULL) {
postOrderPrint_UseRecursion(subTree->lChild);
postOrderPrint_UseRecursion(subTree->rChild);
cout << subTree->data << " ";
}
}
//2.使用栈进行遍历
void postOrderPrint_UseStack() {
if (root == NULL)
return;
BNode *p = root;
stack<BNode *> s;
s.push(p);
BNode *lastPop = NULL;
while (!s.empty())
{
while (s.top()->lChild != NULL)
s.push(s.top()->lChild);
while (!s.empty())
{
//右叶子结点 || 没有右结点
if (lastPop == s.top()->rChild || s.top()->rChild == NULL)
{
cout << s.top()->data << " ";
lastPop = s.top();
s.pop();
}
else if (s.top()->rChild != NULL)
{
s.push(s.top()->rChild);
break;
}
}
}
}
复制代码
4.先序遍历(广义表形式)htm
///先序遍历,广义表形式
void preOrder_GenTable() {
cout << "preOrder num with generalize table: ";
GenTablePrint(root);
cout << endl;
}
///二叉树以广义表形式输出
void GenTablePrint(BNode *BT) {
if (BT != NULL) //树为空时结束递归
{
cout << BT->data;
if (BT->lChild != NULL || BT->rChild != NULL)
{
cout << '(';
if (BT->lChild != NULL)
{
GenTablePrint(BT->lChild);
}
cout << ',';
if (BT->rChild != NULL)
{
GenTablePrint(BT->rChild);
}
cout << ')';
}
}
}
复制代码
5.层次遍历blog
///层次遍历(使用队列实现)
void levelOrderPrint() {
BNode* p = root;
queue<BNode*> Queue;
Queue.push(p);
while (1) {
if(p->lChild!=NULL)
Queue.push(p->lChild);
if (p->rChild != NULL)
Queue.push(p->rChild);
cout << p->data << " ";
Queue.pop();
if (Queue.empty())
break;
else
p = Queue.front();
}
}
复制代码
1.获取根节点递归
BNode* getRoot() {
return root;
}
复制代码
2.获取二叉树节点数量
int size() { //二叉树大小
return size(root);
}
///节点p开头的子树节点数目
int size(BNode* p) {
if (p == NULL)
return 0;
return 1 + size(p->lChild) + size(p->rChild);
}
复制代码
3.获取二叉树高度
int height() {
//return height_UseStack(root);
return height_UseRecursion(root);
}
///节点p开头的子树节点高度
int height_UseRecursion(BNode *p) {
if (p == NULL)
return 0;
int i = height_UseRecursion(p->lChild);
int j = height_UseRecursion(p->rChild);
return i > j ? i + 1 : j + 1;
}
int height_UseStack(BNode *T) {
if (!T)
return 0;
int front = -1, rear = -1;
int last = 0, level = 0;
BNode* tree[100];
tree[++rear] = T;
BNode* p;
while (front < rear) {
p = tree[++front];
if (p->lChild != 0)
tree[++rear] = p->lChild;
if (p->rChild != NULL)
tree[++rear] = p->rChild;
if (front == last)
{
level++;
last = rear;
}
}
return level;
}
复制代码
4.寻找某个节点的父节点
BNode* parent(BNode* subTree, BNode* current) {
if (subTree == NULL)
return NULL;
if (subTree->lChild == current || subTree->rChild == current)
return subTree;
BNode* p;
if ((p = parent(subTree->lChild, current)) != NULL)
return p;
else
return parent(subTree->rChild, current);
}
复制代码
5.销毁二叉树并回收空间
void destroy(BNode *p) {
if (p == NULL)
return;
else {
destroy(p->lChild);
destroy(p->rChild);
delete p;
p = NULL;
}
}
复制代码
6.判断两个二叉树是否一致(静态函数)
static bool equal(BNode* a, BNode *b) {
if (a == NULL&&b == NULL)
return true;
if (a != NULL&&b != NULL && (a->data == b->data) && equal(a->rChild, b->rChild) && equal(a->lChild, b->lChild))
return true;
else
return false;
}
复制代码
7.判断是不是彻底二叉树
///判断是不是彻底二叉树_方法1 使用层次遍历,h-1层的最后一个节点序号为pow(2, level - 1) - 1
bool isFullBinaryTree_1() {
BNode *T = root;
if (!T)
return true;
int front = -1, rear = -1;
int last = 0, level = 0;
BNode* a[100];
stack<int> frontHistory;
a[++rear] = T;
BNode* p;
while (front < rear) {
p = a[++front];
if (p->lChild != NULL)
a[++rear] = p->lChild;
if (p->rChild != NULL)
a[++rear] = p->rChild;
if (front == last) {
last = rear;
level++;
frontHistory.push(front);
}
}
frontHistory.pop();
//只须要验证h-1层最后一个节点序号是不是2^(h-1)-1便可!
return (frontHistory.top()+1) == (pow(2, level - 1) - 1);
}
///判断是不是彻底二叉树_方法2 利用性质:层次遍历时出现一个叶子节点则后面的均为叶子节点(空节点)
bool isFullBinaryTree_2() {
BNode* p = root;
queue<BNode*> que;
que.push(p);
while (!que.empty()) {
p = que.front();
que.pop();
if (p) {
que.push(p->lChild);
que.push(p->rChild);
}
else {
while (!que.empty())
{
p = que.front();
que.pop();
if (p)
return false;
}
}
}
return true;
}
复制代码
8.交换节点p为根节点的子树的全部的左右节点
///交换节点p为根节点的子树的全部的左右节点(层次遍历)
void swapLeftAndRight(BNode* p) {
// BNode* p = root;
if (!p)
return;
queue<BNode*> que;
que.push(p);
while (!que.empty()) {
p = que.front();
que.pop();
if (p->lChild != NULL)
que.push(p->lChild);
if (p->rChild != NULL)
que.push(p->rChild);
swap(p);
}
}
复制代码
9.输出中序遍历的第i个值,其余遍历方法相似
void valueOfMidOrderNo(int i) {
int No = 0;
recursiveMidOrderTemp(root, i,No);
}
///输出中序遍历的第n的节点的值
void recursiveMidOrderTemp(BNode * subTree, int i, int &No) {//No须要全部递归部分共同维护
if (subTree != NULL) {
recursiveMidOrderTemp(subTree->lChild, i, No);
// cout << subTree->data << " ";
No++;
if (No == i) {
cout << subTree->data << endl;
return;
}
if (No > i)
return;
recursiveMidOrderTemp(subTree->rChild, i, No);
}
}
复制代码
10.递归遍历寻找顶点到节点值为X的路径(此为逆序输出)
void printWayToX(char x) {
//printWayToX_UseRecursion(root, x);
printWayToX_UseStack(root, x);
cout << endl;
}
//1.使用递归实现
bool printWayToX_UseRecursion(BNode* p, char x) {
if (!p)
return false;
if (printWayToX_UseRecursion(p->lChild, x) || printWayToX_UseRecursion(p->rChild, x))
{
cout << p->data;
return true;
}
else if (p->data == x)
{
cout << p->data;
return true;
}
else {
return false;
}
}
//2.非递归遍历寻找。使用后序遍历,当查找到x时,栈中元素即为x的祖节点
void printWayToX_UseStack(BNode* p, char x) {
stack<BNode*> s;
BNode* lastPos = NULL;
s.push(p);
while (!s.empty()) {
while (s.top()->lChild != NULL)
{
s.push(s.top()->lChild);
//节点值为x的节点入栈后,直接将栈中元素所有输出而后退出该函数
if (s.top()->data == x) {
while (!s.empty()) {
cout << s.top()->data << " ";
s.pop();
}
return;
}
}
while (!s.empty())
{
if (lastPos == s.top()->rChild || s.top()->rChild == NULL)
{
lastPos = s.top();
s.pop();
}
else if (s.top() != NULL)
{
s.push(s.top()->rChild);
//查找到x时,直接将栈中元素所有输出而后退出该函数
if (s.top()->data == x) {
while (!s.empty()) {
cout << s.top()->data << " ";
s.pop();
}
return;
}
break;
}
}
}
}
复制代码
11.找到值为 x 和 y的最近公共祖先节点。先分别利用后序查找x y的祖先节点存储在栈中,再在两个栈中查找最近相同节点!
char ClosestAncestorNode(char x, char y) {
if (root == NULL)
return '#';
stack<BNode*> s1,s2;
AncestorsNodeStack(x,s1);
AncestorsNodeStack(y,s2);
if (s1.empty() || s2.empty())
return '#';
int n = 0;
if (s1.size() >= s2.size())
{
n = s1.size() - s2.size();
for (int i = 0; i < n; i++)
s1.pop();
}
else {
n = s2.size() - s1.size();
for (int i = 0; i < n; i++)
s2.pop();
}
while (!s1.empty()) {
if (s1.top()->data == s2.top()->data)
return s1.top()->data;
s1.pop();
s2.pop();
}
return '#';
}
///返回存储节点值为x的祖先节点的栈
void AncestorsNodeStack(char x, stack<BNode*> &s) {
if (root == NULL)
return;
BNode* lastPos = NULL;
s.push(root);
while (!s.empty()) {
while (s.top()->lChild != NULL)
{
s.push(s.top()->lChild);
//节点值为x的节点入栈后,停止该函数
if (s.top()->data == x)
return;
}
while (!s.empty())
{
if (lastPos == s.top()->rChild || s.top()->rChild == NULL)
{
lastPos = s.top();
s.pop();
}
else if (s.top() != NULL)
{
s.push(s.top()->rChild);
//查找到x时,停止该函数
if (s.top()->data == x)
return;
break;
}
}
}
}
复制代码
12.二叉树的最大宽度(节点最多的一层的节点数)
int WidthOfBTree() {
if (root == NULL)
return 0;
int maxWidth = 0;
int front, rear;
front = rear = -1;
int last = 0;
BNode* p = root;
BNode* a[100];
a[++rear] = p;
while (front < rear) {
p=a[++front];
if (p->lChild != NULL)
a[++rear] = p->lChild;
if (p->rChild != NULL)
a[++rear] = p->rChild;
if (front == last)
{
last = rear;
if (rear - front > maxWidth)
maxWidth = rear - front;
}
}
return maxWidth;
}
复制代码