Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Tree Traversal and Node
// Example program using TreeNode #include <iostream> using namespace std; template<class ItemType> class TreeNode { private: ItemType item; // A data item TreeNode<ItemType>* leftChild; // Pointer to left child TreeNode<ItemType>* rightChild; // Pointer to left child public: TreeNode() {leftChild = nullptr; rightChild = nullptr;}; void setItem(const ItemType& anItem) {item = anItem;}; void setLeft(TreeNode<ItemType>* treeNodePtr) {leftChild = treeNodePtr;}; void setRight(TreeNode<ItemType>* treeNodePtr) {rightChild = treeNodePtr;}; ItemType getItem() const {return item;}; TreeNode<ItemType>* getLeft() const {return leftChild;}; TreeNode<ItemType>* getRight() const {return rightChild;}; }; // end TreeNode template<class ItemType> void treeSearch(TreeNode<ItemType> *node) { if (node == nullptr) { return; } if (node->getLeft() != nullptr) { cout << "("; } treeSearch(node->getLeft()); cout << node->getItem(); treeSearch(node->getRight()); if (node->getRight() != nullptr) { cout << ")"; } // Post-Traversal Order: cout << node->getItem() << " "; } int main() { cout << "*** Tree Example Program for ECE 2574 ***\n\n"; TreeNode<char> node0; node0.setItem('*'); TreeNode<char> node10; node10.setItem('+'); node0.setLeft(&node10); TreeNode<char> node11; node11.setItem('-'); node0.setRight(&node11); TreeNode<char> node20; node20.setItem('a'); node10.setLeft(&node20); TreeNode<char> node21; node21.setItem('b'); node10.setRight(&node21); TreeNode<char> node22; node22.setItem('c'); node11.setLeft(&node22); TreeNode<char> node23; node23.setItem('d'); node11.setRight(&node23); // set root and search tree TreeNode<char> *root = &node0; treeSearch(root); cout << endl; return 0; }
run
|
edit
|
history
|
help
0
Recursive Enumeration Example
Assertion Divide by Zero Example
BinaryGap, C++ - Find longest sequence of zeros in binary representation of an integer.
Own initialization
STL stack
001
Forgetting to check end
C++ Template
DESim Example
<string> No indirect include of <errno.h>