#include <iostream>
#include <queue>
#include <stack>
using namespace std;
class Node
{
public:
int data;
Node *parent;
Node *left;
Node *right;
public:
Node():data(-1),parent(NULL),left(NULL),right(NULL){}
Node(int num):data(num),parent(NULL),left(NULL),right(NULL){}
};
class Tree
{
public:
Tree(int num[], int len);
void insertNode1(int data);
void insertNode(int data);
Node *searchNode(int data);
void deleteNode(int data);
void InOrderTree();
void InOrderTree(Node *current);
void PreOrderTree();
void PreOrderTree(Node *current);
void PostOrderTree();
void PostOrderTree(Node *current);
void LevelOrderTree();
bool IsSortedTree(Tree tree);
private:
void insertNode(Node *current, int data);
Node *searchNode(Node *current, int data);
void deleteNode(Node *current);
public:
Node *root;
};
Tree::Tree(int num[], int len)
{
root = new Node(num[0]);
for(int i = 1; i < len; i++)
insertNode1(num[i]);
}
void Tree::insertNode1(int data)
{
Node *p, *par;
Node *newNode = new Node(data);
p = par = root;
while(p != NULL)
{
par = p;
if(data > p->data)
p = p->right;
else if(data < p->data)
p = p->left;
else if(data == p->data)
{
delete newNode;
return;
}
}
newNode->parent = par;
if(par->data > newNode->data)
par->left = newNode;
else
par->right = newNode;
}
Node *Tree::searchNode(Node *current, int data)
{
if(data < current->data)
{
if(current->left == NULL)
return NULL;
return searchNode(current->left, data);
}
else if(data > current->data)
{
if(current->right = NULL)
return NULL;
return searchNode(current->right, data);
}
return current;
}
void Tree::InOrderTree()
{
if(root == NULL)
return;
InOrderTree(root);
}
void Tree::InOrderTree(Node *current)
{
if(current != NULL)
{
InOrderTree(current->left);
cout < <current->data < <" ";
InOrderTree(current->right);
}
}
void Tree::PreOrderTree()
{
if(root == NULL)
return;
PreOrderTree(root);
}
void Tree::PreOrderTree(Node *current)
{
if(current != NULL)
{
cout < <current->data < <" ";
PreOrderTree(current->left);
PreOrderTree(current->right);
}
}
void Tree::PostOrderTree()
{
if(root == NULL)
return;
PostOrderTree(root);
}
void Tree::PostOrderTree(Node *current)
{
if(current != NULL)
{
PostOrderTree(current->left);
PostOrderTree(current->right);
cout < <current->data < <" ";
}
}
void Tree::LevelOrderTree()
{
queue <Node *>q;
Node *ptr = NULL;
q.push(root);
while(!q.empty())
{
ptr = q.front();
q.pop();
cout < <ptr->data < <" ";
if(ptr->left != NULL)
q.push(ptr->left);
if(ptr->right != NULL)
q.push(ptr->right);
}
}
bool IsSortedTree(Tree tree)
{
int lastvalue = 0;
stack <Node *>s;
Node *p = tree.root;
while(p != NULL || !s.empty())
{
while(p != NULL)
{
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
s.pop();
if(lastvalue == 0 || lastvalue < p->data)
lastvalue = p->data;
else if(lastvalue >= p->data)
return false;
p = p->right;
}
}
return true;
}
int main()
{
int num[] = {5, 3, 7, 2, 4, 6, 8, 1};
Tree tree(num, 8);
cout < <"InOrder:";
tree.InOrderTree();
cout < <endl;
cout < <"PreOrder:";
tree.PreOrderTree();
cout < <endl;
cout < <"PostOrder:";
tree.PostOrderTree();
cout < <endl;
cout < <"LevelOrder:";
tree.LevelOrderTree();
cout < <endl;
cout < <"IsSortedTree:" < <IsSortedTree(tree) < <endl;
cout < <endl;
Node *node = tree.searchNode(4); //这道经典的程序这个地方怎么改?
node->data = 1;
cout < <"InOrder:";
tree.InOrderTree();
cout < <endl;
cout < <"IsSortedOrderTree:" < <IsSortedTree(tree) < <endl;
cout < <endl;
system("pause");
return 0;
}
二叉搜索
最新推荐文章于 2024-09-18 19:31:33 发布