Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Tree Traversal and Node
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
// 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; }
clang++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.73 sec, absolute running time: 0.21 sec, cpu time: 0.13 sec, memory peak: 3 Mb, absolute service time: 0,94 sec
edit mode
|
history
|
discussion
*** Tree Example Program for ECE 2574 *** a b + c d - *