0% found this document useful (0 votes)
31 views19 pages

Tree (Autosaved)

Trees are a non-linear data structure that combines advantages of arrays and linked lists. They allow for efficient access and update of large data collections. Binary trees restrict each node to having at most two children. Key terms include root, child, parent, leaf, height, and traversal. Binary trees are widely used due to fast search and insertion/deletion capabilities.

Uploaded by

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

Tree (Autosaved)

Trees are a non-linear data structure that combines advantages of arrays and linked lists. They allow for efficient access and update of large data collections. Binary trees restrict each node to having at most two children. Key terms include root, child, parent, leaf, height, and traversal. Binary trees are widely used due to fast search and insertion/deletion capabilities.

Uploaded by

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

Non Linear Data Structures:

Trees
Your Observations (Data Structures
so far)
•Array
• Possibly the most common data structure used to
store data.
• One memory block is allocated for the entire
array which holds all the elements of the array
• Elements can be accessed in a constant time by
using the index of the particular element as the
subscript.
•Advantages of using arrays:
• Easier to use and access
• Faster access to the elements
Disadvantages of using arrays:
• Fixed size - the size of the array is static
• One block allocation
• Array cannot use scattered memory block free to be
used.
• Complex position-based insertion
• If you want to insert an element at a position already
covered by some other element then you got to shift
right by one position all the elements to the right of
that position.
• This will vacate the position for you to insert the new
element at the desired position.
• The more elements you have to the right of the
desired position, the more expensive the process will
be.
Linked List

•Elements are not stored in contiguous


memory locations
• Every node contains an element and a link to
the next element
•The allocation is not static instead it's
dynamic
Advantages of using Linked Lists:

•Flexibility - insert at (or delete from) any


position
•No single allocation of memory needed -
fragmented memory can be put to a better
use
•Dynamic allocation - the size is not required
to be known in advance
Disadvantages of using Linked Lists:

• Complex to use and access - relatively complex as


compared to arrays
• No constant time access to the elements - simply
because it doesn't involve the simple arithmetic
used by arrays to compute the memory address, so
relatively inefficient as compared to arrays
Introduction to Trees

• One of the fundamental data storage structures


used in programming.
• Combines advantages of an ordered array and a
linked list.
• Searching as fast as in ordered array.
• Insertion and deletion as fast as in linked list.
• Tree structures permit both efficient access and
update to large collections of data.
Applications of Trees

•Trees, particularly BINARY trees are widely


used in computer science
•Applications include:
• Prioritizing jobs
• Describing mathematical expressions and
• syntactic elements of computer programs
• organizing the information needed to drive data
compression algorithms
Tree (example)

node
Tree characteristics
• Consists of nodes connected by edges.
• Nodes often represent entities (complex objects) such
as people, car parts etc.
• Edges between the nodes represent the way the
nodes are related.
• The only way to get from node to node is to follow a
path along the edges.
Special kinds of trees

• Ordered vs. unordered


• Binary tree
• Empty vs non-empty
• Full
• Perfect
• Complete
Ordered trees

• Have a linear order on the children of each node


• That is, can clearly identify a 1st, 2nd, …, kth child
• An unordered tree doesn’t have this property
Binary Trees
• Every node in a binary tree can have at most two
children.

• The two children of each node are called the left


child and right child corresponding to their
positions.

• A node can have only a left child or only a right


child or it can have no children at all.

• In BST Left child is always less that its parent, while


right child is greater than its parent.
Selected Samples of Binary Trees

A A

B C B

C
D E F G
D

H I E

Tree A Tree B
Size 9 Depth 3 Size 5 Depth 4
Tree Terminology
• Root: Node at the top of the tree.
• Subtree: Any node can be considered to be the root of a
subtree, which consists of its children and its children’s
children and so on.
• Parent: Any node, except root has exactly one edge running
upward to another node. The node above it is called parent.
• Child: Any node may have one or more lines running
downward to other nodes. Nodes below are children.
• Sibling: term siblings is used to refer to two nodes that share
the same parent
• Leaf: A node that has no children.
• Path: Traversal from node to node along the edges results in
a sequence called path. The sum of edges is its Path Length
• Degree: The degree of a node is the number of subtrees of
the node
Tree Terminology
•Traversing: To traverse a tree means to visit
all the nodes in some specified order.
•Depth/Level: The depth of a node M in the
tree is the length of the path from the root to
M.
•Height: The height of a tree is one more than
the depth of the deepest node in the tree
•Internal node: An internal node is any node
that has at least one non-empty child
•Keys: Key value is used to search for the item
or perform other operations on it.
Tree organization
Root

Path Edge

Parent Node
Child

Height Level 2

Leaf

Sibling
s
Binary Tree
Empty binary tree

• A binary tree is either empty or is a node with left and right empty subtrees.

You might also like