16,551
社区成员
发帖
与我相关
我的任务
分享
#pragma once
#include "resource.h"
struct bitree
{
int data;
bitree *lChild,*rChild;
};
bitree *Creat(int n);//创建二叉排序树,反复调用Insert函数
void Insert(bitree **BST,int x);
void MidOrder(bitree *BST);
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Test.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
int nCountNode = 0;
bitree *root;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
cout <<"请输入结点数目:" <<endl;
cin>>nCountNode;
cout <<"请输入" <<nCountNode <<"个结点" <<endl;
root = Creat(nCountNode);
cout <<"中序遍历:" <<endl;
MidOrder(root);
cout <<endl;
}
return nRetCode;
}
bitree *Creat(int n)//创建二叉排序树,反复调用Insert函数
{
// inline void Insert(bitree *BST,int x);
int x;
bitree *BST = NULL;
for(int i = 1;i <=n;i++)
{
cout <<"第" <<i <<"个:" <<endl;
cin>>x;
Insert(&BST,x);
}
return BST;
}
void Insert(bitree **BST,int x)//插入
{
if(*BST == NULL)
{
// bitree *p = new bitree;
// p->data = x;
// p->lChild = NULL;
// p->rChild = NULL;
*BST = new bitree;
(*BST)->data = x;
(*BST)->lChild = NULL;
(*BST)->rChild = NULL;
}
else if((*BST)->data >= x)
{
Insert(&(*BST)->lChild,x);
}
else
{
Insert(&(*BST)->rChild,x);
}
}
void MidOrder(bitree *BST)//中序遍历
{
bitree *p = BST;
bitree *q[100];
int top = 0;
while(p != NULL || top > 0)
{
while(p != NULL)
{
q[++top] = p;
cout <<p->data <<" ";
p = p->lChild;
}
p = q[top--];
p = p->rChild;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
int flag = FALSE;
struct element {
int key;
};
struct tree_node {
tree_node *left_child;
tree_node *right_child;
element data;
int bf;
};
void left_rotation(tree_node *&parent)
{
tree_node *child = parent->left_child;
if(child->bf == 1) {
parent->left_child = child->right_child;
child->right_child = parent;
parent->bf = 0;
parent = child;
} else {
tree_node *grand_child = child->right_child;
parent->left_child = grand_child->right_child;
child->right_child = grand_child->left_child;
grand_child->right_child = parent;
grand_child->left_child = child;
switch(grand_child->bf) {
case 0: parent->bf = child->bf = 0;
break;
case 1: parent->bf = -1;
child->bf = 0;
break;
case -1: parent->bf = 0;
child->bf = 1;
}
parent = grand_child;
}
parent->bf = 0;
}
void right_rotation(tree_node *&parent)
{
tree_node *child = parent->right_child;
if(child->bf == -1) {
parent->right_child = child->left_child;
child->left_child = parent;
parent->bf = 0;
parent = child;
} else {
tree_node *grand_child = child->left_child;
parent->right_child = grand_child->left_child;
child->left_child = grand_child->right_child;
grand_child->right_child = child;
grand_child->left_child = parent;
switch(grand_child->bf) {
case 0: parent->bf = child->bf = 0;
break;
case -1: parent->bf = 1;
child->bf = 0;
break;
case 1: parent->bf = 0;
child->bf = -1;
}
parent = grand_child;
}
parent->bf = 0;
}
void avl_insert(tree_node *&parent, element x, int &unbalanced)
{
if(parent == NULL) {
parent = (tree_node*)malloc(sizeof(tree_node));
parent->left_child = parent->right_child = NULL;
parent->data = x;
parent->bf = 0;
unbalanced = TRUE;
} else if(x.key < parent->data.key) {
avl_insert(parent->left_child, x, unbalanced);
if(unbalanced) {
switch(parent->bf) {
case 0: parent->bf = 1;
break;
case -1: parent->bf = 0;
unbalanced = FALSE;
break;
case 1: left_rotation(parent);
unbalanced = FALSE;
}
}
} else if(x.key > parent->data.key) {
avl_insert(parent->right_child, x, unbalanced);
if(unbalanced) {
switch(parent->bf) {
case 0: parent->bf = -1;
break;
case 1: parent->bf = 0;
unbalanced = FALSE;
break;
case -1: right_rotation(parent);
unbalanced = FALSE;
}
}
} else {
unbalanced = FALSE;
}
}
void swap(tree_node *p, tree_node *q)
{
element temp = p->data;
p->data = q->data;
q->data = temp;
}
void avl_del(tree_node *&parent, element x, int &unbalanced)
{
if(parent->data.key == x.key) {
unbalanced = TRUE;
if(!parent->right_child && !parent->left_child) {
parent = NULL;
} else if(parent->right_child && !parent->left_child) {
parent = parent->right_child;
} else if(parent->left_child && !parent->right_child) {
parent = parent->left_child;
} else if(parent->left_child && parent->right_child) {
tree_node *p = parent;
p = p->right_child;
while(p->left_child) {
p = p->left_child;
}
swap(parent, p);
avl_del(parent->right_child, x, unbalanced);
}
} else if(x.key > parent->data.key) {
if(!parent->right_child) return;
avl_del(parent->right_child, x, unbalanced);
if(unbalanced) {
switch(parent->bf) {
case 0: parent->bf = 1;
unbalanced = FALSE;
break;
case -1: parent->bf = 0;
unbalanced = FALSE;
break;
case 1: left_rotation(parent);
}
}
} else if(x.key < parent->data.key) {
if(!parent->left_child) return;
avl_del(parent->left_child, x, unbalanced);
if(unbalanced) {
switch(parent->bf) {
case 0: parent->bf = -1;
unbalanced = FALSE;
break;
case 1: parent->bf = 0;
unbalanced = FALSE;
break;
case -1: right_rotation(parent);
}
}
}
}
void inorder(tree_node*);
int depth(tree_node*);
int main()
{
tree_node *root = NULL;
int tmp;
cin>>tmp;
while(tmp!=-1000) {
element data;
data.key = tmp;
avl_insert(root, data, flag);
cin>>tmp;
}
inorder(root);
printf("\n");
int depleft = depth(root->left_child);
int depright = depth(root->right_child);
printf("%d\n", depleft);
printf("%d\n", depright);
element data;
data.key = 92;
avl_del(root, data, flag);
inorder(root);
printf("\n");
//PostOrderFree(root);
return 0;
}
int depth(tree_node *root)
{
if(root == NULL) return 1;
else {
int rcd = depth(root->right_child);
int lcd = depth(root->left_child);
if(lcd > rcd) return lcd + 1;
else return rcd + 1;
}
}
struct stack {
tree_node *data[1024];
int top;
};
void initstack(stack *s)
{
s->top = -1;
}
void pushstack(stack *s, tree_node *p)
{
if(s->top >= 1024) {
printf("stack full\n");
exit(1);
}
s->data[++s->top] = p;
}
void popstack(stack *s, tree_node *&p)
{
if(s->top == -1) {
printf("stack empty\n");
exit(1);
}
p = s->data[s->top--];
}
void inorder(tree_node *root)
{
if(root)
{
inorder(root->left_child);
printf("%d",root->data.key);
inorder(root->right_child);
}
}
void PostOrderFree(tree_node *root)
{
if(root)
{
PostOrderFree(root->left_child);
PostOrderFree(root->right_child);
free(root);
}
}