0% found this document useful (0 votes)
6 views31 pages

Tree

A tree is a non-linear hierarchical data structure consisting of nodes connected by edges, with a root node at the top. Key concepts include node types (leaf, internal), tree height, and various traversal methods (inorder, preorder, postorder). Trees have applications in databases, compilers, and routing, and can be implemented in programming languages like Python.

Uploaded by

minathilisback
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views31 pages

Tree

A tree is a non-linear hierarchical data structure consisting of nodes connected by edges, with a root node at the top. Key concepts include node types (leaf, internal), tree height, and various traversal methods (inorder, preorder, postorder). Trees have applications in databases, compilers, and routing, and can be implemented in programming languages like Python.

Uploaded by

minathilisback
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Tree

• A tree data structure is a non-linear data structure because it


does not store in a sequential manner.
• It is a hierarchical structure as elements in a Tree are arranged
in multiple levels
Terminology
• Node
– A node is an entity that contains a key or value
and pointers to its child nodes.
– The last nodes of each path are called leaf nodes
or external nodes that do not contain a
link/pointer to child nodes.
– The node having at least a child node is called an
internal node.
• Edge
– It is the link between any two nodes.
• Root-It is the topmost node of a tree.
• The height of a node is the number of edges from the node to
the deepest leaf (ie. the longest path from the node to a leaf
node).
• Depth of a Node-The depth of a node is the number of edges
from the root to the node
• Height of a Tree-The height of a Tree is the
height of the root node or the depth of the
deepest node.
Height
• Degree of a Node
• The degree of a node is the total number of
branches of that node.
Types of Tree
• Binary Tree
• Binary Search Tree
• AVL Tree
• B-Tree
Tree Applications
• Binary Search Trees(BSTs) are used to quickly check whether
an element is present in a set or not.
• Heap is a kind of tree that is used for heap sort.
• A modified version of a tree called Tries is used in modern
routers to store routing information.
• Most popular databases use B-Trees and T-Trees, which are
variants of the tree structure we learned above to store their
data
• Compilers use a syntax tree to validate the syntax of every
program you write.
Tree Traversal
• Linear data structures like arrays, stacks, queues, and linked
list have only one way to read the data.
• But a hierarchical data structure like a tree can be traversed in
different ways.
• inorder
• preorder
• Postorder
Starting from top, Left to right 1 -> 12 -> 5 -> 6 -> 9

Starting from bottom, Left to right 5 -> 6 -> 12 -> 9 -> 1


traversal

Depth First Traversals:


(a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
Breadth-First or Level Order Traversal: 1 2 3 4 5
• Algorithm Preorder(tree)
• 1. Visit the root.
• 2. Traverse the left subtree, i.e., call Preorder(left-subtree)
• 3. Traverse the right subtree, i.e., call Preorder(right-subtree)
• Algorithm Preorder(tree)
• 1. Visit the root.
• 2. Traverse the left subtree, i.e., call Preorder(left-subtree)
• 3. Traverse the right subtree, i.e., call Preorder(right-subtree)
• Algorithm Postorder(tree)
• 1. Traverse the left subtree, i.e., call Postorder(left-subtree)
• 2. Traverse the right subtree, i.e., call Postorder(right-
subtree)
• 3. Visit the root.
• To create a tree in Python, we first have to start by creating a
Node class that will represent a single node.
• This Node class will contain 3 variables; the first is the left
pointing to the left child, the second variable data containing
the value for that node, and the right variable pointing to the
right child.
• class Node:
• def __init__(self, data):
• self.left = None
• self.right = None
• self.data = data
initialize a tree
• root = Node(10)
• root.left = Node(34)
• root.right = Node(89)
• root.left.left = Node(45)
• root.left.right = Node(50)
• 10
• / \
• 34 89
• / \
• 45 50
• Whenever you create an object of class Node, the __init__
constructor will be called, and all the variables inside that
constructor will be initialized.
• The root contains the root node of the tree, which has a value
of 10, and root.left and root.right using which we will insert
the left child with the value 34 and the right child to the root
node with the value 89. Since it is a binary tree, every node
will contain at most two nodes.
• In the end, we insert two more nodes to the tree i.e 45 and 50,
as the children for the node 34. You can insert any number of
nodes you want inside a tree depending upon the type of tree
you are creating.
Insertion
• def insert(self, data):
• # Compare the new value with the parent node
• if self.data:
• if data < self.data:
• if self.left is None:
• self.left = Node(data)
• else:
• self.left.insert(data)
• elif data > self.data:
• if self.right is None:
• self.right = Node(data)
• else:
• self.right.insert(data)
• else:
• self.data = data
• # Print the tree
• def PrintTree(self):
• if self.left:
• self.left.PrintTree()
• print( self.data),
• if self.right:
• self.right.PrintTree()
• def deleteTree(root):
• if root:
• # delete left subtree
• deleteTree(root.leftChild)
• # delete right subtree
• deleteTree(root.rightChild)
• # traverse root
• print("Deleting Node:", root.data)
• del root
Tree Traversal
• Algorithm Inorder(tree)
• 1. Traverse the left subtree, i.e., call
Inorder(left-subtree)
• 2. Visit the root.
• 3. Traverse the right subtree, i.e., call
Inorder(right-subtree)
• 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)
• Algorithm Preorder(tree)
• 1. Visit the root.
• 2. Traverse the left subtree, i.e., call
Preorder(left-subtree)
• 3. Traverse the right subtree, i.e., call
Preorder(right-subtree)
• # 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)
• Algorithm Postorder(tree)
• 1. Traverse the left subtree, i.e., call
Postorder(left-subtree)
• 2. Traverse the right subtree, i.e., call
Postorder(right-subtree)
• 3. Visit the root.
• # 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),

You might also like