Tree (Autosaved)
Tree (Autosaved)
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
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
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.