Coder Social home page Coder Social logo

bediger4000 / tree-pattern-matching Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 1.0 16 KB

Hoffman and O'Donnel's "Pattern matching in trees" implemented in C

License: BSD 3-Clause "New" or "Revised" License

C 94.37% Makefile 4.19% Shell 1.44%
binary-trees algorithm matching

tree-pattern-matching's Introduction

Matching Patterns of binary trees

An implementation of: Hoffman, C. and O'Donnel, J., "Pattern matching in trees", JACM, 29(1), 68โ€“95, 1982.

Which in turn uses Aho/Corasick multiple string matching: Aho, Alfred V.; Margaret J. Corasick (June 1975). "Efficient string matching: An aid to bibliographic search". Communications of the ACM 18 (6): 333โ€“340.

Implemented in ANSI C89, I believe.

A Daily Coding Problem

This is a solution to Daily Coding Problem: Problem #877 [Hard], although the glob-style matching is overkill.

That problem statement reads:

This problem was asked by Google.

Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself.

Analysis

The "[Hard]" rating is possibly an understatement. There are other tree-matching algorithms, but they're all much more complicated, or they're dead slow (O(mn)) or they only work on proper subtrees. This is also not a well-known algorithm.

This problem is unsuited for whiteboarding. If it's not a take-home problem, that's a big red flag for the job candidate. There's no way a human could whiteboard any of the tree-matching algorithms.


TEST INPUT

Test program input trees are represented textually as lisp-style 2-element lists. Must be fully parenthesized.

(a b)
(one (two three))
((one two) (three four))

Internally, the algorithm uses conventional binary trees:

struct tree {
	enum node_type type;
	char   label[64];
	int    labelsz;
	int tree_size;
	struct tree *left;
	struct tree *right;
};

Patterns

Pattern lists can contain '*' characters which represent "match anything", globbing-style wildcards. A '*' matches any subtree, or any leaf. Any other character or string (besides parentheses and whitespace) matches a node name only.

$ ./test6 -p '(a *)' -s '((a b) (a c))'

Match pattern with subtree:
(a b)

Match pattern with subtree:
(a c)

$ ./test6 -p '(b *)' -s '((a b) (a c))'

$

BUILDING

make test6
sh ./test.input

Uses gcc as C compiler, but Clang and pcc also work.

tree-pattern-matching's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

pombredanne

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.