0% found this document useful (0 votes)
18 views2 pages

Trees: Sample Implementations For Tree Traversal

A tree is a data structure where one node is designated as the root, and remaining nodes are partitioned into subtrees of the root. Tree nodes have properties like depth, height, and whether they are leaf nodes. There are different traversal methods for binary trees like preorder, inorder, and postorder that recursively visit nodes in a specific order - preorder visits the root first, then left subtree, then right subtree, inorder visits left subtree, then root, then right subtree, and postorder visits left subtree, right subtree, then root.

Uploaded by

afifa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Trees: Sample Implementations For Tree Traversal

A tree is a data structure where one node is designated as the root, and remaining nodes are partitioned into subtrees of the root. Tree nodes have properties like depth, height, and whether they are leaf nodes. There are different traversal methods for binary trees like preorder, inorder, and postorder that recursively visit nodes in a specific order - preorder visits the root first, then left subtree, then right subtree, inorder visits left subtree, then root, then right subtree, and postorder visits left subtree, right subtree, then root.

Uploaded by

afifa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Trees

A tree is a non-empty set one element of which is designated the root of the tree while the remaining
elements are partitioned into non-empty sets each of which is a subtree of the root.
Tree nodes have many useful properties. The depth of a node is the length of the path (or the
number of edges) from the root to that node. The height of a node is the longest path from that node
to its leaves. The height of a tree is the height of the root. A leaf node has no children -- its only path
is up to its parent.
Many problems require we visit the nodes of a tree in a systematic way: tasks such as counting how
many nodes exist or finding the maximum element. Three different methods are possible for binary
trees: preorder, postorder, and in-order, which all do the same three things: recursively traverse both
the left and right subtrees and visit the current node. The difference is when the algorithm visits the
current node:
preorder: Current node, left subtree, right subtree (DLR)
postorder: Left subtree, right subtree, current node (LRD)
in-order: Left subtree, current node, right subtree (LDR)
levelorder: Level by level, from left to right, starting from the root node.
* Visit means performing some operation involving the current node of a tree, like incrementing a
counter or checking if the value of the current node is greater than any other recorded.

Sample implementations for Tree Traversal[edit]


preorder(node)
visit(node)
if node.left
if node.right
inorder(node)
if node.left
visit(node)
if node.right
postorder(node)
if node.left
if node.right
visit(node)

null then preorder(node.left)


null then preorder(node.right)
null then inorder(node.left)
null then inorder(node.right)
null then postorder(node.left)
null then postorder(node.right)

Examples of Tree Traversals[edit]

preorder: 50,30, 20, 40, 90, 100


inorder: 20,30,40,50, 90, 100
postorder: 20,40,30,100,90,50

You might also like