#pragma once
#ifndef _BINARY_SERARCH_TREE
#include "Queue_L.h"
typedef struct ST_TREENODE
{
int val;
struct ST_TREENODE* pleftnode;
struct ST_TREENODE* prightnode;
};
class CBinarySearchTree
{
public:
CBinarySearchTree ();
~CBinarySearchTree ();
void InsertNode ( int nodeval );
void ShowNodes (int flag=0);
void DeleteAllItem ();
private:
bool bHaveLeftChild ( struct ST_TREENODE* pnode ); // 是否存在左子
bool bHaveRightChild ( struct ST_TREENODE* pnode ); // 是否存在右子
bool bHaveChild ( struct ST_TREENODE* pnode ); // 是否存在右子
struct ST_TREENODE* GetLeftChild ( struct ST_TREENODE* pnode ); // 是否存在左子
struct ST_TREENODE* GetRightChild ( struct ST_TREENODE* pnode ); // 是否存在右子
void GetTreeNodeValue ( QUEUENode queuenode, struct ST_TREENODE* pnode, int n = 0 );
void PreOrder ( struct ST_TREENODE* pnode ); // 左中右
void InOrder ( struct ST_TREENODE* pnode ); //中左右
void PostOrder ( struct ST_TREENODE* pnode ); // 右中左
struct ST_TREENODE* m_stRoot;
CQueue_L m_ql;
};
#endif
#include <stdio.h>
#include <iostream>
#include "Tree.h"
CBinarySearchTree::CBinarySearchTree ()
{
m_stRoot = nullptr;
}
CBinarySearchTree::~CBinarySearchTree ()
{
if (m_stRoot!=nullptr)
{
delete m_stRoot;
m_stRoot = nullptr;
}
}
void CBinarySearchTree::DeleteAllItem ()
{
// https://www.cnblogs.com/lliuye/p/9183914.html
}
void CBinarySearchTree::InsertNode ( int nodeval )
{
if ( m_stRoot ==nullptr)
{
m_stRoot = new struct ST_TREENODE; // 临时父节点
memset ( m_stRoot, 0, sizeof ( ST_TREENODE ) );
m_stRoot->val = nodeval;
}
else
{
struct ST_TREENODE* ptemp = m_stRoot;
struct ST_TREENODE *pnode = m_stRoot; // 临时父节点
// 遍历现有节点找到插入位置
while ( pnode != nullptr )
{
if (bHaveChild(pnode))
{
if ( nodeval >= pnode->val ) // 插入值大于 根值 右子
{
if (bHaveRightChild(pnode))
{
pnode = GetRightChild ( pnode );
}
else
break;
}
else if ( nodeval < pnode->val ) // 插入值小于 根值 左子
{
if ( bHaveLeftChild ( pnode ) )
{
pnode = GetLeftChild ( pnode );
}
else
break;
}
ptemp = pnode;
}
else
break;
}
pnode = new ST_TREENODE;
memset ( pnode, 0, sizeof ( ST_TREENODE ) );
pnode->val = nodeval;
if ( nodeval >= ptemp->val ) // 插入值大于 根值 右子
{
ptemp->prightnode = pnode;
}
else if ( nodeval < ptemp->val ) // 插入值小于 根值 左子
{
ptemp->pleftnode = pnode;
}
}
}
bool CBinarySearchTree::bHaveLeftChild ( struct ST_TREENODE* pnode ) // 是否存在左子
{
if ( pnode->pleftnode != nullptr )
return true;
else
return false;
}
bool CBinarySearchTree::bHaveRightChild ( struct ST_TREENODE* pnode ) // 是否存在右子
{
if ( pnode->prightnode != nullptr )
return true;
else
return false;
}
bool CBinarySearchTree::bHaveChild ( struct ST_TREENODE* pnode ) // 是否存在右子
{
if ( pnode->pleftnode != nullptr )
return true;
else
{
if ( pnode->prightnode != nullptr )
return true;
else
return false;
}
}
struct ST_TREENODE* CBinarySearchTree::GetLeftChild ( struct ST_TREENODE* pnode ) // 是否存在左子
{
if ( bHaveLeftChild ( pnode ) )
{
return pnode->pleftnode;
}
return NULL;
}
struct ST_TREENODE* CBinarySearchTree::GetRightChild ( struct ST_TREENODE* pnode ) // 是否存在右子
{
if ( bHaveRightChild ( pnode ) )
{
return pnode->prightnode;
}
return NULL;
}
QUEUENode queuenode;
void CBinarySearchTree::ShowNodes ( int flag )
{
if (flag==0) // 左中右
{
PreOrder ( m_stRoot );
std::cout << std::endl;
}
else if ( flag == 1 ) // 中左右
{
PostOrder ( m_stRoot );
std::cout << std::endl;
}
else if ( flag == 2 ) // 左右中
{
InOrder ( m_stRoot );
std::cout << std::endl;
}
}
void CBinarySearchTree::PreOrder ( struct ST_TREENODE* pnode ) //
{
if (pnode==nullptr)
return;
PreOrder ( pnode->pleftnode );
std::cout << pnode->val << " "; // 左中右
PreOrder ( pnode->prightnode );
}
void CBinarySearchTree::InOrder ( struct ST_TREENODE* pnode ) //左中右
{
if ( pnode == nullptr )
return;
std::cout << pnode->val << " ";
InOrder ( pnode->pleftnode );
InOrder ( pnode->prightnode );
}
void CBinarySearchTree::PostOrder ( struct ST_TREENODE* pnode ) // 左中右
{
if ( pnode == nullptr )
{
return;
}
PostOrder ( pnode->pleftnode );
PostOrder ( pnode->prightnode );
std::cout << pnode->val << " ";
}
void CBinarySearchTree::GetTreeNodeValue ( QUEUENode queuenode, struct ST_TREENODE* pnode, int n )
{
struct ST_TREENODE* tempnode;
if ( ( tempnode = GetLeftChild ( pnode ) ) != NULL )
{
if ( bHaveChild ( tempnode ) )
{
memset ( &queuenode, 0, sizeof ( QueueNode ) );
queuenode.pcnodedata = new char [sizeof ( ST_TREENODE )];
queuenode.pcnodedata = ( char* ) pnode;
queuenode.dwnodedatalen = sizeof ( ST_TREENODE );
GetTreeNodeValue ( queuenode, tempnode );
}
else
{
std::printf ( "rigth: %d ", tempnode->val );
if ( queuenode.pcnodedata != NULL )
{
delete[]queuenode.pcnodedata;
queuenode.pcnodedata = NULL;
}
}
}
else if ( ( tempnode = GetRightChild ( pnode ) ) != NULL )
{
if ( bHaveChild ( tempnode ) )
{
memset ( &queuenode, 0, sizeof ( QueueNode ) );
queuenode.pcnodedata = new char [sizeof ( ST_TREENODE )];
queuenode.pcnodedata = ( char* ) pnode;
queuenode.dwnodedatalen = sizeof ( ST_TREENODE );
GetTreeNodeValue ( queuenode, tempnode );
}
else
{
std::printf ( "left: %d ", tempnode->val );
// std::cout << "left: " << tempnode->val << std::endl;
if ( queuenode.pcnodedata != NULL )
{
delete[]queuenode.pcnodedata;
queuenode.pcnodedata = NULL;
}
}
}
}
#include "List.h"
#include "Tree.h"
#include "Graph.h"
int main ()
{
// CBinarySearchTree tr;
// int nval[] = { 45, 12, 78, 43, 26, 80, 11, 31, 77, 39 };//, 60,15,29,44
// //int nval[] = { 15,4,20,17,19 };
// for ( int i = 0; i < 10; i++ )
// {
// tr.InsertNode ( nval [i] );
// }
//
// tr.ShowNodes (0);
Graph g;
g.createGraph ();
g.printG ();
std::cout << std::endl << g.GetElement ( 2 ) << std::endl;
return 0;
}