Hackerrank-Data structures-ree


基本的な簡単なテーマは、データ構造の入門用に適しています.基本的には、まず中を通して、木の高さを測ったり、木の左右のノードを印刷したり、挿入したり、子樹を交換したり、ハフマン復号したりといった基本的な操作の難しさはModeateの1.Tree:Huffman Decodingです.
/*
  :Tree: Huffman Decoding
  :Moderate
  :/
  :      ,      :                
*/

#include
#include
using namespace std;
struct node
{
    int freq;
    char data;
    node * left;
    node * right;
};
void decode_huff(node* root,string s)
{
    int i = 0;
    node* temp=root;
    string result = "";
    while (i < s.length())
    {
        while (temp->data == '\0')
        {
            if (s[i] == '1')
            {
                temp = temp->right;
                i++;
            }
            else
            {
                temp = temp->left;
                i++;
            }
        }
        result =result+temp->data;
        temp = root;
    }
    cout << result;
}

int main()
{
    char a = '\0';
    cout << a;
    return 0;
}
総合的な1.Swap Nodes[Algo]
/*
  :Swap Nodes [Algo]
  :Easy
  :/
  :       ,    、  。       (         )
*/
#include
#include 
using namespace std;
struct Node
{
    int data;
    int height;
    Node *left;
    Node *right;
};

Node* Create(Node *root)
{
    int N;
    int left, right;
    int front = 0, rear = 0;
    Node *queue[2000];
    queue[rear++] = root;
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        Node *Leftchild = NULL, *Rightchild = NULL;
        Node *parent = queue[front++];
        cin >> left >> right;
        if (left != -1)
        {
            Leftchild = (Node*)malloc(sizeof(Node));
            Leftchild->data = left;
            Leftchild->left = NULL;
            Leftchild->right = NULL;
            Leftchild->height = parent->height + 1;

            queue[rear++] = Leftchild;
        }
        if (right != -1)
        {
            Rightchild = (Node*)malloc(sizeof(Node));
            Rightchild->data = right;
            Rightchild->left = NULL;
            Rightchild->right = NULL;
            Rightchild->height = parent->height + 1;

            queue[rear++] = Rightchild;
        }
        parent->left = Leftchild;
        parent->right = Rightchild;
    }
    return root;
}
void Mid(Node* root)
{
    if (root->left) Mid(root->left);
    cout << root->data<<' ';
    if (root->right) Mid(root->right);
}
Node* swap(Node *root,int height)
{
    Node *temp = root;
    if (root->height == height)
    {
        Node *temp2 = root->left;
        root->left = root->right;
        root->right = temp2;
        return root;
    }
    else
    {
        if (temp->left) temp->left = swap(temp->left, height);
        if (temp->right) temp->right = swap(temp->right, height);
    }
    return root;
}

int main() 
{
    int T, height;
    Node *root = (Node*)malloc(sizeof(Node));
    root->data = 1;
    root->height = 1;
    root->left = NULL;
    root->right = NULL;
    root = Create(root);
    cin >> T;
    for (int i = 0; i < T; i++)
    {
        cin >> height;
        for (int j = 1; j*height <= 1500; j++)
        {
            root = swap(root, j*height);
        }
        Mid(root);      
        cout << endl;
    }


    return 0;
}
難易度はEasyの1.Prorderです.
void Preorder(node *root) 
{
    if (root)
    {
        cout << root->data<<' ';
        Preorder(root->left);
        Preorder(root->right);
    }
}
2.Postorder
void Postorder(node *root) 
{
    if (root)
    {
        Postorder(root->left);
        Postorder(root->right);
        cout << root->data << ' ';
    }
}
3.Inorder
void Inorder(node *root) 
{
    if (root)
    {
        Inorder(root->left);
        cout << root->data << ' ';
        Inorder(root->right);
    }
}
4.ht
int height(node * root)
{
    if (!root)
        return 0;
    else
    {
        int L = height(root->left) + 1;
        int R = height(root->right) + 1;
        if (L >= R)
            return L;
        else
            return R;
    }
}
5.top_ビュー
void top_viewL(node* root)
{
    if (root)
        top_viewL(root->left);
    else
        return;
    cout << root->data << ' ';
}
void top_viewR(node* root)
{
    if (root)
    {
        cout << root->data << ' ';
        top_viewR(root->right);
    }
    else
        return;
}
void top_view(node * root)
{
    top_viewL(root->left);
    cout << root->data << ' ';
    top_viewR(root->right);
}
6.LevelOrder
void LevelOrder(node * root)
{
    node *queue[100];
    int front = 0, rear = 0;
    cout << root->data << ' ';
    queue[rear++] = root->left;
    queue[rear++] = root->right;
    while (front != rear)
    {
        node *temp = queue[front++];
        cout << temp->data << ' ';
        if(temp->left) 
            queue[rear++] = temp->left;
        if (temp->right) 
            queue[rear++] = temp->right;
    }
}
7.insert
node* insert(node * root, int value)
{
    node *temp = root;
    node* insert = (node*)malloc(sizeof(node));
    insert->data = value;
    insert->left = NULL;
    insert->right = NULL;
    if (!root)
        return insert;
    while (true)
    {
        if (value < temp->data)
            if (!temp->left) { temp->left = insert; break; }
            else temp = temp->left;
        else
            if (!temp->right){ temp->left = insert; break; }
            else temp = temp->right;
    }
    return root;
}
8.Lowest Common Ancestor
node * lca(node * root, int v1, int v2)
{
    int max, min=v1+v2;
    node *temp = root;
    v1 > v2 ? max = v1 : max = v2;
    min -= max;
    if (temp->data<max && temp->data>min)
        return temp;
    else
    {
        if (max < temp->data)
            if (temp->left)     temp = lca(temp->left, max, min);   
        else
            if (temp->right)    temp = lca(temp->right, max, min);
    }
    return temp;
}