Puuuurimine Targo Tennisberg Aprill 2014

Slides:



Advertisements
Similar presentations
Fenwick Tree (binary indexed tree).
Advertisements

Computer Science C++ High School Level By Guillermo Moreno.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Lowest Common Ancestors Two vertices (u, v) Lowest common ancestors, lca (u, v) Example lca (5, 6) = 4 lca (3, 7) = 2 lca (7, 8) = 1 l(v):
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.
Optimal binary search trees
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
1 8/16/2015 MATH 224 – Discrete Mathematics A rooted tree is a tree that has a distinguished node called the root. Just as with all trees a rooted tree.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
Data Structure (III) GagGuy.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
Building Java Programs Binary Search Trees; TreeSet.
BSTImp: Insert, Delete. Inserting an Element into a BST Search for the position in the tree where the element would be found Insert the element in the.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
Trees By Charl du Plessis. Contents Basic Terminology Basic Terminology Binary Search Trees Binary Search Trees Interval Trees Interval Trees Binary Indexed.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
The LCA Problem Revisited
Analysis of Algorithms CS 477/677
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Interval Trees Marco Gallotta. Problem ● Given a collection of items i, each with value V i ● Want to answer many queries of the form: How many items.
Tree Insert Animation.
Recursive Objects (Part 4)
Binary search tree. Removing a node
Binary Search Trees Chapter 7 Objectives
Trees.
Topic 18 Binary Search Trees
Binary Search Trees.
Priority Queues Linked-list Insert Æ Æ head head
Chapter 20: Binary Trees.
Binary Search Trees.
Topic 19 Binary Search Trees
Chapter 21: Binary Trees.
فصل سوم: برنامه نویسی پویا
Building Java Programs
Binary Search Tree AVL Tree
Node Removal From BST Source:
Find in a linked list? first last 7  4  3  8 NULL
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Search Trees (BSTs)
Trees Lecture 9 CS2110 – Fall 2009.
Binary Search Trees (13.1/12.1)
Binary Search Trees (BSTs)
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Representing binary trees with lists
Lecture 21: Binary Search Trees; TreeSet
2019/4/10 chapter25.
Trees.
Building Java Programs
Lecture 21: Binary Search Trees; TreeSet
Trees are Everywhere family genealogy organizational charts
Trees.
Lecture 10: BST and AVL Trees
Priority Queue and Heap
Binary Search Trees (BSTs)
Honors Track: Competitive Programming & Problem Solving Binary Indexed Trees Christiaan Dirkx.
Trees Lecture 10 CS2110 – Spring 2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Puuuurimine Targo Tennisberg Aprill 2014 http://www.targotennisberg.com/eio http://www.targotennisberg.com/tarkvara

Kava Kahendpuu Kahendotsimise puu Fenwicki puu Lõikude puu

Kahendpuu

Kahendpuude erijuhud Homogeenne kahendpuu Kompaktne kahendpuu Täielik kahendpuu

Kahendpuu „loomulik“ esitus

Kahendpuu esitus massiivina Sobib hästi täielike ja kompaktsete puude jaoks ai vasak alluv on a2i ai parem alluv on a2i+1 ai ülemus on ai/2

Kahendpuu läbimine Eesjärjestus Keskjärjestus Lõppjärjestus

Kahendotsingu puu Vasaku alampuu kõik väärtused on juurtipu väärtusest väiksemad Parema alampuu kõik väärtused on juurtipu väärtusest suuremad Alampuud on ise ka kahendotsingu puud

Otsimine otsingupuus function Find-recursive(key, node): // call initially with node = root if node = Null or node.key = key then return node else if key < node.key then return Find-recursive(key, node.left) else return Find-recursive(key, node.right)

Lisamine otsingupuusse bool BinarySearchTree::add(int value) { if (root == NULL) { root = new BSTNode(value); return true; } else return root->add(value); } bool BSTNode::add(int value) { if (value == this->value) return false; else if (value < this->value) { if (left == NULL) { left = new BSTNode(value); return true; } else return left->add(value); } else if (value > this->value) { if (right == NULL) { right = new BSTNode(value); return right->add(value); } return false;

Eemaldamine otsingupuust bool BinarySearchTree::remove(int value) { if (root == NULL) return false; else { if (root->getValue() == value) { BSTNode auxRoot(0); auxRoot.setLeftChild(root); BSTNode* removedNode = root->remove(value, &auxRoot); root = auxRoot.getLeft(); if (removedNode != NULL) { delete removedNode; return true; } else return false; } else { BSTNode* removedNode = root->remove(value, NULL); } } } BSTNode* BSTNode::remove(int value, BSTNode *parent) { if (value < this->value) { if (left != NULL) return left->remove(value, this); else return NULL; } else if (value > this->value) { if (right != NULL) return right->remove(value, this); } else { if (left != NULL && right != NULL) { this->value = right->minValue(); return right->remove(this->value, this); } else if (parent->left == this) { parent->left = (left != NULL) ? left : right; return this; } else if (parent->right == this) { parent->right = (left != NULL) ? left : right; } int BSTNode::minValue() { if (left == NULL) return value; else return left->minValue();

Fenwicki puu Tuntud ka kui „binary indexed tree“ (BIT) Ülesanne: Meil on n karpi ja võimalikud operatsioonid: Lisada kuulike mingisse karpi Leida, mitu kuulikest on karpides i kuni j Naiivne lahendus on O(1) lisamiseks ning O(n) otsimiseks

Fenwicki puu F[i] – kuulikeste arv karbis i C[i] – kuulikeste arvude summa F[0] + … + F[i] Tree[i] – kuulikeste arvude summa vastutusalaga i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 tree 1..2 1..4 5..6 1..8 9..10 9..12 13..14 1..16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 f c 19 21 23 26 27 29 tree

Fenwicki puu Oletame, et soovime esimese 13 karbi summat 13 kahendsüsteemis on 1101 c[1101] = tree[1101] + tree[1100] + tree[1000]

Viimase mittenullise biti isoleerimine X & -X Bitwise operaator Proovige, mida see teeb! Miks?

Esimese n karbi lugemine int read(int idx) { int sum = 0; while (idx > 0){ sum += tree[idx]; idx -= (idx & -idx); } return sum;

Kuulikeste lisamine void update(int idx ,int val) { while (idx <= MaxVal) tree[idx] += val; idx += (idx & -idx); }

position of the last digit Lisamine: näide iteration idx position of the last digit idx & -idx 1 5 = 101 1 (2 ^0) 2 6 = 110 2 (2 ^1) 3 8 = 1000 8 (2 ^3) 4 16 = 10000 16 (2 ^4) 5 32 = 100000 ---

Lisamine: näide

Fenwicki puu - ülesanne http://community.topcoder.com/stat?c=problem_statement&pm=6551&rd=9990 Given a sequence of N integers in the range [0,M), find the sum of medians of N-K+1contiguous subsequences of length K. (1<=N<=250,000; 1<=K<=1000, M=65536).

Lähim esivanem (Lowest Common Ancestor, LCA) Probleem: leida evolutsioonipuus kahe liigi lähim ühine esivanem

Range Minimum Query (RMQ) Kuidas leida massiivi alamlõigus väikseima elemendi indeks?

RMQ - eeltöötlus Et see päring oleks võimalikult kiire, on vajalik eeltöötlus Koostame näiteks 2D tabeli, kus on kõikvõimalikud miinimumid Jõumeetod – O(n3)

RMQ eeltöötlus – DP lähenemine void process1(int M[MAXN][MAXN], int A[MAXN], int N) { int i, j; for (i =0; i < N; i++) M[i][i] = i; for (i = 0; i < N; i++) for (j = i + 1; j < N; j++) if (A[M[i][j - 1]] < A[j]) M[i][j] = M[i][j - 1]; else M[i][j] = j; } Keerukus O(n2)

Lõikude puu Puu iga sõlm hoiab infot vastava lõigu kohta

Lõikude puu koostamine void initialize(int node, int b, int e, int M[MAXIND], int A[MAXN], int N) { if (b == e) M[node] = b; else //compute the values in the left and right subtrees initialize(2 * node, b, (b + e) / 2, M, A, N); initialize(2 * node + 1, (b + e) / 2 + 1, e, M, A, N); //search for the minimum value in the first and //second half of the interval if (A[M[2 * node]] <= A[M[2 * node + 1]]) M[node] = M[2 * node]; M[node] = M[2 * node + 1]; }

Lõikude puust pärimine int query(int node, int b, int e, int M[MAXIND], int A[MAXN], int i, int j) { int p1, p2; //if the current interval doesn't intersect //the query interval return -1 if (i > e || j < b) return -1; //if the current interval is included in //the query interval return M[node] if (b >= i && e <= j) return M[node]; //compute the minimum position in the //left and right part of the interval p1 = query(2 * node, b, (b + e) / 2, M, A, i, j); p2 = query(2 * node + 1, (b + e) / 2 + 1, e, M, A, i, j); //return the position where the overall //minimum is if (p1 == -1) return M[node] = p2; if (p2 == -1) return M[node] = p1; if (A[p1] <= A[p2]) }

LCA jälle

LCA - läbikäimine Käime puu sügavuti läbi Leiame kõrgeima sõlme u ja v vahel

LCA ja RMQ Koostame 3 massiivi: E[1, 2*N-1] – läbikäimisel külastatud sõlmed L[1, 2*N-1] – läbikäimisel külastatud sõlmede sügavused H[1, N] – sõlmede esmakordsed esinemised massiivis E

LCA ja RMQ LCAT(u, v) = E[RMQL(H[u], H[v])] 

Lõikude puu - ülesanne http://poj.org/problem?id=2374