4.2.3
Trees
Trees
Trees
Unlike other data structures in computer science, trees represent data hierarchically.
![Illustrative background for Roots and branches](https://image-v2.cdn.app.senecalearning.com/2020-08/b4dedd1d-0509-4a0a-8e8e-c294bcdf3fff/Screenshot%202020-08-14%20at%2016.51.21-min,h_400,q_80,w_640.png)
![Illustrative background for Roots and branches ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/b4dedd1d-0509-4a0a-8e8e-c294bcdf3fff/Screenshot%202020-08-14%20at%2016.51.21-min,h_400,q_80,w_640.png)
Roots and branches
Roots and branches
- Trees begin with a ‘root’ node, with one or more ‘branches’ beneath it.
- Each branch, in turn, can have one or more sub-branches connected to it.
![Illustrative background for Tree construction](https://image-v2.cdn.app.senecalearning.com/2020-08/b4dedd1d-0509-4a0a-8e8e-c294bcdf3fff/Screenshot%202020-08-14%20at%2016.51.21-min,h_400,q_80,w_640.png)
![Illustrative background for Tree construction ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/b4dedd1d-0509-4a0a-8e8e-c294bcdf3fff/Screenshot%202020-08-14%20at%2016.51.21-min,h_400,q_80,w_640.png)
Tree construction
Tree construction
- The rules for creating a tree are:
- Each node must only be connected to one ‘parent’ node and one or more ‘children’.
- There should be no connections directly between nodes on the same level of the hierarchy.
![Illustrative background for Tree traversal](https://image-v2.cdn.app.senecalearning.com/2018-03/c77f5caa-3788-4c57-80d2-bbcd600d1dc4/shutterstock_624132143,h_400,q_80,w_640.jpg)
![Illustrative background for Tree traversal ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-03/c77f5caa-3788-4c57-80d2-bbcd600d1dc4/shutterstock_624132143,h_400,q_80,w_640.jpg)
Tree traversal
Tree traversal
- There are three ways to navigate, or ‘traverse’ a tree data structure:
- Pre-order.
- In-order.
- Post-order.
![Illustrative background for Pre-order traversal](https://image-v2.cdn.app.senecalearning.com/2020-08/aa1cd128-7b8b-41a0-8f05-3a4136a0d100/Screenshot%202020-08-14%20at%2016.49.06-min,h_400,q_80,w_640.png)
![Illustrative background for Pre-order traversal ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/aa1cd128-7b8b-41a0-8f05-3a4136a0d100/Screenshot%202020-08-14%20at%2016.49.06-min,h_400,q_80,w_640.png)
Pre-order traversal
Pre-order traversal
- Pre-order traversal starts with the root node, then visits each of the other nodes from left to right.
- In the example shown, the nodes are visited in the following order:
- Tiger, Zebra, Lion, Gazelle, Giraffe, Rhino, Hippo, Elephant
![Illustrative background for In-order traversal](https://image-v2.cdn.app.senecalearning.com/2020-08/aa1cd128-7b8b-41a0-8f05-3a4136a0d100/Screenshot%202020-08-14%20at%2016.49.06-min,h_400,q_80,w_640.png)
![Illustrative background for In-order traversal ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/aa1cd128-7b8b-41a0-8f05-3a4136a0d100/Screenshot%202020-08-14%20at%2016.49.06-min,h_400,q_80,w_640.png)
In-order traversal
In-order traversal
- In-order traversal starts with the nodes in the left subtree, then visits the root node followed by the nodes in the right subtree.
- In this case, the nodes above are visited in the following order:
- Lion, Zebra, Gazelle, Giraffe, Tiger, Elephant, Hippo, Rhino.
![Illustrative background for Post-order traversal](https://image-v2.cdn.app.senecalearning.com/2020-08/aa1cd128-7b8b-41a0-8f05-3a4136a0d100/Screenshot%202020-08-14%20at%2016.49.06-min,h_400,q_80,w_640.png)
![Illustrative background for Post-order traversal ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/aa1cd128-7b8b-41a0-8f05-3a4136a0d100/Screenshot%202020-08-14%20at%2016.49.06-min,h_400,q_80,w_640.png)
Post-order traversal
Post-order traversal
- Post-order traversal starts by visiting the nodes in the left subtree from bottom to top, then the right subtree bottom to top, before finishing with a visit to the root node. - The nodes in the example above are therefore visited in the following order:
- Lion, Gazelle, Giraffe, Zebra, Elephant, Hippo, Rhino, Tiger.
Binary Search Trees
Binary Search Trees
A binary search tree is a tree data structure used for efficient searching of data.
![Illustrative background for Binary search trees](https://image-v2.cdn.app.senecalearning.com/2020-08/94c6bd6a-8cf8-4f98-86a1-69b3ab4895c1/Screenshot%202020-08-17%20at%2009.51.14-min,h_400,q_80,w_640.png)
![Illustrative background for Binary search trees ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/94c6bd6a-8cf8-4f98-86a1-69b3ab4895c1/Screenshot%202020-08-17%20at%2009.51.14-min,h_400,q_80,w_640.png)
Binary search trees
Binary search trees
- Binary search trees share the same properties as trees, with the exception that each node can have no more than two ‘child’ nodes.
- Binary search trees are very quick to search.
![Illustrative background for Storing data](https://image-v2.cdn.app.senecalearning.com/2020-08/36435c90-eae1-4b2f-889e-a89fdb73574e/Screenshot%202020-08-14%20at%2016.59.24-min,h_400,q_80,w_640.png)
![Illustrative background for Storing data ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/36435c90-eae1-4b2f-889e-a89fdb73574e/Screenshot%202020-08-14%20at%2016.59.24-min,h_400,q_80,w_640.png)
Storing data
Storing data
- After storing the first piece of data in the root node:
- The second entry is stored in a child node to the left of the root if it has a lesser value.
- The second entry is stored in a child node to the right if it has a greater value.
![Illustrative background for Example](https://image-v2.cdn.app.senecalearning.com/2020-08/54270ccc-1604-4af3-9bfa-dc2d5d204e57/Screenshot%202020-08-14%20at%2016.54.51-min,h_400,q_80,w_640.png)
![Illustrative background for Example ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/54270ccc-1604-4af3-9bfa-dc2d5d204e57/Screenshot%202020-08-14%20at%2016.54.51-min,h_400,q_80,w_640.png)
Example
Example
- The procedure for storing ‘Tiger’, ‘Zebra’, ‘Lion’, ‘Gazelle’ and ‘Rhino’ in the order given is as follows:
- 'Tiger’ takes the root node.
- ‘Zebra’ is greater than ‘Tiger’ alphabetically, so is stored as a child node to the right of the root.
![Illustrative background for Step 2](https://image-v2.cdn.app.senecalearning.com/2020-08/54270ccc-1604-4af3-9bfa-dc2d5d204e57/Screenshot%202020-08-14%20at%2016.54.51-min,h_400,q_80,w_640.png)
![Illustrative background for Step 2 ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/54270ccc-1604-4af3-9bfa-dc2d5d204e57/Screenshot%202020-08-14%20at%2016.54.51-min,h_400,q_80,w_640.png)
Step 2
Step 2
- ‘Lion’ comes before ‘Tiger’ and so is stored as a child node to the left of the root.
![Illustrative background for Step 3](https://image-v2.cdn.app.senecalearning.com/2020-08/54270ccc-1604-4af3-9bfa-dc2d5d204e57/Screenshot%202020-08-14%20at%2016.54.51-min,h_400,q_80,w_640.png)
![Illustrative background for Step 3 ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/54270ccc-1604-4af3-9bfa-dc2d5d204e57/Screenshot%202020-08-14%20at%2016.54.51-min,h_400,q_80,w_640.png)
Step 3
Step 3
- ‘Gazelle’ comes before ‘Tiger’ and before ‘Lion’, so is stored as a left child to the ‘Lion’ node.
- ‘Rhino’ also comes before ‘Tiger’, but after ‘Lion’ so is stored on the right of this node.
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
Jump to other topics
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
![Go student ad image](/en-GB/revision-notes/_next/image?url=%2Fen-GB%2Frevision-notes%2Fimages%2Fgo-student-uk-ad.jpg&w=640&q=100)
Unlock your full potential with GoStudent tutoring
Affordable 1:1 tutoring from the comfort of your home
Tutors are matched to your specific learning needs
30+ school subjects covered