Problem Solving using C++
#include <cstdlib>
using namespace std;
#ifndef NULL
#define NULL 0
#endif
#ifndef MAXSIZE
#define MAXSIZE 10
#endif
typedef struct BST//Binary Search Tree{
int key;
//maybe there are some other satellites
struct BST* left;
struct BST* right;
struct BST* parent;
} BST;
BST
void BST_Insert(int key)//add value key to the Binary Search Tree{
BST* y=NULL;//y records the parent node BST* x=root;//x records the current node
BST* node =new BST();
node->key = key;
node->left = NULL;
node->right = NULL;
node->parent = NULL;
while
{
y=x;
if(key<x->key)
x=x->left;
else
x=x->right;
}
node->parent=y;
if(y==NULL)
root = node;
else
{
if(key<y->key)
y->left=node;
else
y->right=
}
}
void BST_Delete(BST* p)
{
if(p)
{
BST_Delete(p->left);
BST_Delete(p->right);
delete p;
}
}
void BST_Build()
{
int temp;
cout<<"Original Input:"<<endl;
for(int i=0;i<MAXSIZE;i++)
{
temp=rand()%MAXSIZE;
cout<<temp<<"";
BST_Insert(temp);
}
cout<<endl;
}
void BST_Inorder_Walk(BST* p)
{
if(p)
{
BST_Inorder_Walk(p->left);
cout<<p->key<<"";
BST_Inorder_Walk(p->right);
}
}
void BST_Preorder_Walk(BST* p)
{
if(p)
{
cout<<p->key<<"";
BST_Preorder_Walk(p->left);
BST_Preorder_Walk(p->right);
}
}
void BST_Postorder_Walk(BST* p)
{
if(p)
{
BST_Postorder_Walk(p->left);
BST_Postorder_Walk(p->right);
cout<<p->key<<"";
}
}
BST* BST_Search(int key)
{
BST* x=root;
while(x)
{
if(x->key==key)
return x;
elseif(x->key>key)
x=x->left;
else
x=x->right;
}
return NULL;
}
BST* BST_Min(BST* p=root)
{
//BST* p = root;while(p->left)
{
p=p->left;
}
return p;
}
BST* BST_Max(BST* p=root)
{
//BST* p = root;while(p->right)
{
p=p->right;
}
return p;
}
BST* BST_Successor(int key)
{
BST* p = BST_Search(key);
BST* y;
if(p)
{
if(p->right)
{
return BST_Min(p->right);
}
y=p->parent;
while(y&&(y->right==p))
{
p=y;
y=y->parent;
}
return y;
}
return NULL;
}
BST* BST_Predecessor(int key)
{
BST* p = BST_Search(key);
BST* y;
if(p)
{
if(p->left)
return BST_Max(p->left);
y=p->parent;
while(y&&(y->left==p))
{
p=y;
y=y->parent;
}
return y;
}
return p;
}
BST* BST_Delete(int key)
{
BST* p = BST_Search(key);
BST* y,*x;
if(p)
{
if(!p->left||!p->right)
{
y=p;
}
else
y=BST_Successor(key);
if(y->left)
x=y->left;
else
x=y->right;
if(x!=NULL)
x->parent=y->parent;
if(!y->parent)
root=x;
else
{
if(y==y->parent->left)
y->parent->left=x;
else
y->parent->right=x;
}
if(y!=p)
p->key=y->key;
return y;
}
return p;
}
int main(int argc,char* argv[])
{
int input;
BST_Build();
BST_Delete(7);
cout<<"Inorder Walk:"<<endl;
BST_Inorder_Walk(root);
cout<<endl;
cout<<"Preorder Walk:"<<endl;
BST_Preorder_Walk(root);
cout<<endl;
cout<<"Postorder Walk:"<<endl;
BST_Postorder_Walk(root);
cout<<endl;
cout<<"Min: "<<BST_Min()->key<<endl;
cout<<"Max: "<<BST_Max()->key<<endl;
while(1)
{
cin>>input;
if(input==-1)
break;
BST* p;
if(p=BST_Search(input))
{
cout<<"Parent="<<(p->parent)->key<<endl;
if(p->left)
cout<<"Left:"<<p->left->key<<endl;
if(p->right)
cout<<"Right:"<<p->right->key<<endl;
}
else
{
cout<<"Not found!"<<endl;
break;
}
if(p=BST_Predecessor(input))
{
cout<<"Predecessor:"<<p->key<<endl;
}
if(p=BST_Successor(input))
{
cout<<"Successor:"<<p->key<<endl;
}
}
BST_Delete(root);
cout<<endl;
system("pause");
return0;
}
相關推薦
Problem Solving using C++
#include <iostream>#include <cstdlib>using namespace std;#ifndef NULL#define NULL 0#endif#ifndef MAXSIZE#define MAXSIZE 10#endiftypedef stru
bzoj 1700: [Usaco2007 Jan]Problem Solving 解題 ——dp
ble 比較 表示 opened play lin ans 轉移 hid Description 過去的日子裏,農夫John的牛沒有任何題目. 可是現在他們有題目,有很多的題目. 精確地說,他們有P (1 <= P <= 300) 道題目要做. 他們還離開了農
bzoj 1700 Problem Solving 解題 dp
com mat cst time include 免費 普通 圖片 string [Usaco2007 Jan]Problem Solving 解題 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 492 Solved:
Csharp: read Sybase SQL anywhere5.5 using c#
lec oid info clu dbf click nec select post private void button1_Click(object sender, EventArgs e) { try
TCP 、UDP網絡編程作業代寫、代寫C 語言 TCP程序 Network Programming using C
java upper single required mission GC recommend may could TCP 、UDP網絡編程作業代寫、代寫C 語言 TCP程序Network Programming using COverview? This homework
多線程動態規劃算法求解TSP(Traveling Salesman Problem) 並附C語言實現例程
影響 () 高效率 let ever 好的 出現 我們 運算 TSP問題描述: 旅行商問題,即TSP問題(Travelling Salesman Problem)又譯為旅行推銷員問題、貨郎擔問題,是數學領域中著名問題之一。假設有一個旅行商人要拜訪n個城市,他必須選擇所
problem-solving-with-algorithms-and-data-structure-usingpython(使用python解決算法和數據結構) -- 基本數據結構(一)
匹配 剛才 第一個 ems sem spl pla 查看 線性數據結構 1. 什麽是線性數據結構? 棧,隊列,deques, 列表是一類數據的容器,它們數據項之間的順序由添加或刪除的順序決定。 一旦一個數據項被添加,它相對於前後元素一直保持該位置不變。 諸
Luogu_2876_[USACO07JAN]解決問題Problem Solving
題目描述 過去的日子裡,農夫John的牛沒有任何題目. 可是現在他們有題目,有很多的題目. 精確地說,他們有\(P(1 \leq P \leq 300)\)道題目要做. 他們還離開了農場並且象普通人一樣找到了工作. 他們的月薪是\(M(1 \leq M \leq 1000)\) 元. 他們的題目是一流的
Problem Description——用c語言實現素數的判定
Problem Description 對於表示式n^2+n+41,當n在(x,y)範圍內取整數值時(包括x,y)(-39<=x<y<=50),判定該表示式的值是否都為素數。 Input 輸入資料有多組,每組佔一行,由兩個整數x,y組成,當x=0,y=0時,表示輸入結束,該
CGLIB Common causes of this problem include using a final class or a non-visible class;
類似以下的spring錯誤資訊: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class $Proxy145]: Common causes o
Problem Solving Process of The terminal process terminated with exit code 1
參考前輩的配置VScode C/C++環境的經驗: 成成賜我力量 bat67 參考之後我的配置 c_cpp_properties.json "configurations": [ { "name": "Mac",
Problem Solving Skills: Add Two Numbers
Problem Solving Skills: Add Two NumbersMy thinking process to solve the Add Two Numbers problemWhen I first solve this problem, I felt it could be a good e
洛谷P2876 [USACO07JAN]解決問題Problem Solving
n <= 300 求最優問題 考慮動態規劃 ; f[i][j] 是 第i個月 完成了前j個任務 我們的決策有兩個 (1) 不做任務 , 光還債;(2) 做任務 然後我們寫出dp方程 (圖中sa是預付款的字首和 , sb是後付款的字首和) #include
Sign Up and Confirm With Amazon Cognito User Pools Using C#
This post was authored by Tom Moore & Mike Morain, AWS Solutions Architects. With Amazon Cognito, you can add user sign-up and sign-in
【HDUOJ】第1002題 A + B Problem II 純C語言解法
【HUDOJ-1002】1.原題:Problem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of
反射技術與工廠方法 (using C#)
讓我們看這樣一個例子,我們需要建立一種交通工具,可以是汽車、火車或者輪船,結構如下: 我們可以採用簡單工廠,通過引數告訴建立工廠我們所需要的物件型別。如果我們增加子類,比如卡車、轎車等等,我們必須增加引數和相應的程式碼,如果子類層次很多,就會使程式變得難以維護。如果用簡單工廠實現上面的結構顯然很煩瑣。
1001 A + B Problem II(C語言 大數加法模板)
#include<stdio.h> int len(char *a)//一個判斷字串長度的函式與strlen一樣 { int i=0; while(*a++!='\0') { i++; } return i; } /*************
lintcode :expression expand using C++
Given an expression s includes numbers, letters and brackets. Number represents the number of repetitions inside the brackets(can b
implementation of Vector (using C++)
The Vector will be a first-class type,meaning that unlike the primitive array in C++,the Vector can be cpoied,and the memory it uses can
Using Swift with Cocoa and Objective-C下載
target cocoa 下載地址 obj swift nbsp 地址 bject uil 《Using Swift with Cocoa and Objective-C Building App 》 下載地址 http://download.csdn.net/