0% found this document useful (0 votes)
4 views

binary tree

The document explains the concept of trees in computer science, highlighting their hierarchical structure with roots, branches, nodes, and leaves. It details binary trees, which allow each node to have at most two children, and discusses methods for representing trees in memory, including linked and array representations. Additionally, it outlines different types of binary trees, such as full, complete, perfect, balanced, and degenerate, emphasizing their performance implications for operations like searching and insertion.

Uploaded by

gssschhakoh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

binary tree

The document explains the concept of trees in computer science, highlighting their hierarchical structure with roots, branches, nodes, and leaves. It details binary trees, which allow each node to have at most two children, and discusses methods for representing trees in memory, including linked and array representations. Additionally, it outlines different types of binary trees, such as full, complete, perfect, balanced, and degenerate, emphasizing their performance implications for operations like searching and insertion.

Uploaded by

gssschhakoh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

A tree in computer science is like a real-life tree, but upside down.

It's a way of
organizing information in a hierarchical structure.

 Root: The top of the tree is called the root. It's the starting point.
 Branches: Lines connecting the circles are called branches. They show the relationships
between the items.
 Nodes: The circles are called nodes. Each node contains a piece of information.
 Leaves: Nodes at the very bottom with no children

The concept of a Binary Tree

 It's a special type of tree where each node can have at most two children, referred to as the
left child and the right child.
 This "two-child" limit makes binary trees simpler to implement and analyze than general
trees.

Here's a simple analogy:

Imagine your family tree. You are the root, your parents are your children, your
grandparents are your parents' parents, and so on. This is a tree structure. If each
person only had two children, it would be a binary tree.

Key Points:

 Hierarchical: Trees represent hierarchical relationships, like parent-child relationships.


 Unique Path: There's only one way to get from the root to any other node.
 No Cycles: Unlike some other data structures, trees don't have cycles (you can't go in a loop).

Common Uses:

Trees are used for many things in computer science, including:

 Storing hierarchical data: Like file systems on your computer.


 Searching and sorting: Efficiently finding and organizing information.
 Representing code: Compilers use trees to understand the structure of programs.

When we talk about representing a binary tree in a computer's memory, we're


essentially figuring out how to store the tree's structure and data in a way the
computer can understand and access. There are two main ways to do this:

1. Linked Representation
 Nodes with Pointers: Imagine each node in the tree as a little box. Inside this box, we store
two things:
o The actual data (e.g., a number, a letter, etc.)
o Two "pointers" (like arrows) that point to the left and right children of that node.
 Connecting the Dots: These pointers are memory addresses that tell the computer where to
find the child nodes in memory. So, by following the pointers, the computer can traverse the
tree.
 Example:

A
/ \
B C
/ \
D E

In memory, this might look like:

[Node A] : Data = 'A', Left = (address of Node B), Right = (address


of Node C)
[Node B] : Data = 'B', Left = (address of Node D), Right = (address
of Node E)
[Node C] : Data = 'C', Left = NULL, Right = NULL
[Node D] : Data = 'D', Left = NULL, Right = NULL
[Node E] : Data = 'E', Left = NULL, Right = NULL

(NULL means there's no child)

 Advantages:
o Flexible: Easy to add or remove nodes.
o Efficient for most trees: Only uses memory for the nodes that actually exist.
 Disadvantages:

o Extra memory for pointers: Each node needs to store those addresses.

2. Array Representation

 One Big Array: We store all the nodes in a single array (a list of items in a computer's
memory).
 Implicit Relationships: Instead of pointers, we use the position of the nodes in the array to
figure out parent-child relationships.

o If a node is at index i, its left child is at 2i + 1, and its right child is at 2i + 2.

 Example: Using the same tree as above:

A
/ \
B C
/ \
D E

In an array:

Index: 0 1 2 3 4
Value: A B C D E
 A is at index 0.
 B (left child) is at 2 * 0 + 1 = 1.
 C (right child) is at 2 * 0 + 2 = 2.
 D (left child of B) is at 2 * 1 + 1 = 3.
 E (right child of B) is at 2 * 1 + 2 = 4.

Advantages:

o Simple to implement: Just a basic array.


o Good for complete trees: When the tree is full (every level is filled).

 Disadvantages:

o Wasted space: If the tree is sparse (has missing nodes), you end up with empty
spaces in the array.
o Not flexible: Adding or removing nodes can be tricky and may require shifting
elements in the array.

Which method is better?

It depends on the specific situation:

 Linked representation is generally more common because it's more flexible and efficient for
most types of binary trees.
 Array representation is useful for complete binary trees or when memory space is extremely
limited.

The different types of binary trees in simple terms:

Full Binary Tree:

1.
1. Every node has either 0 or 2 children. No node has only one child.
2. Think of it as a complete family tree where every parent has either no children or
exactly two children.

A
/ \
B C
/ \ / \
D E F G

Complete Binary Tree:

1. All levels are completely filled except possibly the last level, which is filled from left
to right.
2. Imagine building a tree level by level, filling each spot from left to right before
moving to the next level.

A
/ \
B C
/ \ /
D E F
Perfect Binary Tree:

1. A special case of a full binary tree where all internal nodes have two children and all
leaf nodes are at the same level.
2. It's like a perfectly balanced family tree where every generation has double the
members of the previous one.

A
/ \
B C
/ \ / \
D E F G

(Notice this is also a full and complete binary tree)

Balanced Binary Tree:

2.

1. The height of the left and right subtrees of any node differ by at most 1. This helps
keep the tree relatively "flat" and efficient for searching.
2. There are different types of balanced trees (AVL trees, Red-Black trees), each with
its own rules for maintaining balance. The key idea is to prevent the tree from
becoming too skewed (like a linked list).

A
/ \
B C
/ / \
D E F

Degenerate (or Pathological) Binary Tree:

1. Every node has only one child (either left or right). This essentially turns the tree
into a linked list.
2. This is the worst-case scenario for a binary tree, as it loses the benefits of tree-like
structure for searching (searching becomes O(n) instead of O(log n)).

A
\
B
\
C
\
D

Why these distinctions matter:

The type of binary tree affects its performance, especially for operations like
searching, insertion, and deletion. Balanced trees are generally the most efficient for
these operations because they keep the tree relatively shallow, preventing worst-case
scenarios like the degenerate tree.
In summary:

 Full: 0 or 2 children per node.


 Complete: Filled level by level, left to right.
 Perfect: Full and all leaves at the same level.
 Balanced: Height difference between subtrees is limited.
 Degenerate: Every node has only one child (like a linked list).

You might also like