Skip to content

Commit 86fbdb3

Browse files
Merge pull request #748 from JayRajM97/master
Added python and cpp code of Tree Sort
2 parents fa44cc9 + 6c80793 commit 86fbdb3

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

Sorting/TreeSort/C++/TreeSort.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <vector>
2+
#include <iostream>
3+
4+
using namespace std;
5+
struct Node
6+
{
7+
int data;
8+
struct Node *left, *right;
9+
};
10+
struct Node *newnode(int key)
11+
{
12+
struct Node *temp = new Node;
13+
temp->data = key;
14+
temp->left = NULL;
15+
temp->right = NULL;
16+
return temp;
17+
}
18+
19+
Node* insert(Node *node, int key)
20+
{
21+
if (node == NULL)
22+
return newnode(key); //If tree is empty return new node
23+
if (key < node->data)
24+
node->left = insert(node->left, key);
25+
else
26+
node->right = insert(node->right, key);
27+
return node;
28+
}
29+
void store(Node *root, int a[], int &i)
30+
{
31+
if (root != NULL)
32+
{
33+
store(root->left, a, i);
34+
a[i++] = root->data;
35+
store(root->right, a, i);
36+
}
37+
}
38+
void TreeSort(vector<int>& a)
39+
{
40+
struct Node *root = NULL;
41+
42+
//Construct binary search tree
43+
root = insert(root, a[0]);
44+
for (size_t i = 1; i < a.size(); i++)
45+
insert(root, a[i]);
46+
47+
//Sorting the array using inorder traversal on BST
48+
int i = 0;
49+
store(root, a.data(), i);
50+
}
51+
52+
int main()
53+
{
54+
55+
vector<int> a{1, 6, 8, 3, 10, 2, 12};
56+
57+
TreeSort(a);
58+
59+
cout << "The sorted array is :\n";
60+
//Printing the sorted array
61+
for (size_t i = 0; i < a.size(); i++)
62+
cout << a[i] << " ";
63+
64+
return 0;
65+
}

Sorting/TreeSort/Python/TreeSort.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Node:
2+
"""Base class to contain the tree metadata per instance of the class"""
3+
4+
def __init__(self, val):
5+
self.l_child = None
6+
self.r_child = None
7+
self.data = val
8+
9+
10+
def binary_insert(root, node):
11+
"""This recursive function will insert the objects into the tree"""
12+
if root is None: # If we don't have a root node
13+
root = node # Set the root node equal to the first node argument that was provided
14+
else: # We already have a root
15+
if root.data > node.data: # If our current root is bigger than the node we are about to insert
16+
if root.l_child is None: # If we don't have any node to the left of root
17+
root.l_child = node # Insert the node as the left node under root
18+
else: # There's already a node to the left of root
19+
binary_insert(
20+
root.l_child, node
21+
) # Call the insert function recursively with the left node value as the
22+
# temp root for comparison
23+
else: # Node to be inserted is bigger than root (going right side)
24+
if root.r_child is None: # if there's no right child
25+
root.r_child = node # insert the node as the right child of root (under)
26+
else: #
27+
binary_insert(
28+
root.r_child, node
29+
) # Call the insert function recursively with the right node value as the
30+
# temp root for comparison
31+
32+
33+
def post_sort_print(root):
34+
if not root:
35+
return None
36+
post_sort_print(root.l_child)
37+
print(root.data)
38+
post_sort_print(root.r_child)
39+
40+
41+
def pre_sort_print(root):
42+
if not root:
43+
return None
44+
print(root.data)
45+
pre_sort_print(root.l_child)
46+
pre_sort_print(root.r_child)
47+
48+
49+
r = Node(6)
50+
binary_insert(r, Node(2))
51+
binary_insert(r, Node(8))
52+
binary_insert(r, Node(90))
53+
binary_insert(r, Node(23))
54+
binary_insert(r, Node(12))
55+
binary_insert(r, Node(91))
56+
57+
print("---------")
58+
print("PRE SORT")
59+
pre_sort_print(r)
60+
print("---------")
61+
print("POST SORT")
62+
post_sort_print(r)

0 commit comments

Comments
 (0)