You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
195 lines
4.9 KiB
Plaintext
195 lines
4.9 KiB
Plaintext
2 years ago
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "f9f2a93f-62ee-411a-a8dd-2a6745444c02",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# a sample tree, the starter program to begin all the trees with"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "c0e9adc4-5400-491d-a6bd-4ae93b4fbb38",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"\"\"Python3 program to demonstrate insert\n",
|
||
|
"operation in binary search tree \"\"\"\n",
|
||
|
"\n",
|
||
|
"# A Binary Tree Node\n",
|
||
|
"# Utility function to create a\n",
|
||
|
"# new tree node\n",
|
||
|
"class newNode:\n",
|
||
|
"\n",
|
||
|
" # Constructor to create a newNode\n",
|
||
|
" def __init__(self, data):\n",
|
||
|
" self.key= data\n",
|
||
|
" self.left = None\n",
|
||
|
" self.right = self.parent = None\n",
|
||
|
"\n",
|
||
|
"# A utility function to insert a new\n",
|
||
|
"# Node with given key in BST\n",
|
||
|
"def insert(root, key):\n",
|
||
|
"\n",
|
||
|
" # Create a new Node containing\n",
|
||
|
" # the new element\n",
|
||
|
" newnode = newNode(key)\n",
|
||
|
"\n",
|
||
|
" # Pointer to start traversing from root\n",
|
||
|
" # and traverses downward path to search\n",
|
||
|
" # where the new node to be inserted\n",
|
||
|
" x = root\n",
|
||
|
"\n",
|
||
|
" # Pointer y maintains the trailing\n",
|
||
|
" # pointer of x\n",
|
||
|
" y = None\n",
|
||
|
"\n",
|
||
|
" while (x != None):\n",
|
||
|
" y = x\n",
|
||
|
" if (key < x.key):\n",
|
||
|
" x = x.left\n",
|
||
|
" else:\n",
|
||
|
" x = x.right\n",
|
||
|
" \n",
|
||
|
" # If the root is None i.e the tree is\n",
|
||
|
" # empty. The new node is the root node\n",
|
||
|
" if (y == None):\n",
|
||
|
" y = newnode\n",
|
||
|
"\n",
|
||
|
" # If the new key is less then the leaf node key\n",
|
||
|
" # Assign the new node to be its left child\n",
|
||
|
" elif (key < y.key):\n",
|
||
|
" y.left = newnode\n",
|
||
|
"\n",
|
||
|
" # else assign the new node its\n",
|
||
|
" # right child\n",
|
||
|
" else:\n",
|
||
|
" y.right = newnode\n",
|
||
|
"\n",
|
||
|
" # Returns the pointer where the\n",
|
||
|
" # new node is inserted\n",
|
||
|
" return y\n",
|
||
|
"\n",
|
||
|
"# A utility function to do inorder\n",
|
||
|
"# traversal of BST\n",
|
||
|
"def Inorder(root) :\n",
|
||
|
"\n",
|
||
|
" if (root == None) :\n",
|
||
|
" return\n",
|
||
|
" else:\n",
|
||
|
" Inorder(root.left)\n",
|
||
|
" print( root.key, end = \" \" )\n",
|
||
|
" Inorder(root.right)\n",
|
||
|
"\n",
|
||
|
"#Driver Code\n",
|
||
|
"if __name__ == '__main__':\n",
|
||
|
"\n",
|
||
|
" root = None\n",
|
||
|
" root = insert(root, \"m\")\n",
|
||
|
" insert(root, \"n\")\n",
|
||
|
" insert(root, \"l\")\n",
|
||
|
" insert(root, \"p\")\n",
|
||
|
" insert(root, \"q\")\n",
|
||
|
" insert(root, \"a\")\n",
|
||
|
" insert(root, \"b\")\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
" # Pr inorder traversal of the BST\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"# This code is contributed by\n",
|
||
|
"# SHUBHAMSINGH10"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"id": "921f4017-3f1f-4085-8da8-cc850fbd4c6e",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"\n",
|
||
|
" q\n",
|
||
|
"\n",
|
||
|
" p\n",
|
||
|
"\n",
|
||
|
" n\n",
|
||
|
"\n",
|
||
|
"m\n",
|
||
|
"\n",
|
||
|
" l\n",
|
||
|
"\n",
|
||
|
" b\n",
|
||
|
"\n",
|
||
|
" a\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# print binary tree in 2D copied from sample program\n",
|
||
|
"COUNT = [10]\n",
|
||
|
"def print2DUtil(root, space) :\n",
|
||
|
"\n",
|
||
|
" # Base case\n",
|
||
|
" if (root == None) :\n",
|
||
|
" return\n",
|
||
|
"\n",
|
||
|
" # Increase distance between levels\n",
|
||
|
" space += COUNT[0]\n",
|
||
|
"\n",
|
||
|
" # Process right child first\n",
|
||
|
" print2DUtil(root.right, space)\n",
|
||
|
"\n",
|
||
|
" # Print current node after space\n",
|
||
|
" # count\n",
|
||
|
" print()\n",
|
||
|
" for i in range(COUNT[0], space):\n",
|
||
|
" print(end = \" \")\n",
|
||
|
" print(root.key)\n",
|
||
|
"\n",
|
||
|
" # Process left child\n",
|
||
|
" print2DUtil(root.left, space)\n",
|
||
|
"\n",
|
||
|
"# Wrapper over print2DUtil()\n",
|
||
|
"def print2D(root) :\n",
|
||
|
" \n",
|
||
|
" # space=[0]\n",
|
||
|
" # Pass initial space count as 0\n",
|
||
|
" print2DUtil(root, 0)\n",
|
||
|
"\n",
|
||
|
"# Driver Code\n",
|
||
|
"if __name__ == '__main__':\n",
|
||
|
"\n",
|
||
|
" \n",
|
||
|
" print2D(root)"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.9.7"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|