From aa190d4fe3dafa708f440e6bf92a7040f47269d4 Mon Sep 17 00:00:00 2001 From: Ratna Priya Date: Tue, 1 Oct 2019 11:36:24 +0530 Subject: [PATCH 1/6] Identical Tree Finding This Python file finds the pair of identical tree in the given input. --- Trees/identical_tree.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Trees/identical_tree.py diff --git a/Trees/identical_tree.py b/Trees/identical_tree.py new file mode 100644 index 00000000..d05331a7 --- /dev/null +++ b/Trees/identical_tree.py @@ -0,0 +1,32 @@ +# to determine if two trees are identical + +from tree_utils import TreeNode + + +def identical_trees(root1, root2): + if not root1 and not root2: + return True + if root1 and root2: + return (root1.data == root2.data) and \ + identical_trees(root1.left, root2.left) and \ + identical_trees(root1.right, root2.right) + return False + + +def test(): + root1 = TreeNode(1) + root2 = TreeNode(1) + root1.left = TreeNode(2) + root1.right = TreeNode(3) + root1.left.left = TreeNode(4) + root1.left.right = TreeNode(5) + + root2.left = TreeNode(2) + root2.right = TreeNode(3) + root2.left.left = TreeNode(4) + root2.left.right = TreeNode(5) + + if identical_trees(root1, root2): + print "Both trees are identical" + else: + print "Trees are not identical" From 740746a0abe92afc726780b2f68a626d29b5f2ce Mon Sep 17 00:00:00 2001 From: Ratna Priya Date: Tue, 1 Oct 2019 11:39:18 +0530 Subject: [PATCH 2/6] Python Script containing basic structure of tree This Python Script containing the basic structure of a tree making in Data Structure. --- Trees/basic_structure_tree.py | 131 ++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 Trees/basic_structure_tree.py diff --git a/Trees/basic_structure_tree.py b/Trees/basic_structure_tree.py new file mode 100644 index 00000000..2ad5d508 --- /dev/null +++ b/Trees/basic_structure_tree.py @@ -0,0 +1,131 @@ +# This file contains the basic structure of tree + +import sys +from tree_utils import * + + +# Finding maximum element in a binary tree +def find_max(root): + max_val = -sys.maxint -1 + if root: + root_val = root.data + left_val = find_max(root.left) + right_val = find_max(root.right) + if left_val > right_val: + max_val = left_val + else: + max_val = right_val + if max_val < root_val: + max_val = root_val + return max_val + + +# Finding an element in Binary Tree +def find_element(root, data): + if root: + if root.data == data: + return 1 + else: + temp = find_element(root.left, data) + if temp != 0: + return temp + else: + return find_element(root.right, data) + return 0 + + +# Finding size of tree : Number of nodes in tree +def size_of_tree(root): + if not root: + return 0 + else: + left_size = size_of_tree(root.left) + right_size = size_of_tree(root.right) + return left_size + 1 + right_size + + +# Printing level order data in reverse order : 1 2 3 4 5 6 7 -> 4 5 6 7 2 3 1 +def reverse_level_order(root): + level_queue = list() + level_stack = list() + level_queue.append(root) + while level_queue: + curr_node = level_queue.pop(0) + if curr_node.right: + level_queue.append(curr_node.right) + if curr_node.left: + level_queue.append(curr_node.left) + level_stack.append(curr_node) + while level_stack: + curr_node = level_stack.pop() + print curr_node.data, + + +# LeftView of Tree +def left_view_of_tree(root): + if root: + print root.data + if root.left: + left_view_of_tree(root.left) + else: + left_view_of_tree(root.right) + + +# Height of Tree +def height_of_tree(root): + if not root: + return -1 + else: + left_height = height_of_tree(root.left) + right_height = height_of_tree(root.right) + if left_height > right_height: + max_height = left_height + 1 + else: + max_height = right_height + 1 + return max_height + + +# Bottom View of Tree +def bottom_view(root): + if root: + bottom_view(root.left) + if not root.left and not root.right: + print root.data + bottom_view(root.right) + + +# Leaf Node Count +def get_leaf_node_count(root): + if not root: + return 0 + + if root.left is None and root.right is None: + return 1 + else: + return get_leaf_node_count(root.left) + get_leaf_node_count(root.right) + +def test(): + # l1 = TreeNode(4, None, None) + # l2 = TreeNode(5, None, None) + # l3 = TreeNode(6, None, None) + # l4 = TreeNode(7, None, None) + # l11 = TreeNode(2, l1, l2) + # l21 = TreeNode(3, l3, l4) + # root = TreeNode(1, l11, l21) + + root = TreeNode(3) + root.left = TreeNode(2) + root.right = TreeNode(5) + root.left.left = TreeNode(1) + root.left.right = TreeNode(4) + + # print "find_element:", find_element(root, 6) + # print "size_of_tree", size_of_tree(root) + # print "reverse_level_order: " + # reverse_level_order(root) + # print "left_view: " + # left_view_of_tree(root) + # print "height: " + # print height_of_tree(l1) + bottom_view(root) + print "Leaf:",get_leaf_node_count(root) From 563f735c3a9dc897084078bca73ac9f600e57a8d Mon Sep 17 00:00:00 2001 From: Ratna Priya Date: Tue, 1 Oct 2019 22:32:40 +0530 Subject: [PATCH 3/6] Rename Trees/basic_structure_tree.py to Data_Structure/tree/basic_structure_tree.py --- {Trees => Data_Structure/tree}/basic_structure_tree.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Trees => Data_Structure/tree}/basic_structure_tree.py (100%) diff --git a/Trees/basic_structure_tree.py b/Data_Structure/tree/basic_structure_tree.py similarity index 100% rename from Trees/basic_structure_tree.py rename to Data_Structure/tree/basic_structure_tree.py From f4b1edfc2fbb578fecb0be20bb4f8336d5cd0d80 Mon Sep 17 00:00:00 2001 From: Ratna Priya Date: Tue, 1 Oct 2019 22:33:35 +0530 Subject: [PATCH 4/6] Rename Data_Structure/tree/basic_structure_tree.py to Data_Structure/basic_structure_tree.py --- Data_Structure/{tree => }/basic_structure_tree.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Data_Structure/{tree => }/basic_structure_tree.py (100%) diff --git a/Data_Structure/tree/basic_structure_tree.py b/Data_Structure/basic_structure_tree.py similarity index 100% rename from Data_Structure/tree/basic_structure_tree.py rename to Data_Structure/basic_structure_tree.py From 2f4415fd86ee174d3480ce9d5769f1365678bff1 Mon Sep 17 00:00:00 2001 From: Ratna Priya Date: Tue, 1 Oct 2019 22:34:06 +0530 Subject: [PATCH 5/6] Rename Data_Structure/basic_structure_tree.py to Data_Structure/src/basic_structure_tree.py --- Data_Structure/{ => src}/basic_structure_tree.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Data_Structure/{ => src}/basic_structure_tree.py (100%) diff --git a/Data_Structure/basic_structure_tree.py b/Data_Structure/src/basic_structure_tree.py similarity index 100% rename from Data_Structure/basic_structure_tree.py rename to Data_Structure/src/basic_structure_tree.py From 9b153b00f705e34af0b8a20695014963355d1139 Mon Sep 17 00:00:00 2001 From: Ratna Priya Date: Tue, 1 Oct 2019 22:34:48 +0530 Subject: [PATCH 6/6] Rename Trees/identical_tree.py to Data_Structure/src/identical_tree.py --- {Trees => Data_Structure/src}/identical_tree.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Trees => Data_Structure/src}/identical_tree.py (100%) diff --git a/Trees/identical_tree.py b/Data_Structure/src/identical_tree.py similarity index 100% rename from Trees/identical_tree.py rename to Data_Structure/src/identical_tree.py