设t为一颗二叉树的根节点地址指针,设计一个非递归的算法把二叉树中每个节点的左右孩子位置交换
void Exchange(BinTreeNode* t){
stack<BinTreeNode*> s;
BinTreeNode* p;
if(T == NULL){
return ;
}
s.push(t);
while(!s.empty()){
t = s.top();
s.pop();
p = t->lchild;
t->lchild = t->rchild;
t->rchild = p;
if(t->lchild){
s.push(t->lchild);
}
if(t->rchild){
s.push(t->rchild);
}
}
}
设t是一颗按后序遍历方式构成的线索二叉树的根节点指针,设计一个非递归的算法,把一个地址为x的新节点插到t树中,已知地址为y的节点有则作为节点y的右孩子,并把插入后的二叉树仍为后续线索二叉树
一颗高度k具有n个节点的二叉树,按顺序方式存储;
(1)编写用先跟遍历树种每个节点的递归算法
(2)编写将树种最大序号叶子节点的祖先节点节点全部打印输出的算法
一颗高度为k且有n个节点的二叉排序树,同时又是一颗完全二叉树存于向量t中,设计删除树中序号为i且具有左右孩子的一个节点,而不使存储量增加 保证仍为二叉排序树(不一定是完全二叉树)
编写算法判断二叉树是否为平衡二叉树
int Tree_Depth(BinTreeNode* t){//求t节点的深度
if(t == NULL){
return 0;
}else{
int left_Depth = Tree_Depth(t->left);
int right_Depth = Tree_Depth(t->right);
return 1+(left_Depth > right_Depth ? left_Depth : right_Depth);
}
}
bool Is_Balance_Tree(BinTreeNode* t){//判断是否是平衡二叉树
if(t == NULL){
return true;
}
int left_Depth = Tree_Depth(t->left);//左子树深度
int right_Depth = Tree_Depth(t->right);//右子树深度
if(abs(left_Depth - right_Depth) > 1){
return false;
}else{
return Is_Balance_Tree(t->left) && Is_Balance_Tree(t->right);
}
}
编写一个算法,利用叶子节点中的空指针域将所有叶子节点链接为一个带头节点的双链表,算法返回头结点的地址
BinTreeNode* head;//链表头节点
BinTreeNode* pre;//链表当前节点的前一个节点
void Creat_list(BinTreeNode* t){
if(t){
Creat_list(t->lchild);//不一定用中序遍历,因为题目没有要求叶子节点要按照什么样的顺序,因此只要遍历到所有的叶子节点就可以。
if(t->lchild == NULL && t->right == NULL){
if(head == NULL){//两种情况:头节点是否为空
head = new BinTreeNode;
head->lchild = NULL;
head->rchild = t;
t->lchild = head;
pre = t;
}else{
pre->rchild = t;
t->lchild = pre;
pre = t;
}
}
Creat_list(t->rchild);
pre->rchild = NULL;
}
}
已知一个二叉树的中序序列和后序序列,写一个建立该二叉树的二叉链表存储结构的算法
char inorder_str[] = {};//中序序列
char postorder_str[] = {};//后序序列
int Find_Position(char c){//查找字符c在中序序列中的位置
int i = 0;
for(i = 0;i < inorder_str.lenght();i++){
if(inorder_str[i] == c)
break;
}
return i;
}
void Creat_BinTree(BinTreeNode t,int l1,int h1,int l2,int h2){
//l1,h1中序序列的开始和结束位置;l2,h2后序序列的开始和结束位置
int position = 0
t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
t->data = postorder_str[h2];
t->lchild = NULL;
t->rchild = NULL;
if(l1 < h1){
position = Find_Position(postorder_str[e2]);
if(position != l1){//position == l1 时说明该节点没有左子树
Creat_BinTree(t->lchild,l1,positon-1,l2,l2+(position-1-l1));
}
if(position != h1){//position == h1 时说明该节点没有右子树
Creat_BinTree(t->rchild,position+1,h1,(h2-1)-(h1-position-1),h2-1);
}
}
}
写出删除二叉排序树bt中值为x的节点算法(二叉排序树以二叉链表形式存储,删除后仍然保持二叉排序树性质)
void Delete(BSTree *bst,int x){
BSTree* f,*p = bst;
while(p && p->data != x){
if(p->data > x){
f = p;
p = p->lchild;
}else{
f = p;
p = p->rchild;
}
}
if(p == NULL){
return ;
}
if(p->lchild == NULL){//被删子树没有左子树
if(f->lchild == p){
f->lchild = p->rchild;
}else{
f->rchild = p->rchild;
}
}else if(p->rchild == NULL){//被删子树没有右子树
if(f->lchild == p){
f->lchild = p->lchild;
}else{
f->rchild = p->lchild;
}
}else{
BSTree* q = p->lchild;
BSTree* s = q;
while(q->rchild != NULL){
s = q;
q = q->rchild;
}
if(s == p->lchild){//p左子树的根节点没有右子树
p->data = s->data;
p->lchild = s->lchild;
free(s);
}else{//p左子树的根节点有右子树
p->data = q->data;
s->rchild = q->lchild;
free(q);
}
}
}
设t是一颗满二叉树,编写一个将t的线序遍历序列转换为后序遍历序列的递归算法
写出后序线索二叉树的非递归遍历算法
编写一个递归函数,判断两颗二叉树是否相似。如果两个二叉树s和t相似,那么或者他们的左右子树都相似,或者s和t都为空
bool Is_like(BinTreeNode* t1,BinTreeNode* t2){
if(t1 == NULL && t2 == NULL){//分为三种情况
return true;
}else if(t1 == NULL || t2 == NULL){
return false;
}else{
return Is_like(t1->lchild,t2->rchild) && Is_like(t2->lchild,l2->rchild);
}
}
对二叉链表存储的非空二叉树,从右向左依次释放所有叶子节点,释放的同时,把节点值存放到一个向量中
该二叉排序树已经以二叉链表的形式存储在内存中,使用递归算法,求每个节点的平衡因子并输出
int Tree_Depth(BinTreeNode* t){//求t节点的深度
if(t == NULL){
return 0;
}else{
int left_Depth = Tree_Depth(t->left);
int right_Depth = Tree_Depth(t->right);
return 1+(left_Depth > right_Depth ? left_Depth : right_Depth);
}
}
void printf_num(BinTreeNode* t){
if(t != NULL){
printf("节点%d的平衡因子为%d\n",t->value,Tree_Depth(t->left)-Tree_Depth(t->right));
printf_num(t->left);
printf_num(t->right);
}
}
给出中序线索二叉树的节点结构,试编写在不使用栈和递归的情况下先序遍历中序线索二叉树的算法
设二叉树中节点的数据域的值互不相同,设计一个算法将数据域值为x的节点的所有祖先节点的数据域打印出来
bool Is_Ancestor(BinTreeNode* t,int x){
if(t == NULL){
return false;
}
if(t->value == x){
return true;
}
if(Is_Ancestor(t->lchild,x)||Is_Ancestor(t->rchild,x){
printf("%d",t->value);
return true;
)
}
设二叉树二叉链表为存储结构,编写计算二叉树中所有节点的平衡因子,同时返回二叉树中非平衡节点个数的算法
int Tree_Depth(BinTreeNode* t){//求t节点的深度
if(t == NULL){
return 0;
}else{
int left_Depth = Tree_Depth(t->left);
int right_Depth = Tree_Depth(t->right);
return 1+(left_Depth > right_Depth ? left_Depth : right_Depth);
}
}
int printf_num(BinTreeNode* t){
stack<BinTreeNode*> s;
if(t == NULL){
return 0;
}
int num = 0;
int temp = 0;
s.push(t);
BinTreeNode* p;
while(!s.empty()){
p = s.top();
s.pop();
if(p != NULL){
temp = p->value,Tree_Depth(p->left) - Tree_Depth(p->right);
printf("节点%d的平衡因子为%d",p->value,temp);
if(temp != 0){
num++;
}
s.push(p->left);
s.push(p->right);
}
}
return num;
}
设有n个节点的平衡二叉树的每个节点都标明了平衡因子b,设计节点存储结构,并编写求平衡二叉树的高度的算法(要求算法的时间复杂度为o(log2(n)))
一直一颗二叉树的前序序列和中序序列分别存于两个一维数组中,编写算法建立该二叉树的二叉链表
类似上面的知中序和后序
假设一个仅包含二元运算符的算术表达式以二叉链表形式存储在二叉树t中,编写按后序序列遍历计算表达式的值
二叉树采用二叉链表作为存储结构。编写算法,求出二叉树中第i层和第i+1层叶子节点个数之和
以二叉链表为存储结构,编写算法实现如下功能,在二叉树中查找值为x的节点,并求连续节点在书中的层数