编写复制一颗二叉树的非递归算法
2013-11-28 10:39
309 查看
问题描述:设栈的类型为seqstack,initstack(s)为对栈s初始化。
基本思路:用两个栈保存左右树访问的节点,每次访问根后PUSH一下,然后pop出继续保存左右子树。
// CopyBinaryTrees.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
using namespace std;
typedef char element;
typedef struct bitree
{
element data;
struct bitree* lchild;
struct bitree* rchild;
}bitree;
typedef struct node
{
bitree* data;
struct node *next;
}node,*pnode;
class seqstack
{
private:
node* top;//当前位置,头结点
public:
void initstack();
void push(bitree* bt);
BOOL pop(bitree* &bt);
BOOL isempty();//栈是否为空,空返回true,非空返回false
};
void seqstack::initstack()
{
top = NULL;
}
void seqstack::push(bitree* bt)
{
node* p = (node*)malloc(sizeof(node));
p->data = bt;
p->next = top;
top = p;
}
BOOL seqstack::pop(bitree* &bt)
{
node* p;
if( isempty()) // 堆栈为空,删除失败,返回0
return FALSE;
else{
p = top;
bt = p->data; // 将栈顶元素数据域送入item中
p->next = NULL;
top = top->next; // 修改栈顶指针
free( p ); // 释放被删除结点
return TRUE; // 操作成功,返回1
}
}
BOOL seqstack::isempty()
{
if (top == NULL)
return true;
else
return false;
}
void CreateBiTree(bitree* &T)
{
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else
{
T=(bitree *)malloc(sizeof(bitree));
T->data=ch;
T->lchild = NULL;
T->rchild = NULL;
cout<<"put in \'"<<T->data<<"\' lchild"<<endl;
CreateBiTree(T->lchild);
cout<<"put in \'"<<T->data<<"\' rchild"<<endl;
CreateBiTree(T->rchild);
}
}//CreateBiTree
bitree* copyBinaryTree(bitree *bSrc)
{
seqstack stack_left,stack_right;
bitree* newbt;
if (bSrc!=NULL)
{
//new root
newbt=new bitree;
newbt->data=bSrc->data;
newbt->lchild = NULL;
newbt->rchild = NULL;
//travel bt and travel newbt at the same time
stack_left.push(bSrc);
stack_right.push(newbt);
while (!stack_left.isempty())
{
bitree* pleft;
stack_left.pop(pleft);
bitree* pright;
stack_right.pop(pright);
if (pleft->rchild!=0)
{
stack_left.push(pleft->rchild);
pright->rchild=new bitree;
pright->rchild->data=pleft->rchild->data;
pright->rchild->lchild = NULL;
pright->rchild->rchild = NULL;
stack_right.push(pright->rchild);
}
if (pleft->lchild!=0)
{
stack_left.push(pleft->lchild);
pright->lchild=new bitree;
pright->lchild->data=pleft->lchild->data;
pright->lchild->lchild = NULL;
pright->lchild->rchild = NULL;
stack_right.push(pright->lchild);
}
}
}
return newbt;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("enter # is over,put in root node\n");
bitree *b1 = (bitree *)malloc(sizeof(bitree));
CreateBiTree(b1);
bitree* b2 = copyBinaryTree(b1);
return 0;
}
基本思路:用两个栈保存左右树访问的节点,每次访问根后PUSH一下,然后pop出继续保存左右子树。
// CopyBinaryTrees.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
using namespace std;
typedef char element;
typedef struct bitree
{
element data;
struct bitree* lchild;
struct bitree* rchild;
}bitree;
typedef struct node
{
bitree* data;
struct node *next;
}node,*pnode;
class seqstack
{
private:
node* top;//当前位置,头结点
public:
void initstack();
void push(bitree* bt);
BOOL pop(bitree* &bt);
BOOL isempty();//栈是否为空,空返回true,非空返回false
};
void seqstack::initstack()
{
top = NULL;
}
void seqstack::push(bitree* bt)
{
node* p = (node*)malloc(sizeof(node));
p->data = bt;
p->next = top;
top = p;
}
BOOL seqstack::pop(bitree* &bt)
{
node* p;
if( isempty()) // 堆栈为空,删除失败,返回0
return FALSE;
else{
p = top;
bt = p->data; // 将栈顶元素数据域送入item中
p->next = NULL;
top = top->next; // 修改栈顶指针
free( p ); // 释放被删除结点
return TRUE; // 操作成功,返回1
}
}
BOOL seqstack::isempty()
{
if (top == NULL)
return true;
else
return false;
}
void CreateBiTree(bitree* &T)
{
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else
{
T=(bitree *)malloc(sizeof(bitree));
T->data=ch;
T->lchild = NULL;
T->rchild = NULL;
cout<<"put in \'"<<T->data<<"\' lchild"<<endl;
CreateBiTree(T->lchild);
cout<<"put in \'"<<T->data<<"\' rchild"<<endl;
CreateBiTree(T->rchild);
}
}//CreateBiTree
bitree* copyBinaryTree(bitree *bSrc)
{
seqstack stack_left,stack_right;
bitree* newbt;
if (bSrc!=NULL)
{
//new root
newbt=new bitree;
newbt->data=bSrc->data;
newbt->lchild = NULL;
newbt->rchild = NULL;
//travel bt and travel newbt at the same time
stack_left.push(bSrc);
stack_right.push(newbt);
while (!stack_left.isempty())
{
bitree* pleft;
stack_left.pop(pleft);
bitree* pright;
stack_right.pop(pright);
if (pleft->rchild!=0)
{
stack_left.push(pleft->rchild);
pright->rchild=new bitree;
pright->rchild->data=pleft->rchild->data;
pright->rchild->lchild = NULL;
pright->rchild->rchild = NULL;
stack_right.push(pright->rchild);
}
if (pleft->lchild!=0)
{
stack_left.push(pleft->lchild);
pright->lchild=new bitree;
pright->lchild->data=pleft->lchild->data;
pright->lchild->lchild = NULL;
pright->lchild->rchild = NULL;
stack_right.push(pright->lchild);
}
}
}
return newbt;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("enter # is over,put in root node\n");
bitree *b1 = (bitree *)malloc(sizeof(bitree));
CreateBiTree(b1);
bitree* b2 = copyBinaryTree(b1);
return 0;
}
相关文章推荐
- 编写一个递归算法,实现将一颗二叉树的左右孩子互换。
- 试编写一个函数,返回一颗给定二叉树在中序遍历下的最后一个节点(分别用递归和非递归实现)
- 复制一棵二叉树的非递归算法
- 假设二叉树采用链接方式存储,编写一个计算一颗二叉树t的高度的函数
- 数据结构-----二叉树的中序遍历的非递归算法实现
- 编写一个程序,将字符数组s2中的全部字符复制到s1中。即实现strcpy函数功能
- 配合异步复制,编写数据库批量导出导入脚本
- 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径
- 《苦练算法》-剑指Offer- 二十二、从上往下打印二叉树 -python编写
- 一颗普通的二叉树,如何寻找两个节点的最低公共祖先(发现的一个与算法无关的引用问题)
- 6.36③ 若已知两棵二叉树B1和B2皆为空,或者皆 不空且B1的左、右子树和B2的左、右子树分别相似, 则称二叉树B1和B2相似。试编写算法,判别给定两 棵二叉树是否相似。
- 二叉树三种遍历的非递归算法
- java编写二叉树以及前序遍历、中序遍历和后序遍历 .
- 二叉树前序遍历的递归与非递归算法
- 编写一个类,该类具有统计文件长度,实现文件复制的函数。 其中复制功能函数如: void copy(string srcFileName,string targetFileName);
- 求二叉树的高度/销毁一颗二叉树【递归思想】
- 编写一个应用程序实现文件的复制。使用格式:java Copy 源文件目标文件,功能是将源文件的内容复制到目标文件。
- 练习 3-2 编写一个函数escape(s, t),将字符串t复制到字符串s中,并在复制过程中将换行符、制表符等不可见字符分别转换为\n、\t等相应的可见的转义字符序列。要求使用swich语句。
- 8-31 编写函数求二叉树中叶子结点的个数
- 求二叉树的高度/销毁一颗二叉树-->Destroy(Node* root)