In a graph-theoretic sense, a tree is a connected, undirected, acyclic graph. In a data structure sense, a tree is accessed beginning at a node distinguished as the root. Every node in the tree is either a leaf (a node with no children/descendants) or an interior node.
In a formal sense, a tree is either
A binary tree is a tree in which each node has at most 2 children. A binary search tree (BST) is a binary tree in which the values stored in the left subtree are less than the value stored at the node and the values stored in the right subtree are greater than the value stored at the node. A BST has search performance of O(log n) assuming it's balanced.
Other terms to be aware of are
Some interesting things to consider:
The code for "standard" binary tree operations in C is not that
different from what you'd write in Java. I've provided code that
implements most of the standard ops in b-tree-code.c. I encourage you to take a look
at the printTree
function which makes use of a
non-standard traversal of the tree to print a "reasonable"
approximation of the tree rotated 90 degrees.
There are a number of operators in C that operate on the bit representation of your data. Many have names that are similar to logical operators, but remember that bit operators operate on bits, not booleans-implemented-as-integers! In particular, the bit operators we will examine are:
A bit-wise NOT or complement is a unary operation which performs logical negation on each bit. 0 digits become 1, and vice-versa. For example:
NOT 0111 = 1000
In C, the NOT operator is "~" (tilde). For example:
x = ~y;
assigns x the result of "NOT y". This is different from the C logical "not" operator,
"!" (exclamation point), which treats the entire numeral as a single Boolean value.
For example:
x = !y;
assigns x a Boolean value of "true" if y is "false", or "false" if y is "true".
In C, a numerical value is interpreted as "true" if it is non-zero.
The logical "not" is not considered a bit-wise operation, since it
does not operate at the bit level.
Bit-wise NOT is useful in finding the one's complement of a binary numeral.
A bit-wise OR takes two bit patterns of equal length, and produces another one of the same length by matching up corresponding bits (the first of each, the second of each, and so on) and performing the logical OR operation on each pair of corresponding bits. In each pair, the result is 1 if either (or both) of the corresponding bits is 1. Otherwise, the result is zero. For example:
0101 OR 0011 = 0111
In C, the bit-wise OR operator is "|" (vertical bar). For example:
x = y | z;
assigns x the result of "y OR z". This is different from the C logical
"or" operator, "||" (two vertical bars), which treats its operands as Boolean values,
and returns "true" (non-zero) or "false" (zero).
The bit-wise OR may be used in situations where a set of bits are used as flags. The bits in a single binary numeral may each represent a distinct Boolean flag. Applying the bit-wise OR operation to the numeral along with a bit pattern containing 1 in some positions will result in a new numeral with those bits set. For example:
0010
can be considered as a set of four flags. The first, second, and fourth flags are not set (0); the third flag is set (1). The first flag may be set by applying the bit-wise OR to this value, along with another value in which only the first flag is set:
0010 OR 1000 = 1010
This technique may be employed by programmers who are working under restrictions of memory; one bit pattern can represent the states of several independent variables at once.
A bit-wise AND takes two bit patterns of equal length and performs the logical AND on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is zero. For example:
0101 AND 0011 = 0001
In C, the bit-wise AND operator is "&" (ampersand). For example:
x = y & z;
assigns x the result of "y AND z". This is different from the C logical "and"
operator, "&&", which takes two logical operands as input and produces a result of "true" (non-zero)
or "false" (zero).
The bit-wise AND may be used to clear particular bits in a pattern or perform a bit mask operation. A masking operation may be used to isolate part of a string of bits, or to determine whether a particular bit is 1 or 0. For example, given a bit pattern:
0101
To determine whether the third bit is 1, a bit-wise AND is applied to it along with another bit pattern containing 1 in the third bit, and 0 in all other bits:
0101 AND 0010 = 0000
Since the result is zero, the third bit in the original pattern was 0. Using bit-wise AND in this manner is called bit masking, by analogy to the use of masking tape to cover, or mask, portions that should not be altered, or that are not of interest. In this case, the 0 values in the masking operand mask the bits that are not of concern (all but the third bit).
A bit-wise XOR takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if the corresponding bits are different, and 0 if they are the same. For example:
0101 XOR 0011 = 0110
In C, the bit-wise XOR operator is "^" (circumflex). For example:
x = y ^ z;
assigns x the result of "y XOR z".
Assembly language programmers sometimes use the XOR operation as a short-cut to set the value of a register to zero. On many architectures, the XOR operation requires fewer CPU clock cycles than the sequence of operations that may be required to load a zero value and save it to the register. Using a given value as input to both sides of the bit-wise XOR operation always results in an output of zero; by XORing a register with itself, that register can be easily set to zero.
The bit-wise XOR may also be used to toggle (invert) flags in a set of bits. Given a bit pattern:
0010
The first and third bits may be toggled simultaneously by a bit-wise XOR with another bit pattern containing 1 in the first and third positions:
0010 XOR 1010 = 1000
Standard swapping algorithms require the use of temporary storage. Here is one such algorithm to swap x and y:
Copy the value of y to temporary storage: temp = y
Assign y to get the value of x: y = x
Assign x to get the temporary storage value: x = temp
If the two variables x and y are of type integer, an arithmetic algorithm to swap them is as follows:
x = x + y; y = x - y; x = x - y;
The above algorithm breaks down on systems that trap integer overflow.
Also, when x and y are aliased to the same storage location the result is
to zero out that location. Using the XOR swap algorithm, however,
neither temporary storage nor overflow detection are needed. However,
the problem still remains that if x and y use the same storage location,
the values will be zeroed out. The algorithm is as follows:
x = x XOR y; y = x XOR y; x = x XOR y;
This algorithm typically corresponds to three machine code instructions and thus is particularly attractive to assembly language programmers due to its performance and efficiency. It eliminates the use of an intermediate register, which is a limited resource in assembly language programming. It also eliminates two memory access cycles, which are expensive compared to a register operation.
For example, let's say we have two values X = 12 and Y = 10. In binary, we have
X = 1 1 0 0 Y = 1 0 1 0 Now, we XOR X and Y to get 0 1 1 0 and store in X. We now have X = 0 1 1 0 Y = 1 0 1 0 XOR X and Y again to get 1 1 0 0 - store in Y. We now have X = 0 1 1 0 Y = 1 1 0 0 XOR X and Y again to get 1 0 1 0 - store in X. We ultimately have X = 1 0 1 0 Y = 1 1 0 0
The values are swapped, and the algorithm has worked (at least this time)!
In general, if we call the initial value of X = x and the initial value of Y = y, then performing the above steps (and remembering that a XOR a == 0 and b XOR 0 == b), yields:
x = x XOR y; X == x XOR y Y == y y = x XOR y; X == x XOR y Y == x XOR y XOR y == x x = x XOR y; X == x XOR y XOR x == y Y = x
/* C code to implement an xor swap: */ void xorSwap(int *x, int *y) { if (x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } }
The bit shift is sometimes considered a bit-wise operation, since it operates on a set of bits. Unlike the above, the bit shift operates on the entire bit-string, rather than on the individual bits. In this operation, the digits are moved, or shifted, to the left or right. Registers in a computer processor have a fixed number of available bits for storing numerals, so some bits may be shifted past the "end" of the register; the different kinds of shift typically differ in what they do with the bits that are shifted past the end.
For example, the number
0111 LEFT-SHIFT = 1110 0111 RIGHT-SHIFT = 0011
In the first case, the left-most 0 was shifted past the end of the register, and a 0 was put into the right-most position. In the second case, the rightmost 1 was shifted past the end (and is often in the carry flag though that can't usually be accessed in high level languages), and the sign bit, 0, was copied into the leftmost position.
You should ALWAYS use unsigned values as arguments to the shift operators. Consider what happens when you RIGHT shift an unsigned value - you are guaranteed to put a zero in the left most bit(s). No problem. But if you RIGHT shift a signed value you cannot be sure whether the sign bit will be replicated or whether a zero will be replicated. The behavior is platform-dependent and thus should be avoided by always shifting unsigned values.
In C, the left and right shift operators are "<<" and ">>", respectively. The number of places to shift is given as an argument to the shift operators. For example:
0111 LEFT-SHIFT-BY-TWO = 1100
x = y << 2;
assigns x the result of shifting y to the left by two digits.
NOTE: A left shift is equivalent to multiplying by two (provided the value does not overflow), while a right shift is equivalent to dividing by two and rounding down (i.e., x / 2).
There are a few ways to generate the power set (the set of all subsets) of a set: writing nested loops (awkward), using recursion (more elegant but still not the simplest), or using bitmaps (easier). Remember, the cardinality of the power set of a set with N elements is 2N. This should give you a bit of a hint as to how to generate the power set.
A bit map is an association we make between a bit (1/0 or TRUE/FALSE) and an element of a set. The association is simple: for every element in the set we assign one bit to correspond to one element of the set. The correspondence is defined as a 1 indicating inclusion into the subset, and a 0 meaning exclusion from the subset. If our set has N elements then we need N bits to map each set element. An example:
original set: { 12 21 13 31 14 41 15 51 16 61 17 71 18 81 77 34 } our bitmap: 1 0 1 1 0 1 1 1 1 0 1 1 0 0 1 0 the subset indicated: { 12 13 31 41 15 51 16 17 71 77 }
Notice that only where the associated bit is a 1, do we include that element in our subset. Bits that are 0 cause the exclusion of their associated set element.
The remaining question is: what tools does C give us to set up this mapping and generate all the possible subsets? C gives us the unsigned integer type (which we can think of as an array of bits), the bit-wise AND operator (the single ampersand & used as a binary operator - recall that the single ampersand & used as a unary operator is address-of). C also provides us with some bit-shifting operators: << and >>. We can use these tools to determine if any specific bit in an integer is a 1 or a 0.
As an example, if you wanted to take an integer and print out its binary bit pattern from left to right you could use a loop as illustrated by the following: