using namespace std;
struct BinaryTree
{
int value;
BinaryTree *left;
BinaryTree *right;
};
typedef BinaryTree* BinaryTreePointer;
BinaryTreePointer Root = nullptr;
void insert(int & item)
{
BinaryTreePointer locptr = Root,
parent = nullptr;
bool found = false;
while (!found && locptr != nullptr)
{
parent = locptr;
if (item < locptr->value)
locptr = locptr->left;
else if (item > locptr->value)
locptr = locptr->right;
else
found = true;
}
if (!found)
{
locptr = new BinaryTree();
locptr->value = item;
if (parent == nullptr)
Root = locptr;
else if (item < parent->value)
parent->left = locptr;
else
parent->right = locptr;
}
else
cout << "%d already in trn tree\n", item;
}
//中序遍历
void readAux(ostream &out, BinaryTreePointer root)
{
if (root != nullptr)
{
readAux(out, root->left);
cout << root->value << " ";
readAux(out, root->right);
}
}
void read(ostream &out)
{
readAux(out, Root);
}
//二叉树的镜像
void MirrorRecursively(BinaryTreePointer pNode)
{
if (pNode == nullptr)
return;
if (pNode->left == nullptr && pNode->right == nullptr)
return;
BinaryTreePointer pTemp = pNode->left;
pNode->left = pNode->right;
pNode->right = pTemp;
if (pNode->left)
MirrorRecursively(pNode->left);
if(pNode->right)
MirrorRecursively(pNode->right);
}
//从上到下打印二叉树1用deque
void PrintFromTopToBottom1(BinaryTreePointer pTreeRoot)
{
if (!pTreeRoot)
return;
deque< BinaryTreePointer>dequeTreeNode;
dequeTreeNode.push_back(pTreeRoot);
while (dequeTreeNode.size())
{
BinaryTreePointer pNode = dequeTreeNode.front();
dequeTreeNode.pop_front();
cout << " " << pNode->value;
if (pNode->left)
dequeTreeNode.push_back(pNode->left);
if(pNode->right)
dequeTreeNode.push_back(pNode->right);
}
}
//从上到下打印二叉树1用queue
void PrintFromTopToBottom2(BinaryTreePointer Root)
{
if (Root == nullptr)
return;
queue< BinaryTreePointer>nodes;
nodes.push(Root);
int nextLevel = 0,
tobeCout = 1;
while (!nodes.empty())
{
BinaryTreePointer pNode = nodes.front();
cout <<" " <<pNode->value;
if (pNode->left != nullptr)
{
nodes.push(pNode->left);
++nextLevel;
}
if (pNode->right != nullptr)
{
nodes.push(pNode->right);
++nextLevel;
}
nodes.pop();
--tobeCout;
if (tobeCout == 0)
{
cout << endl;
tobeCout = nextLevel;
nextLevel = 0;
}
}
}
//把二叉树按照中序遍历的顺序转化成双向二叉树
void ConvertNode(BinaryTreePointer pNode, BinaryTreePointer *pLastNodeInlist)
{
if (pNode == nullptr)
return;
BinaryTreePointer pCurrent = pNode;
if (pCurrent->left != nullptr)
ConvertNode(pCurrent->left, pLastNodeInlist);
pCurrent->left = *pLastNodeInlist;
if (*pLastNodeInlist != nullptr)
(*pLastNodeInlist)->right = pCurrent;
*pLastNodeInlist = pCurrent;
if (pCurrent->right != nullptr)
ConvertNode(pCurrent->right, pLastNodeInlist);
}
BinaryTreePointer Convert(BinaryTreePointer pRootOftree)
{
BinaryTreePointer pLastNodeInList = nullptr;
ConvertNode(pRootOftree, &pLastNodeInList);
//返回头节点
BinaryTreePointer pHeadOfList = pLastNodeInList;
while (pHeadOfList != nullptr&&pHeadOfList->left != nullptr)
pHeadOfList = pHeadOfList->left;
return pHeadOfList;
}
//二叉查找树的第K大节点
BinaryTreePointer KthNodeCore(BinaryTreePointer pRoot, unsigned int& k)
{
BinaryTreePointer target = nullptr;
if (pRoot->left != nullptr)
target = KthNodeCore(pRoot->left, k);
if (target == nullptr)
{
if (k == 1)
target = pRoot;
k--;
}
if (target == nullptr&&pRoot->right != nullptr)
target = KthNodeCore(pRoot->right, k);
return target;
}
BinaryTreePointer KthNode(BinaryTreePointer pRoot, unsigned int k)
{
if (pRoot == nullptr || k == 0)
return nullptr;
return KthNodeCore(pRoot, k);
}
int main()
{
int a[7] = {42,13,6,95,14,79,100,};
for (int i = 0; i < 7; i++)
{
insert(a[i]);
}
cout << Root->value << endl<<"中序遍历: ";
read(cout);
//MirrorRecursively(Root);
//cout << endl<<"镜像";
//read(cout);
cout << endl<<"从上到下打印二叉树1:";
PrintFromTopToBottom1(Root);
cout << endl << "从上到下打印二叉树2:"<<endl;
PrintFromTopToBottom2(Root);
cout << endl << "二叉查找树的第K大节点:" << KthNode(Root,1)->value <<endl;
cin.get();
}