二叉树的创建及简单操作

来源:互联网 发布:中国传媒大学南广 知乎 编辑:程序博客网 时间:2024/05/16 07:22
#pragma once
#include<vector>
#include<assert.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef int DataType;
struct BinaryNode
{
DataType _data;
BinaryNode *_left;
BinaryNode *_right;


BinaryNode(DataType x) :_left(NULL), _right(NULL), _data(x)
{}
};
void CreatBinaryTree(BinaryNode *&root, const vector<DataType>& datas, int &i)//构造树遇见#为空格,两个#向上回溯。
{
if (root == NULL&&i < datas.size() && datas[i] != '#')
{
root = new BinaryNode(datas[i]);
CreatBinaryTree(root->_left, datas, ++i);
CreatBinaryTree(root->_right, datas, ++i);
}




}
BinaryNode * Find(BinaryNode *root, int x)//查找
{


if (root)
{
if (root->_data == x)
{
return root;
}
BinaryNode *ret = NULL;
if (ret = Find(root->_left, x))
{
return ret;
}
else if (ret = Find(root->_right, x))
{
return ret;
}
}
return NULL;
}




void PreyTraverse(BinaryNode *root)
{
if (root)
{
cout << root->_data << " ";
PreyTraverse(root->_left);
PreyTraverse(root->_right);
}


}
void MidTraverse(BinaryNode *root)
{
if (root)
{
PreyTraverse(root->_left);
cout << root->_data << " ";
PreyTraverse(root->_right);
}


}
void BackTraverse(BinaryNode *root)//后序
{
if (root)
{
PreyTraverse(root->_left);
PreyTraverse(root->_right);
cout << root->_data << " ";
}


}
//层次遍历思想:先把队列中放一个root,然后放入左右子书,然后把root出去,然后找到左子树出去放入左子树的左右子树。


void LevelTraverse(BinaryNode *root)
{
if (root == NULL)
{
return;
}
queue<BinaryNode*> q;
q.push(root);
while (!q.empty())
{
BinaryNode *node = q.front();
cout << q.front()->_data << " ";
if (node->_left)
{
q.push(node->_left);
}
if (node->_right)
{
q.push(node->_right);
}
q.pop();
}


}
int GetHeight(BinaryNode *root)//求树的高度,递归的思想,根节点的高度等于他的左子树的高度家一。
{
if (root == NULL || (root->_left == NULL) && (root->_right == NULL))
{
return 0;
}
int lefthight = GetHeight(root->_left);
int righthight = GetHeight(root->_right);
return lefthight > righthight ? lefthight + 1 : righthight + 1;
}
int GetleafNum(BinaryNode *root)//叶子节点;
{
if (root == NULL)
{
return 0;
}
if (root->_left == NULL && root->_right == NULL)
{
return 1;
}
return GetleafNum(root->_left) + GetleafNum(root->_right);
}


int getklevenum(BinaryNode *root, int k)//k层的节点个数
{
if (k < 1 || root == NULL)
{
return 0;
}
if (k == 1)
{
return 1;
}
return getklevenum(root->_left, k - 1) + getklevenum(root->_right, k - 1);




}
bool IsIntree(BinaryNode *root, BinaryNode *n)//判断一个节点是否在树中   
{
assert(n);
if (root == n)
{
return true;
}
else
{
if (IsIntree(root->_left, n))
{


return true;
}
if (IsIntree(root->_right, n))
{


return true;
}
}
return false;


}
BinaryNode * GetCommenan(BinaryNode *root, BinaryNode *x1, BinaryNode *x2)
{ //得到最近公共祖先,采用递归的思想,
assert(x1);
assert(x2);
if (root == NULL)
{
return NULL;
}
if (root == x1&&IsIntree(root,x2))
{
return root;


}
if (root == x2&&IsIntree(root, x1))
{
return root;


}
bool x1Inleft = false,
x1Inright = false,
x2Inleft = false,
x2Inright = false;
x1Inleft = IsIntree(root->_left, x1);
x2Inright = IsIntree(root->_right, x2);
if (x1Inleft&&x2Inright)
{
return root;
}
x1Inright = IsIntree(root->_right, x1);
x2Inleft = IsIntree(root->_left, x2);
if (x1Inright&&x2Inleft)
{
return root;
}
if (x1Inleft&&x2Inleft)
{
return GetCommenan(root->_left,x1,x2);
}
if (x1Inright&&x2Inright)
{
return GetCommenan(root->_right,x1,x2);
}



}


bool GetNodePath(BinaryNode *root, stack<BinaryNode*> &s, BinaryNode *x)
{
if (root)
{
s.push(root);
if (root == x)
{
return true;
}
else
{
if (GetNodePath(root->_left, s, x))

{

return true;
}
if (GetNodePath(root->_right, s, x))
{
return true;
}
s.pop();


}
}


if (root == NULL)
{
return false;
}


}

bool IsBanlancetree(BinaryNode *root)
{
if (root == NULL||root->_left==NULL&&root->_right==NULL)
{
return true;
}
int lefthight = GetHeight(root->_left + 1);
int righthight = GetHeight(root->_right + 1);
return abs(lefthight - righthight) <= 1
&& IsBanlancetree(root->_left)
&& IsBanlancetree(root->_right);
}



#include "test.h"


void Test1()
{
// 123##4##56
vector<DataType> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back('#');
v.push_back('#');
v.push_back(4);
v.push_back('#');
v.push_back('#');
v.push_back(5);
v.push_back(6);
BinaryNode* root = NULL;
int index = 0;
CreatBinaryTree(root, v, index);
GetleafNum(root);
GetHeight(root);


PreyTraverse(root);
cout << endl;


MidTraverse(root);
cout << endl;


BackTraverse(root);
cout << endl;
LevelTraverse(root);


}


int main()
{
Test1();
return 0;
}


0 0
原创粉丝点击