0

I need some working snippets on C++ code regarding breadth/depth first searches. Also, in the links below, when using the term tree, is it in reference to a binary tree or more specefically a red and black tree? Or is this a more abstract tree of sorts? Does anyone have a link to working code for these searches...along with constructing the tree?

Tree seems to refer to some sort of constuct with in the "graph"? I believe this is some sort of math I have not taken yet.

breadth or depth first search 1

breadth or depth first search 2

Community
  • 1
  • 1
  • You may like to read the [Wikipedia article](http://en.wikipedia.org/wiki/Tree_(data_structure)) (also [this one](http://en.wikipedia.org/wiki/Tree_(graph_theory))) if the fundamentals are new to you. – Kerrek SB Oct 13 '11 at 01:04
  • In order from general to specefic -> Graph, Tree, Binary Tree, Red/Black tree. Or a tree is a type of graph, a binary tree is a type of tree, a red and black tree is a type of binary tree –  Oct 13 '11 at 01:12

3 Answers3

2

The tree in question is the thing they're searching. It's kinda hard to understand search algorithms without knowing what it is they are searching through.

A tree is a type of graph. A graph is a series of nodes (which presumably represent some data) with connections between certain nodes. A tree is a graph where the connections between nodes form a hierarchy. For any given node in the graph, it has exactly one "parent" that points to it, and it points to zero or more child nodes. And the nodes cannot form circles; a parent cannot point to a child who points to that parent.

Basically, like branches on a tree.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
0

The term "tree" refers to any data structure that can be abstractly looked at as a tree.

A "tree" is a data structure in which there are parent nodes and child nodes, and each child has a single parent, with a single "root" node not having a parent.

If a node in your tree has multiple parents, it is called a "graph".

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • Hm, that's maybe too specific. A tree should be *any* contractible graph, and any node is potentially a root node. It just depends on the specific implementation if who's a parent and who's a child. – Kerrek SB Oct 13 '11 at 01:17
  • 1
    @KerrekSB, if the two definitions produce isomorphic concepts of 'trees', does it matter? – bdonlan Oct 13 '11 at 03:38
0

A tree is a special case of a directed acyclic graph (basically a bunch of 'nodes' with arrows ('edges') pointing at each other, such that there cannot be a loop of arrows) in which the following two conditions hold:

  • No node has more than one incoming edge
  • There exists a single distinguished node (the 'root') from which all other nodes are reachable.

The nodes reachable via an outgoing edge from some node N are often called N's children.

Breadth-first and depth-first search apply to generic trees (indeed, they apply to all DAGs). However there are some more specific types:

  • Binary trees are trees in which no node has more than two outgoing edges; outgoing edges are labelled, usually as 'left' and 'right'
  • Search trees are binary trees in which each node has a key; further, the key in some node N is greater than the child on its left edge (if any) and less than the child on its right edge (if any). This allows for very fast searching for a specific key.
  • Red-black trees are a specific kind of search tree in which a moderately complex algorithm is used to make sure all keys are approximately the same distance from the root.
bdonlan
  • 224,562
  • 31
  • 268
  • 324