numbers greater than M. As optimization of the solution, try to avoid
repeating numbers in the queue. 11. Implement DoubleLinkedListNode
class, which has fields Previous, Next and Value. It will hold to hold a
single list node. Implement also DoubleLinked
(object.Equals(currentNode.Element, item) cfw_ break; prevNode =
currentNode; currentNode = currentNode.NextNode; currentIndex+;
if (currentNode != null) cfw_ 654 Fundamentals of Computer Programming
with C# / The element is found in the list -> remove
the next elements one after another, until we reach the end of the list or
find the searched element. We have two more operations to implement
accessing elements by index (using the indexer) and finding the count of
elements (through a property): / / Get
definition BinaryTree, which holds a value and left and right child nodes
which are of the same type BinaryTree. We have exactly two child nodes
(left and right) instead of list of children. The method PrintInOrder()
works recursively using the DFS algori
previous problem we substitute the queue with a stack, we are going to
get traversal in depth (DFS). 18. Use Breadth-First Search (BFS) by
starting from the position, marked with "*". Each unvisited adjacent to
the current cell we fill with the current nu
self-balancing binary search tree. We will explain what graph is, the
types of graphs, how to represent a graph in the memory (graph
implementation) and where graphs are used in our life and in the
computer technologies. We will see where in .NET Framewor
Chapter 16. Linear Data Structures 677 2 times2 4 times3 3
times4 8. The majorant of an array of size N is a value that occurs in it
at least N/2 + 1 times. Write a program that finds the majorant of given
array and prints it. If it does not exist, print
currentNode.Element = value; / / Gets the count of elements in the
list / public int Count cfw_ get cfw_ return this.count; The indexer works
pretty straightforward first checks the validity of the specified index
and then starts from the head of the
Otherwise append the element to the stack. Finally the majorant will be
in stack (if it exists). Why? Each time when we find any two different
elements, we discard both of them. And this operation keeps the
majorant the same and decreases the length of th
Trees In the previous section we discussed the basic structure of a tree.
In this section we will have a look at a specific type of tree binary tree.
This type of tree turns out to be very useful in programming. The
terminology for trees is also valid abo
(this.LeftChild != null) cfw_ this.LeftChild.PrintInOrder(); / 2. Visit the
root of this sub-tree Console.Write(this.Value + " "); / 3. Visit the right
child if (this.RightChild != null)
cfw_ this.RightChild.PrintInOrder(); / / Demonstrates how the
Bi
node.rightChild; / If the element to be deleted has one child if
(theChild != null) cfw_ theChild.parent = node.parent; / Handle the case
when the element is the root if (node.parent = null) cfw_ root = theChild;
else cfw_ 710 Fundamentals of Computer Pr
linked list (DoublyLinkedList) list, the elements of which have
pointers both to the next and the previous elements. Implement the
operations for adding, removing and searching for an element, as well as
inserting an element at a given index, retrieving a
shoppingList.RemoveAt(0); / Removes "Honey" (first)
shoppingList.RemoveAt(2); / Removes "Fruits" (last)
shoppingList.Add(null); shoppingList.Add("Beer");
shoppingList.Remove(null); Console.WriteLine("We need to buy:"); for
(int i = 0; i < shoppingList.Cou
method Equals() derived from System.Object. Bellow we give
implementations of the operations for searching and checking whether
the list contains a specified element: / Searches for given element in the
list / The item to be searched / / The index of the
considerably more complicated than adding: / Removes and returns
element on the specified index / / The index of the element to be
removed / / The removed element / / if the index is invalid public T
RemoveAt(int index) cfw_ if (index >= count | index < 0
node, conforms to the rule elements in the left sub-tree are less than the
root and the elements in the right sub-tree are bigger than the root. Here
is a sample implementation of this method. You should notice that in the
addition there is a reference to
IComparable<BinaryTreeNode> where T : IComparable cfw_ / Contains
the value of the node internal T value; / Contains the parent of the node
internal BinaryTreeNode parent; / Contains the left child of the node
internal BinaryTreeNode leftChild; / Contains
backward and some operations to be implemented more efficiently. Here
is how a sample doubly-linked list looks like: The ArrayList Class After
we got familiar with some of the basic implementations of the lists, we
are going to consider the classes in C#,
named with some numbers. An the figure we can see the root of the tree
"14", the left sub-tree (with root 19) and the right sub-tree (with root
15) and a right and left child "3" and "21". Chapter 17. Trees and
Graphs 695 We have to note that there is on
empty and for this reason we assign head = tail = null and count = 0. We
are going to implement all basic operations: adding and removing items,
as well as searching for an element and accessing the elements by index.
Lets start with the operation add (ap
prevNode) cfw_ count-; if (count = 0) cfw_ / The list becomes empty ->
remove head and tail this.head = null; this.tail = null; else if (prevNode
= null) cfw_ / The head node was removed -> update the head this.head =
node.NextNode; else cfw_ / Redirect
node. Ordered Binary Search Trees Example The next example shows
a simple implementation of a binary search tree. Our point is to suggest
methods for adding, searching and removing an element in the tree. For
every single operation from the above, we will
Sets. 7. Make a new array "occurrences" with size 1001. After that scan
through the list and for each number p increment the corresponding
value of its occurrences (occurrences[p]+). Thus, at each index, where
the value is not 0, we have an occurring numb
"); for (int i = 0; i < dynamicList.Count; i+) cfw_ Console.WriteLine(" - "
+ dynamicList[i]); The result of using the two types of lists is the
same: Array list: - Zero - Three Dynamic list: - Zero - Three The above
example demonstrates that certain AD
(compareTo > 0) cfw_ node.rightChild = Insert(value, node,
node.rightChild); return node; Searching for an Element Searching
in a binary search tree is an operation which is more intuitive. In the
sample code we have shown how the search of an element
so much information. Trees Terminology For a better understanding of
this part of the chapter we recommend to the reader at every step to draw
an analogy between the abstract meaning and its practical usage in
everyday life. We will simplify the figure de
elements by a given position (index) As we saw, one of the main
problems with this implementation is the resizing of the inner array
when adding and removing elements. In the ArrayList the problem is
solved by preliminarily created array (buffer), which g