Provide python implementation of a spiral order tree traversal
In spiral order tree traversal the elements are printed in a spiral order.
For example, the tree given below prints 1 2 3 4 5 6 7 on spiral order traversal.
The python code for implementation:
# Python program for recursive level order
# traversal in spiral form
class newNode:
# Construct to create a newNode
def __init__(self, key):
self.data = key
self.left = None
self.right = None
""" Function to print spiral traversal of a tree"""
def printspiral(root):
h = height(root)
"""ltr Left to Right. If this variable
is set, then the given level is traversed
from left to right. """
ltr = False
for i in range(1, h + 1):
printGivenLevel(root, i, ltr)
"""Revert ltr to traverse next
level
in opposite order"""
ltr = not ltr
""" Print nodes at a given level """
def printGivenLevel(root, level, ltr):
if(root == None):
return
if(level == 1):
print(root.data, end = " ")
elif (level > 1):
if(ltr):
printGivenLevel(root.left, level - 1, ltr)
printGivenLevel(root.right, level - 1, ltr)
else:
printGivenLevel(root.right, level - 1, ltr)
printGivenLevel(root.left, level - 1, ltr)
""" Compute the "height" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node."""
def height(node):
if (node == None):
return 0
else:
""" compute the height of each
subtree """
lheight = height(node.left)
rheight = height(node.right)
""" use the larger one """
if (lheight > rheight):
return(lheight +
1)
else:
return(rheight +
1)
# Driver Code
if __name__ == '__main__':
root = newNode(1)
root.left = newNode(2)
root.right = newNode(3)
root.left.left = newNode(7)
root.left.right = newNode(6)
root.right.left = newNode(5)
root.right.right = newNode(4)
print("Spiral Order traversal of binary tree
is")
printspiral(root)
NOTE: while executing the code make sure about intendation (spaces) otherwise ypu may get error.
I written this code by following intendation rules only. But there is a chance of getting violated the rules of intendation while you are copying into some other editor to execute.
The in-order traversal of a binary tree processes the nodes in the order ABCD. The post-order traversal of the same binary tree processes the nodes in the order ACDB. Draw the tree below.
(Pre-order and post-order traversal). Implement the textbook's algorithm (not the Java implementation) for pre-order and post-order traversal. To "process" or "visit" a node means to print the data at that node. Pre-order traversal output: 10 6 4 8 18 15 21 Post-order traversal output: 4 8 6 15 21 18 10 Additionally, create and traverse the following trees: Algorithm preorder(p) perform the "visit" action for position p this happens before any recursion for each child c in children(p) do recursively...
Recall that an in-order traversal of a BinarySearch Tree will visit all the nodes in sorted (based on the sorted order of the data). Given the following Node and method definition for a BinarySearch Tree, write a method that returns the Node that follows the Node w in an in-order traversal of the tree, assuming that w.right is nil (n.b., this implies that the Node that follows w must be an ancestor of w, not a descendant). public class Node<T...
[Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line...
3. Show the pre-order traversal of this red black tree while showing the color of each node in the pre-order traversal.
I need help on question 12 please An inorder tree traversal of a binary search tree produces a listing of the tree nodes in alphabetical or numerical order. Construct a binary search tree for "To be or not to be, that is the question, " and then do an inorder traversal. Construct a binary search tree for "In the high and far off times the Elephant, O Best Beloved, had no trunk, " and then do an inorder traversal.
JAVA Given the sequence of in-order traversal for a general binary tree, stored in an array inOrder[] = {9, 5, 1, 7, 2, 12, 8, 4, 3, 11 }. And given the sequence of post-order traversal for the same tree, stored in an array postOrder[] = {9, 1, 2, 12, 7, 5, 3, 11, 4, 8}. Please implement the following method: static BinaryTree buildTree(Object inOrder[], Object postOrder[]) -The method buildTree() returns a BinaryTree object in memory that is constructed on...
Assume you are given “preorder” and “inorder” traversal result of a Binary Tree. Write an algorithm (pseudocode) that constructs the Binary Tree. For example, you can start with the Pre-Order and In-Order traversal of the same tree given below. Pre-Order = 80, 50, 10, 70, 100 In-Order = 10, 50, 70, 80, 100
Write a python function that takes a binary tree as its only parameter. Return the value stored in the shallowest leaf, anywhere in the tree. If there is a tie, then return the "leftmost" of the leaves (that is, the one that you would encounter first in an in-order traversal). You are allowed (and encouraged) to use a helper function. You may assume that the tree nodes have public fields left,right. The code cannot import anything from the Python standard...
Consider the __init__ constructor method for the Python implementation of a node within a binary tree. The binary tree will aim to require minimal space by having only one tree node per search key. How would this constructor method differ between a Set and a Map? How might this constructor method differ between a Map and a Multi-map? How might this constructor method differ between a Set and a Multi-Set?