目录
1、面试题一:单度结点删除
编写一个函数用于删除二叉树中的所有单度结点
要求:结点删除后,其唯一的子结点替代它的位置
递归的思想
这里应该考虑结点中是否包含指向父结点的指针(这里是否包含可以问面试官)
情况一: 结点中包含指向父结点的指针
- 定义功能: delOdd1(node)
功能: 删除 node 为根结点的二叉树中的单度结点
度为0或2的结点不需要删除只需递归的删除左子树单度结点和右子树单度结点
情况二:结点中只包含左右孩子指针
- 定义功能: delOdd2(node)
功能: 删除 node 为根结点的二叉树中的单度结点
编程实验
单度结点删除一 delOdd1
#include <iostream>
#include "BTreeNode.h"
using namespace std;
using namespace DTLib;
template < typename T >
BTreeNode<T>* createTree() // 创建的树,就是前面画的树
{
static BTreeNode<int> ns[9];
for(int i=0; i<9; i++)
{
ns[i].value = i;
ns[i].parent = NULL;
ns[i].left = NULL;
ns[i].right = NULL;
}
ns[0].left = &ns[1];
ns[0].right = &ns[2];
ns[1].parent = &ns[0];
ns[2].parent = &ns[0];
ns[1].left = &ns[3];
ns[1].right = NULL;
ns[3].parent = &ns[1];
ns[2].left = &ns[4];
ns[2].right = &ns[5];
ns[4].parent = &ns[2];
ns[5].parent = &ns[2];
ns[3].left = NULL;
ns[3].right = &ns[6];
ns[6].parent = &ns[3];
ns[4].left = &ns[7];
ns[4].right = NULL;
ns[7].parent = &ns[4];
ns[5].left = &ns[8];
ns[5].right = NULL;
ns[8].parent = &ns[5];
return ns;
}
template < typename T >
void printInOrder(BTreeNode<T>* node) // 中序遍历
{
if( node != NULL )
{
printInOrder(node->left);
cout << node->value << " ";
printInOrder(node->right);
}
}
template < typename T >
BTreeNode<T>* delOdd1(BTreeNode<T>* node)
{
BTreeNode<T>* ret = NULL;
if(node != NULL)
{
// 度为1
if(( node->left && !node->right ) ||
( !node->left && node->right ))
{
BTreeNode<T>* parent = dynamic_cast<BTreeNode<T>*>(node->parent);
BTreeNode<T>* node_child = (node->left != NULL) ? node->left : node->right; // 指向单度结点的唯一孩子
if( parent != NULL )
{
BTreeNode<T>*& parent_chlid = (parent->left == node) ? parent->left : parent->right; // 父结点中指向node的指针
parent_chlid = node_child;
node_child->parent = parent;
}
else // 根结点为单度
{
node_child->parent = NULL;
}
if( node->flag() )
{
delete node;
}
ret = delOdd1(node_child); // 持续删除单度结点
}
else // 度为0,2
{
delOdd1(node->left);
delOdd1(node->right);
ret = node;
}
}
return ret;
}
int main()
{
BTreeNode<int>* ns = createTree<int>();
printInOrder(ns);
cout << endl;
ns = delOdd1(ns);
printInOrder(ns);
cout << endl;
int a[] = {6, 7, 8};
for(int i=0; i<3; i++) // 打印叶结点到根结点路径
{
TreeNode<int>* n = ns + a[i]; // n代表叶节点,这里序号就是顺序
while(n != NULL)
{
cout << n->value << " ";
n = n->parent;
}
cout << endl;
}
return 0;
}
单度结点删除二 delOdd2
#include <iostream>
#include "BTreeNode.h"
using namespace std;
using namespace DTLib;
template < typename T >
BTreeNode<T>* createTree() //创建的树就是前,前面画的树
{
static BTreeNode<int> ns[9];
for(int i=0; i<9; i++)
{
ns[i].value = i;
ns[i].parent = NULL;
ns[i].left = NULL;
ns[i].right = NULL;
}
ns[0].left = &ns[1];
ns[0].right = &ns[2];
ns[1].parent = &ns[0];
ns[2].parent = &ns[0];
ns[1].left = &ns[3];
ns[1].right = NULL;
ns[3].parent = &ns[1];
ns[2].left = &ns[4];
ns[2].right = &ns[5];
ns[4].parent = &ns[2];
ns[5].parent = &ns[2];
ns[3].left = NULL;
ns[3].right = &ns[6];
ns[6].parent = &ns[3];
ns[4].left = &ns[7];
ns[4].right = NULL;
ns[7].parent = &ns[4];
ns[5].left = &ns[8];
ns[5].right = NULL;
ns[8].parent = &ns[5];
return ns;
}
template < typename T >
void printInOrder(BTreeNode<T>* node) // 中序遍历
{
if( node != NULL )
{
printInOrder(node->left);
cout << node->value <<" ";
printInOrder(node->right);
}
}
template < typename T >
void delOdd2(BTreeNode<T>*& node)
{
if(node != NULL)
{
if(( node->left && !node->right ) ||
( !node->left && node->right ))
{
BTreeNode<T>* node_child = (node->left != NULL) ? node->left : node->right; // 指向单度结点的唯一孩子
if(node->flag())
{
delete node;
}
node = node_child; // node是父结点左孩子指针或右孩子指针的引用
delOdd2(node);
}
else
{
delOdd2(node->left);
delOdd2(node->right);
}
}
}
int main()
{
BTreeNode<int>* ns = createTree<int>();
printInOrder(ns);
cout << endl;
delOdd2(ns);
printInOrder(ns);
cout << endl;
return 0;
}
2、面试题二:中序线索化二叉树
编写一个函数用于中序线索化二叉树
要求:不允许使用其它数据结构 (我们前面实现基于队列)
解法一:在中序遍历的同时进行线索化
- 思路:
● 使用辅助指针,在中序遍历时指向当前结点的前驱结点
● 访问当前结点时,连接与前驱结点的先后次序
- 定义功能: inOrderThread(node, pre)
● node: 根结点,也是中序访问的结点
● pre: 为中序遍历时的前驱结点指针
解法二:中序遍历的结点次序正好是结点的水平次序
- 思路:
● 使用辅助指针,指向转换后双向链表的头结点和尾结点
● 根结点与左右子树转换的双向链表连接,成为完整双向链表
- 定义功能: inOrderThread(node, head, tail)
● node: 根结点,也是中序访问的结点
● head: 转换成功后指向双向链表的首结点
● tail : 转换成功后指向双链表的尾结点
编程实验
中序线索化一 inOrderThread1
#include <iostream>
#include "BTreeNode.h"
using namespace std;
using namespace DTLib;
template < typename T >
BTreeNode<T>* createTree() // 创建的树就是前,前面画的树
{
static BTreeNode<int> ns[9];
for(int i=0; i<9; i++)
{
ns[i].value = i;
ns[i].parent = NULL;
ns[i].left = NULL;
ns[i].right = NULL;
}
ns[0].left = &ns[1];
ns[0].right = &ns[2];
ns[1].parent = &ns[0];
ns[2].parent = &ns[0];
ns[1].left = &ns[3];
ns[1].right = NULL;
ns[3].parent = &ns[1];
ns[2].left = &ns[4];
ns[2].right = &ns[5];
ns[4].parent = &ns[2];
ns[5].parent = &ns[2];
ns[3].left = NULL;
ns[3].right = &ns[6];
ns[6].parent = &ns[3];
ns[4].left = &ns[7];
ns[4].right = NULL;
ns[7].parent = &ns[4];
ns[5].left = &ns[8];
ns[5].right = NULL;
ns[8].parent = &ns[5];
return ns;
}
template < typename T >
void printInOrder(BTreeNode<T>* node) // 中序遍历
{
if( node != NULL )
{
printInOrder(node->left);
cout << node->value <<" ";
printInOrder(node->right);
}
}
template < typename T >
void printDualList(BTreeNode<T>* node)
{
BTreeNode<T>* g = node;
cout << "head -> tail: " << endl;
while( node != NULL )
{
cout << node->value << " ";
g = node;
node = node->right;
}
cout << endl;
cout << "tail -> head: " << endl;
while( g != NULL )
{
cout << g->value << " ";
g = g->left;
}
cout << endl;
}
template < typename T >
void inOrderThread(BTreeNode<T>* node, BTreeNode<T>*& pre)
{
if(node != NULL)
{
inOrderThread(node->left, pre); // 左子树为双向链表,pre指向最后一个结点
node->left = pre;
if(pre != NULL)
{
pre->right = node;
}
pre = node;
inOrderThread(node->right, pre);
}
}
template < typename T >
BTreeNode<T>* inOrderThread1(BTreeNode<T>* node)
{
BTreeNode<T>* pre = NULL;
inOrderThread(node, pre);
while( (node != NULL) && (node->left != NULL) )
{
node = node->left;
}
return node;
}
int main()
{
BTreeNode<int>* ns = createTree<int>();
printInOrder(ns);
cout << endl;
delOdd2(ns);
printInOrder(ns);
cout<<endl;
ns = inOrderThread1(ns);
printDualList(ns);
return 0;
}
中序线索化二 inOrderThread2
#include <iostream>
#include "BTreeNode.h"
using namespace std;
using namespace DTLib;
template < typename T >
BTreeNode<T>* createTree() // 创建的树就是前,前面画的树
{
static BTreeNode<int> ns[9];
for(int i=0; i<9; i++)
{
ns[i].value = i;
ns[i].parent = NULL;
ns[i].left = NULL;
ns[i].right = NULL;
}
ns[0].left = &ns[1];
ns[0].right = &ns[2];
ns[1].parent = &ns[0];
ns[2].parent = &ns[0];
ns[1].left = &ns[3];
ns[1].right = NULL;
ns[3].parent = &ns[1];
ns[2].left = &ns[4];
ns[2].right = &ns[5];
ns[4].parent = &ns[2];
ns[5].parent = &ns[2];
ns[3].left = NULL;
ns[3].right = &ns[6];
ns[6].parent = &ns[3];
ns[4].left = &ns[7];
ns[4].right = NULL;
ns[7].parent = &ns[4];
ns[5].left = &ns[8];
ns[5].right = NULL;
ns[8].parent = &ns[5];
return ns;
}
template < typename T >
void printInOrder(BTreeNode<T>* node) // 中序遍历
{
if( node != NULL )
{
printInOrder(node->left);
cout << node->value <<" ";
printInOrder(node->right);
}
}
template < typename T >
void printDualList(BTreeNode<T>* node)
{
BTreeNode<T>* g = node;
cout << "head -> tail: " << endl;
while( node != NULL )
{
cout << node->value << " ";
g = node;
node = node->right;
}
cout << endl;
cout << "tail -> head: " << endl;
while( g != NULL )
{
cout << g->value << " ";
g = g->left;
}
cout << endl;
}
template < typename T >
void inOrderThread(BTreeNode<T>* node,BTreeNode<T>*& head,BTreeNode<T>*& tail)
{
if(node != NULL)
{
BTreeNode<T>* h = NULL;
BTreeNode<T>* t = NULL;
inOrderThread(node->left, h, t);
node->left = t;
if(t != NULL)
{
t->right = node;
}
head = (h != NULL) ? h : node;
h = NULL;
t = NULL;
inOrderThread(node->right, h, t);
node->right = h;
if(h != NULL)
{
h->left = node;
}
tail = (t != NULL) ? t : node;
}
}
template < typename T >
BTreeNode<T>* inOrderThread2(BTreeNode<T>* node)
{
BTreeNode<T>* head = NULL;
BTreeNode<T>* tail = NULL;
inOrderThread(node, head, tail);
return head;
}
int main()
{
BTreeNode<int>* ns = createTree<int>();
printInOrder(ns);
cout << endl;
ns = inOrderThread2(ns);
printDualList(ns);
return 0;
}