字串搜尋樹

2021-06-13 01:51:50 字數 4371 閱讀 2473

字串搜尋樹支援使用字串對物件建立索引,以便於查詢。 這些字串應該都是從物件的屬性上獲取的。而且可以按照指定的規則將字串分解為單詞。

insert方法用來在乙個單詞和指定物件之間建立關聯。

remove方法用來在索引樹中刪除乙個指定物件。

prune方法用於在刪除了大量物件後,優化索引樹,刪除那些沒有作用的節點。

find方法用來查詢物件;支援萬用字元查詢, 萬用字元為*,而且只能放在查詢字串的開始。

public class stringindextree

為了測試, 演示視窗中還顯示出了整個搜尋樹結構,包括每個節點下包含的物件個數。 下面就是demo的**:

後置**如下:

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.windows;

using system.windows.controls;

using system.windows.data;

using system.windows.documents;

using system.windows.input;

using system.windows.media;

using system.windows.media.imaging;

using system.windows.shapes;

using infrastructure;

using system.collections.objectmodel;

namespace mysolution.controls.demo

private observablecollectionemps;

void stringindexingwindow_loaded(object sender, routedeventargs e)

lv.itemssource = emps;

buildstringindex();

tv.itemssource = _indexs.select(i => i.root);

}private list> _indexs = new list>();

private void buildstringindex()

, stringsplitoptions.removeemptyentries))

foreach (var word in item.birthdate.tostring().split(new , stringsplitoptions.removeemptyentries))

foreach (var word in item.address.tostring().split(new , stringsplitoptions.removeemptyentries))

}_indexs.add(nameindex);

_indexs.add(dateindex);

_indexs.add(addrindex);

}private void filtertextbox_textchanged(object sender, textchangedeventargs e)

else

}private void button_click(object sender, routedeventargs e);}

}public class employee

public datetime birthdate

public string address }}

stringindextree的**:

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace infrastructure

public stringindextreenoderoot

public bool insert(string key, t value)

public bool remove(t value)

public void prune()

public ienumerablefind(string filter)

if (filter[0] == wildcardchar)

else

return result;}}

else}}

}stringindextreenode的**:

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace infrastructure

key = key;

children = new list>();

values = new hashset();

}public stringindextreenode(string key, t value)

: this(key)

public string key

public hashsetvalues

public list> children

public bool insert(string key, t value)

//new key cannot be a child of current node

if (key.length < key.length)

//if key is same with current key, add value; 

//else the key is not child of current node

if (key.length == key.length)

return false;

}//make sure current key is start string of target key

if (!key.startswith(key, stringcomparison.currentcultureignorecase))

if (children.count == 0)

//check if child can accept the key

foreach (var child in children)

}string bestcommonstart = key;

stringindextreenodesibling = null;

foreach (var child in children)

}//no common with children, so add to current node

if (bestcommonstart.length == key.length)

//has common with children            

children.remove(sibling);

var commonnode = new stringindextreenode(bestcommonstart, value);

children.add(commonnode);

commonnode.children.add(new stringindextreenode(key, value));

commonnode.children.add(sibling);

return true;

}private string getlongestcommonstartstring(string str1, string str2)

}return string.empty;

}public bool remove(t value)

}return result;

}public void prune()

else if (children[i].children.count == 1)

}children[i].prune();}}

public stringindextreenodefindfromstart(string filter)

if (filter.length <= key.length)

foreach (var child in children)

}return null;

}public ienumerable> findwithcontain(string filter)

foreach (var child in children)

return returnvalue;

}public listselfanddecendentvalues}}

}

字串搜尋

include include include includeusing namespace std char grid 100 100 word 100 int n m int x int y int xx yy void search if k len int main int t cin t ...

字串搜尋

include include include includeusing namespace std char grid 100 100 word 100 int n m int x int y int xx yy void search if k len int main int t cin t ...

KMP字串搜尋

include include include includeusing namespace std string a,b a為被搜尋字串,b為指定字串。int nexte 5000 int next 5001 void makenext 求出前i個字元子串字首與字尾最長的序列數。nextval的求...