int InTree(Node *t, int i) { if(t == NULL) return 0; if(i == t->num) return 1; if(i < t->num) return InTree(t->left, i); else return InTree(t->right, i); }
// // InTree // Given a place to start the search and an ID to look for // do a depth-first search and // return the HTREEItem associated with the ID or NULL // HTREEITEM CThingTreeCtrl::InTree(HTREEITEM hParent, long lID) { // should we actually use this to search the whole tree? if (hParent == NULL) return NULL; long lCurrentId; HTREEITEM hCurrentChildItem; HTREEITEM hReturnItem = NULL; lCurrentId = (long) m_treeCtrl.GetItemData(hParent); if (lCurrentId == lID) return hParent; // not the parent, get the children if (!m_treeCtrl.ItemHasChildren(hParent)) return NULL; hCurrentChildItem = m_treeCtrl.GetChildItem(hParent); // not the first child, loop through the rest while (hCurrentChildItem != NULL) { hReturnItem = InTree(hCurrentChildItem, lID); if (hReturnItem) return hReturnItem; hCurrentChildItem = m_treeCtrl.GetNextSiblingItem(hCurrentChildItem); } return hReturnItem; }
void CThingTreeCtrl::SelectThing(LPDISPATCH Thing) { IThing *pCurrentThing = NULL; HRESULT hr = S_OK; long lCurrentID; HTREEITEM htItem = NULL; if (FAILED(hr = Thing->QueryInterface(IID_IThing, (void**)&pCurrentThing))) goto exit; if(pCurrentThing) { if (FAILED(hr = pCurrentThing->get_ID(&lCurrentID))) goto exit; // Search for the htreeitem that corresponds these ids htItem = InTree(m_hRoom, lCurrentID); // if we found the item, select it and exit if(htItem) { // TODO - Make sure this doesn send a TVN_SELCHANGED notification!!!! // It does, so we have to regulate it with a bool m_bSelectedByMe = FALSE; m_treeCtrl.SelectItem(htItem); } } exit: SAFERELEASE(pCurrentThing); }
HRESULT CThingTreeCtrl::OnEnter(IThing *pthing, CComVariant varArg) { HRESULT hr = S_OK; CThingPtr pWhat; long lWhereID = 0; long lWhatID = 0; CComBSTR bstrName; CString strName; IThing *pUser = NULL; long lIcon = 0; HTREEITEM hItem = NULL; HTREEITEM hSubItem = NULL; UINT uState = 0; pWhat = varArg; if (!pWhat) goto exit; if (FAILED(hr = m_pWorld->get_User(&pUser))) goto exit; if (pWhat != pUser) { if (FAILED(hr = pthing->get_ID(&lWhereID))) goto exit; if(lWhereID > 0) { // first find the thing that contains what is entering hItem = InTree(m_hRoom, lWhereID); hSubItem = AddThingToTree(hItem, pWhat); if (hSubItem) { AddContentsToTree(hSubItem, pWhat); } m_treeCtrl.SortChildren(hItem); } } else { // this is me entering a new room, fill in the room part of the tree // again FillRoomNode(); // Select the room if (m_hRoom) m_treeCtrl.SelectItem(m_hRoom); } exit: SAFERELEASE(pUser); return hr; }
int main(void) { Node *top; int i; assert(scanf("%d", &i) == 1); top = (Node *)malloc(sizeof(Node)); top->num = i; while(scanf("%d", &i) == 1) InsertBinTree(top, i); printf("%s\n", PrintTree(top)); while(scanf("%d", &i) == 1) printf("%d is%s in Tree\n", i, InTree(top, i) ? "" : " not"); return 0; }
HRESULT CThingTreeCtrl::OnLeave(IThing *pthing, CComVariant varArg) { // Get the id of the thing that's leaving the inventory (pWhat) and of its parent (pWhere) // Search the tree for these. It should be in the tree someplace // remove from the tree. Shouldnt need to update anything else long lWhatID = 0; HRESULT hr = S_OK; HTREEITEM hItem = NULL; CThingPtr pWhat; IThing *pUser = NULL; pWhat = varArg; if (!pWhat) goto exit; if (FAILED(hr = m_pWorld->get_User(&pUser))) goto exit; if (pWhat != pUser) { if (FAILED(hr = pWhat->get_ID(&lWhatID))) goto exit; if(lWhatID > 0) { // first find the thing that is leaving hItem = InTree(m_hRoom, lWhatID); if(hItem != NULL) { // remove from the tree m_treeCtrl.DeleteItem(hItem); hr = S_OK; } } } else { // hey, I'm leaving! Delete the room node and wait for the // enter message to fill it back in m_treeCtrl.DeleteItem(m_hRoom); m_hRoom = NULL; } exit: SAFERELEASE(pUser); return hr; }
void findpet(const Tree *pt){ Item temp; if(TreeIsEmpty(pt)){ puts("No Entries !"); return ; //如果树为空,则退出函数 } puts("Please enter name of pet you wish to find :"); fgets(temp.petname,MAXN,stdin); puts("Please enter pet kind:"); fgets(temp.petkind,MAXN,stdin); printf("%s the %s ",temp.petname,temp.petkind); if(InTree(&temp,pt)){ printf("is a member.\n"); }else{ printf("is not a member.\n"); } }
void findpet(const Tree * pt) { Item temp; if (TreeIsEmpty(pt)) { puts("No entries!"); return; /* quit function if tree is empty */ } puts("Please enter name of pet you wish to find:"); gets(temp.petname); puts("Please enter pet kind:"); gets(temp.petkind); uppercase(temp.petname); uppercase(temp.petkind); printf("%s the %s ", temp.petname, temp.petkind); if (InTree(&temp, pt)) printf("is a member.\n"); else printf("is not a member.\n"); }
void findpet(const Tree *pt){ Item temp; if(TreeIsEmpty(pt)){ puts("No enteries"); return; //此语句使函数退出 } puts("Please enter the name of pet you wish to find:"); gets(temp.petname); puts("please enter pet kind"); gets(temp.petkind); uppercase(temp.petname); uppercase(temp.petkind); printf("%s the %s ",temp.petname ,temp.petkind ); if(InTree(&temp,pt)){ printf("is a member.\n"); } else{ printf("is not a member.\n"); } }
HRESULT CThingTreeCtrl::NameChanged(IThing *pthing) { long lID = 0; HTREEITEM hItem = NULL; HTREEITEM hParentItem = NULL; HRESULT hr = S_OK; CComBSTR bstrName; CString strName; // we should update the item in question hr = pthing->get_ID(&lID); // now lets go find the thing with the different name if (lID > 0) { // first find the thing that is leaving hItem = InTree(m_hRoom, lID); if(hItem != NULL) { // get the new name from the thing and put it in the tree hr = pthing->get_Name(&bstrName.m_str); strName = bstrName; m_treeCtrl.SetItemText(hItem, strName); // get the parent item hParentItem = m_treeCtrl.GetParentItem( hItem ); // sort m_treeCtrl.SortChildren(hParentItem); hr = S_OK; } } return hr; }
// // CVWUIView Events // HRESULT CThingTreeCtrl::HandleUIUpdate( enumVWUIEventTypes enumEventType, IVWUIView* pvwSrc,VARIANT varHint ) { HRESULT hr = S_OK; IThing* pCurrentThing = NULL; long lCurrentID = 0; IDispatch* pDisp = NULL; HTREEITEM htItem = NULL; long lID = 0; if (enumEventType != VWUIT_ADDSELECTION) return S_OK; // if we put this selection in, dont do anything if (pvwSrc == m_pVWUIView) { m_bSelectedByMe = TRUE; goto EXIT; } // otherwise, look for this selection in the current tree // and if it is there, select it // first get the id of the current thing if(varHint.vt == VT_DISPATCH) { pDisp = varHint.pdispVal; if(pDisp) { hr = pDisp->QueryInterface(IID_IThing, (void**)&pCurrentThing); if(pCurrentThing) { hr = pCurrentThing->get_ID(&lCurrentID); if(FAILED(hr)) goto EXIT; // Search for the htreeitem that corresponds these ids htItem = InTree(m_hRoom, lCurrentID); // if we found the item, select it and exit if(htItem) { // TODO - Make sure this doesn't send a TVN_SELCHANGED notification!!!! // It does, so we have to regulate it with a bool m_bSelectedByMe = FALSE; // ha! check to see if it's selected, because if it is already // we won't get the selection changed message to turn on m_bSelectedByMe! UINT bits; bits = m_treeCtrl.GetItemState(htItem, TVIS_SELECTED); if (!(bits & TVIS_SELECTED)) m_treeCtrl.SelectItem(htItem); else m_bSelectedByMe = TRUE; goto EXIT; } // if its not in the tree, walk its location and insert it into the tree and select it } } } EXIT: SAFERELEASE(pCurrentThing); return S_OK; }