节点定义:
typedef struct Node
{
int data;
Node* rChild;
Node* lChild;
} *BinTree;
创建2叉树:
int array[31]={1,2,4,8,0,0,9,0,0,5,10,11,0,0,0,0,3,6,12,0,0,13,0,0,7,14,0,0,15,0,0};
void CreateBinTree(BinTree* T)
{
// 构造二叉链表。 T 是指向根指针的指针,故修改 *T 就修改了实参 ( 根指针 ) 本身
int ch;
static int i=0;
ch=array[i++];
//cout<<ch<<endl;
//cin>>ch;
if(ch==0)
T=NULL ;// 读人空格,将相应指针置空
else{ // 读人非空格
*T=(BinTree)malloc(sizeof(Node)); // 生成结点
(*T)->data=ch ;
CreateBinTree(&(*T)->lChild) ; // 构造左子树
CreateBinTree(&(*T)->rChild) ; // 构造右子树
}
}
前序遍历2叉树:
//前序遍历
void FirstRec(Node* root)
{
Node* pNode=root;
stack<Node*> s;
while (pNode!=NULL||s.size()!=0) {
while (pNode!=NULL) {
cout<<pNode->data<<endl;
s.push(pNode);
pNode=pNode->lChild;
}
if(s.size()!=0){
//cout<<"s.szie()==="<<s.size()<<endl;
pNode=s.top();
s.pop();
pNode=pNode->rChild;
}
}
}
中序遍历2叉树与前序类似,这里就不赘述了。
层序遍历2叉树:
void PrintNodeByLevel(Node* root)
{
stack< vector<Node*>* > stac;
vector<Node*> *pre=new vector<Node*>;
pre=NULL;
vector<Node*> *cur=new vector<Node*>;
cur->push_back(root);
stac.push(cur);
while (pre!=cur) {
pre=cur;
vector<Node*>::iterator iter=pre->begin();
vector<Node*> *newLevel=new vector<Node*>;
while (iter!=pre->end()) {
if((*iter)->lChild)
//newLevel->push_back((*iter)->lChild);//在vector的末尾添加元素
newLevel->push_back((*iter)->lChild);//在vector的首部添加元素
if((*iter)->rChild)
newLevel->push_back((*iter)->rChild);
iter++;
}
if(newLevel->size()>=1){
stac.push(newLevel);
cur=newLevel;
}
}
while (stac.size()!=0) {
cur=stac.top();
stac.pop();
vector<Node*>::iterator iter=cur->begin();
while (iter!=cur->end()) {
cout<<(*iter)->data<<"\t";
iter++;
}
cout<<endl;
}
deque <Node*> *pre_1=new deque<Node*>;
pre_1=NULL;
deque<Node*> *cur_1=new deque<Node*>;
stack<deque<Node*>*> stac_1;
cur_1->push_front(root);
stac_1.push(cur_1);
while (pre_1!=cur_1) {
pre_1=cur_1;
deque<Node*>::iterator iter=pre_1->begin();
deque<Node*> *newLevel=new deque<Node*>;
while (iter!=pre_1->end()) {
if((*iter)->lChild)
newLevel->push_front((*iter)->lChild);//在deque的首部添加元素
if((*iter)->rChild)
newLevel->push_front((*iter)->rChild);
iter++;
}
if(newLevel->size()>=1){
stac_1.push(newLevel);
cur_1=newLevel;
}
}
while (stac_1.size()!=0) {
cur_1=stac_1.top();
stac_1.pop();
deque<Node*>::iterator iter=cur_1->begin();
while (iter!=cur_1->end()) {
cout<<(*iter)->data<<"\t";
iter++;
}
cout<<endl;
}
}
在二叉树中查找某个节点:
1.输入参数为根结点和搜索节点:
bool searchNode(Node* parent,Node* searchedNode)
{
if(parent||searchedNode)
return false;
if(parent->data==searchedNode->data)
return true;
else
return (searchNode(parent->lChild, searchedNode)||searchNode(parent->rChild,searchedNode));
}
2.输入参数为根结点和搜索节点对应的值:
bool searchNode(Node* parent,int searchedNode)
{
if(parent==NULL)
return false;
if(parent->data==searchedNode)
return true;
else
return (searchNode(parent->lChild, searchedNode)||searchNode(parent->rChild,searchedNode));
}
在2叉树中查找节点的最近的祖先:
1.输入参数为根结点和查找的两个节点:
Node* findLowestParent(Node* root,Node* no1,Node* no2)
{
if(root==NULL||no1==NULL||no2==NULL)
return NULL;
stack<Node*> sta;
Node* pNode=root;
int flag=0;
while (pNode!=NULL||sta.size()!=0) {
while (pNode!=NULL) {
sta.push(pNode);
if(pNode->data==no1->data){
flag=1;
break;
}
if(pNode->data==no1->data){
flag=2;
break;
}
pNode=pNode->lChild;
}
if(flag){
while (sta.size()!=0) {
Node* temp=sta.top();
if(temp==pNode){
if(flag==1)
if (searchNode(temp, no2)) {
return temp;
}
else
if(searchNode(temp, no1))
return temp;
}
else{
if(flag==1)
if (searchNode(temp->rChild, no2)) {
return temp;
}
else
if(searchNode(temp->rChild, no1))
return temp;
}
sta.pop();
}
}
else{
if(sta.size()!=0){
pNode=sta.top();
sta.pop();
pNode=pNode->rChild;
}
}
}
}
2.输入参数为根结点和两个查找节点对应的值:
Node* findLowestParent(Node* root,int no1,int no2)
{
if(root==NULL||no1==NULL||no2==NULL)
return NULL;
stack<Node*> sta;
Node* pNode=root;
int flag=0;
while (pNode!=NULL||sta.size()!=0) {
while (pNode!=NULL) {
sta.push(pNode);
if(pNode->data==no1){
flag=1;
break;
}
if(pNode->data==no2){
flag=2;
break;
}
pNode=pNode->lChild;
}
if(flag){
while (sta.size()!=0) {
Node* temp=sta.top();
if(temp==pNode){
if(flag==1){
if (searchNode(temp, no2))
return temp;
}
else{
if(searchNode(temp, no1))
return temp;
}
}
else{
if(flag==1){
if (searchNode(temp->rChild, no2))
return temp;
}
else
if(searchNode(temp->rChild, no1))
return temp;
}
sta.pop();
}
return NULL;
}
else{
if(sta.size()!=0){
pNode=sta.top();
sta.pop();
pNode=pNode->rChild;
}
}
}
}
3.输入为树根和两个节点对应的值:
bool HasNode(Node* root,list<Node*>&listNode,const int &num)
{
if(root==NULL)
return false;
cout<<"num="<<num<<"node.data="<<root->data<<endl;
listNode.push_back(root);
if(root->data==num)
return true;
if(!HasNode(root->lChild,listNode,num)){
if(!HasNode(root->rChild, listNode,num)){
listNode.pop_back();
return false;
}
else
return true;
}
else
return true;
}
Node* FindfirstDiffNode(list<Node*> *list1,list<Node*>*list2)
{
list<Node*>::iterator it1=list1->begin();
list<Node*>::iterator it2=list2->begin();
while (it2!=list2->end()&&it1!=list1->end()) {
if(*it1!=*it2){
it1--;
return *(it1);
}
it1++;
it2++;
}
if (it1==list1->end()) {
it1--;
return *it1;
}
else{
it2--;
return *it2;
}
}
Node* FindfirstDiffNode(list<Node*>&list1,list<Node*>&list2)
{
list<Node*>::iterator it1=list1.begin();
list<Node*>::iterator it2=list2.begin();
Node* plastCom;
while (it2!=list2.end()&&it1!=list1.end()) {
if(*it1!=*it2){
it1--;
return *(it1);
}
it1++;
it2++;
}
if (it1==list1.end()) {
it1--;
return *it1;
}
else{
it2--;
return *it2;
}
}
Node* commonParent(const BinTree &root,const int &num1,const int &num2)
{
if(root==NULL)
return NULL;
list<Node*> listofNum1,listofNum2;
bool found1=HasNode(root, listofNum1, num1);
bool found2=HasNode(root, listofNum2, num2);
if(found1&&found2)
return FindfirstDiffNode(listofNum1,listofNum2);
return NULL;
}