0% found this document useful (0 votes)
2 views7 pages

DUYỆT CÂY 3

The document contains C++ code for various tree traversal algorithms and permutation generation. It includes functions for inserting nodes into binary trees and performing BFS and spiral order traversals. Additionally, it features a function to generate all permutations of a given set of integers.

Uploaded by

kiennguyen020204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

DUYỆT CÂY 3

The document contains C++ code for various tree traversal algorithms and permutation generation. It includes functions for inserting nodes into binary trees and performing BFS and spiral order traversals. Additionally, it features a function to generate all permutations of a given set of integers.

Uploaded by

kiennguyen020204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

DUYỆT CÂY 3

#include <bits/stdc++.h>

using namespace std;

// Cau truc node cua cay nhi phan

struct Node {

int data;

Node* trai;

Node* phai;

Node(int val) {

data = val;

trai = phai = NULL;

};

// Ham chen node vao cay theo u v x

void chen(Node* &goc, int u, int v, char x, map<int, Node*> &danhsach) {

Node* nut_cha;

if (danhsach.count(u)) nut_cha = danhsach[u];

else {

nut_cha = new Node(u);

danhsach[u] = nut_cha;

if (!goc) goc = nut_cha;

Node* nut_con = new Node(v);

if (x == 'L') nut_cha->trai = nut_con;

else nut_cha->phai = nut_con;

danhsach[v] = nut_con;

}
// Ham duyet BFS (level-order traversal)

void duyet_BFS(Node* goc) {

if (!goc) return;

queue<Node*> q;

q.push(goc);

while (!q.empty()) {

Node* hien_tai = q.front(); q.pop();

cout << hien_tai->data << " ";

if (hien_tai->trai) q.push(hien_tai->trai);

if (hien_tai->phai) q.push(hien_tai->phai);

cout << endl;

int main() {

int T; cin >> T;

while (T--) {

int N; cin >> N;

Node* goc = NULL;

map<int, Node*> danhsach;

for (int i = 0; i < N; ++i) {

int u, v; char x;

cin >> u >> v >> x;

chen(goc, u, v, x, danhsach);

duyet_BFS(goc);

return 0; }

DUYỆT CÂY KIỂU XOẮN ỐC


#include<iostream>

#include<vector>

#include<iomanip>

#include<algorithm>

#include<string.h>

#include<string>

#include<climits>

#include<set>

#include<map>

#include<stack>

#include<queue>

#define mod 1000000007

#define ll unsigned long long

#define p(x) pair<x,x>

#define v(x) vector<x>

#define tree node*

#define pb(a) push_back(a)

#define pf(a) push_front(a)

#define FOR(i,l,r) for(int i=l;i<r;i++)

#define FORX(i,l,r,x) for(int i=l;i<r;i+=x)

#define FORD(i,l,r) for(int i=l;i>=r;i--)

#define correct(x,y,n,m) 0<=(x)&&(x)<(n)&&0<=(y)&&(y)<(m)

#define cin(M,n) FOR(i,0,n)cin>>M[i]

#define cout(M,n) FOR(i,0,n)cout<<M[i]<<" "

#define rs(M,x) memset(M,x,sizeof(M))

#define reset() FOR(i, 0, 1001)A[i].clear(),check[i]=false

#define faster() cin.tie(0); ios_base::sync_with_stdio(false); cout.tie(0);

#define run() int t; cin >> t; while (t--)

#define pq(x ) priority_queue<x>

#define neg_pq(x) priority_queue<x, vector<x>, greater<x>>


#define all(M) M.begin(),M.end()

using namespace std;

struct node {

int data;

node* pLeft;

node* pRight;

node(int x) {

this->data = x;

pLeft = pRight = NULL;

};

void Add(tree&T,int u,int v,char c) {

if (T == NULL) {

T = new node(u);

tree p = new node(v);

if (c == 'L')T->pLeft = p;

else T->pRight = p;

else {

if (T->data == u) {

tree p = new node(v);

if (c == 'L')T->pLeft = p;

else T->pRight = p;

else {

if (T->pLeft != NULL)Add(T->pLeft, u, v, c);

if (T->pRight != NULL)Add(T->pRight, u, v, c);


}

void Load(tree T) {

if (T == NULL)return;

stack<tree> S1, S2;

S1.push(T);

while (S1.size() || S2.size()) {

while (S1.size()) {

tree tmp = S1.top();

S1.pop();

cout << tmp->data << " ";

if (tmp->pRight != NULL)S2.push(tmp->pRight);

if (tmp->pLeft != NULL)S2.push(tmp->pLeft);

while (S2.size()) {

tree tmp = S2.top();

S2.pop();

cout << tmp->data << " ";

if (tmp->pLeft != NULL)S1.push(tmp->pLeft);

if (tmp->pRight != NULL)S1.push(tmp->pRight);

int main(){

faster();

run() {
int n; cin >> n;

tree T = NULL;

FOR(i, 0, n) {

int u, v; char c;

cin >> u >> v >> c;

Add(T, u, v, c);

Load(T);

cout << endl;

Sinh Hoán vị

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

void sinh_hoan_vi(int N) {

vector<int> a(N);

for (int i = 0; i < N; ++i)

a[i] = i + 1;

do {

for (int i = 0; i < N; ++i)

cout << a[i];

cout << " ";

} while (next_permutation(a.begin(), a.end()));

cout << endl;

}
int main() {

int T;

cin >> T;

while (T--) {

int N;

cin >> N;

sinh_hoan_vi(N);

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy