Question

In: Computer Science

Question 1: Processing Compound Data: Binary Trees A binary tree is a tuple with the following...

Question 1:

Processing Compound Data: Binary Trees

A binary tree is a tuple with the following recursive structure  

("btree", [val, left_tree, right_tree])

where first part of the tuple is a string "btree" and second part of the tuple is a list in which val is the value at the root and left_tree and right_tree are the binary trees for the left and right children or

("btree",[]) for the empty tree.  

(A)

Implement a binary tree ADT.

Type Name Description

Create the following tree in python. Use this tree to test the above functions of the tree ADT.

(B)

Implement the functions preorder,inorder and postorder.

Function preorder takes a tree as an argument and returns a list with the root value being the first element in the list followed by the elements of the left tree and then right tree. Function inorder takes a tree as an argument and returns a list with the elements of the left tree, followed by the root value and then right tree.

Function postorder takes a tree as an argument and returns a list with the elements of the left tree, followed by the elements of the right tree and then root value.

For example

preorder(t1) => [7, 5, 11, 9]

inorder(t1) => [5, 7, 9, 11]

postorder(t1) =>[5, 9, 11, 7]

Solutions

Expert Solution

Python program to for tree traversals :

  

# A class that represents an individual node in a

# Binary Tree

class Node:

def __init__(self,key):

self.left = None

self.right = None

self.val = key

  

  

# A function to do inorder tree traversal

def printInorder(root):

  

if root:

  

# First recur on left child

printInorder(root.left)

  

# then print the data of node

print(root.val),

  

# now recur on right child

printInorder(root.right)

  

  

  

# A function to do postorder tree traversal

def printPostorder(root):

  

if root:

  

# First recur on left child

printPostorder(root.left)

  

# the recur on right child

printPostorder(root.right)

  

# now print the data of node

print(root.val),

  

  

# A function to do preorder tree traversal

def printPreorder(root):

  

if root:

  

# First print the data of node

print(root.val),

  

# Then recur on left child

printPreorder(root.left)

  

# Finally recur on right child

printPreorder(root.right)

  

  

#Drivercode

root = Node(1)

root.left = Node(2)

root.right = Node(3)

root.left.left = Node(4)

root.left.right = Node(5)

print ("Preorder traversal of binary tree is")

printPreorder(root)

  

print ("\nInorder traversal of binary tree is")

printInorder(root)

  

print ("\nPostorder traversal of binary tree is")

printPostorder(root)


Related Solutions

Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree...
Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree a) (Tree a) A tree is balanced if the number of leaves in the left and right subtree of every node differ by at most one. Write a Haskell function balanced that returns whether a tree is balanced or not. balanced :: Tree a -> Bool
Binary Search Trees with Lazy Deletion Implement binary search tree class with lazy deletion that has...
Binary Search Trees with Lazy Deletion Implement binary search tree class with lazy deletion that has TreeNode as nested class in Java. Design the class, TreeNode to have following class variables: int key; // All Keys are in the range 1 to 99 TreeNode leftChild; TreeNode rightChild; boolean deleted; Your program method must have routines to do the following operations. 1. insert //Should insert a new element to a leaf node. If new element is aduplicatethen do nothing. If the...
Data structure Give three distinct binary search trees for the following data:   1 2 3 4...
Data structure Give three distinct binary search trees for the following data:   1 2 3 4 5 6 7. One of these trees must have minimum height and another must-have maximum height. Draw the BSTs that result from each sequence of add operations. (2) a. 10, 20, 30, 40 b. 30, 20, 40, 10 c. 20, 10, 30, 40
Data structure Give three distinct binary search trees for the following data:   1 2 3 4...
Data structure Give three distinct binary search trees for the following data:   1 2 3 4 5 6 7. One of these trees must have minimum height and another must-have maximum height. Draw the BSTs that result from each sequence of add operations. (2) a. 10, 20, 30, 40 b. 30, 20, 40, 10 c. 20, 10, 30, 40
Develop a Java application to implement a binary tree data structure. A tree data structure starts...
Develop a Java application to implement a binary tree data structure. A tree data structure starts from the top node, called the root. Each node in the tree has a set of children, which are also nodes of the tree and can have children of their own, and so on. This keeps on going till we get to the bottom of the tree, to the “leaf” nodes. Each node in the tree, except for the root, has a parent. A...
Considering the following different search trees, binary search trees, AVL trees, red-black trees, and others. Compare...
Considering the following different search trees, binary search trees, AVL trees, red-black trees, and others. Compare their advantages and disadvantages, running times, etc
Subject: Homework - Family Trees please give me C codes, thanks FAMILY TREE In a Binary...
Subject: Homework - Family Trees please give me C codes, thanks FAMILY TREE In a Binary Tree, each node has two children at most. To eliminate the restriction by linking the children together to form a list. In this design, each node in the tree needs to contain only two pointers: one to its eldest child and one to its next younger sibling. Using this representation, in each node, the pointer on the left always points down to child; the...
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
Python question Define a function called max_value_length(tree) that takes a Binary Tree object as a parameter...
Python question Define a function called max_value_length(tree) that takes a Binary Tree object as a parameter and returns an integer that contains the longest character length of all the values in that tree. Use recursion. For example, the character length of value 123 is 3, because there are three digits. The character length of value “Ruru” is 4, because it has four characters. Further, the max_value_length() function would return 4 if the only node values in the tree were 123...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT