C Language Enhancement (1) binary sorting tree into a two-way linked list, binary sorting

Source: Internet
Author: User

C Language Enhancement (1) binary sorting tree into a two-way linked list, binary sorting

Almost every programmer's programming start point is C. After playing Java, C #, PHP, and Python, how does it feel to return to C?

This blog is the first article in the [C Language Enhancement] series to talk about the binary tree that has been scratched by many coders.

You can master this question.

  • How to Create a binary tree
  • How to traverse Binary Trees
  • How to Create a binary linked list
  • How to Use Recursive Algorithms


This is a very old-fashioned but classic data structure question. Maybe many people may say they have done it before, but when many people come back to it, can I write out the solution without any reference?

Requirement: Binary sorting tree-> two-way linked list sorting

Nodes cannot be added, but pointers can only be modified

Ideas
1. Define Binary Tree struct
2. Binary Tree constructor. function: specify the value to be inserted and the root node of the binary tree. It can be automatically inserted to the binary tree.
3. Insert nodes to the two-way linked list while traversing the binary tree in the central order and print them


Source code

# Include <stdio. h> # include <stdlib. h> # include <iostream> using namespace std;/** binary sorting tree-> nodes cannot be added for Bidirectional linked list sorting. You can only modify the pointer pointing to IDEA 1. create a binary tree structure 2. binary Tree constructor 3. insert a node to the two-way linked list while traversing the binary tree in a central order and print * // create the binary tree struct BTreeNode {int value; BTreeNode * leftNode; BTreeNode * rightNode;}; typedef BTreeNode Btn; BTreeNode * dListIndex; // the index of the two-way linked list, pointing to the BTreeNode * dListHead of the current linked list; // The Head of the two-way linked list/* Binary Tree constructor is inserted based on the binary Sorting Algorithm (left small and right big) value of the root node of the node binary tree */void insertToBTree (BTre ENode * & node, int value) {// The node is empty, and the inserted node is the root node if (NULL = node) {BTreeNode * btNode = new BTreeNode (); btNode-> value = value; btNode-> leftNode = NULL; node = btNode;} else {// node is not empty. // The value is smaller than the root node, recursion left node if (node-> value) insertToBTree (node-> leftNode, value); // The value is greater than the root node, recursive right node else if (value> (node-> value) insertToBTree (node-> rightNode, value); // The value is equal to the value of an existing node, error message: elseprintf ("the node already exists. You cannot insert it again! ") ;}}/* Bidirectional linked list constructor btNode node to be inserted */void insertToDoubleList (Btn * btNode) {// The left pointer of the node to be inserted points to the two-way linked list tail btNode-> leftNode = dListIndex; // if the two-way linked list is not empty, the two-way linked list tail points to the node to be inserted on the right, complete bidirectional link if (NULL! = DListIndex) dListIndex-> rightNode = btNode; // The two-way linked list is empty, and the header node is the elsedListHead = btNode to be inserted; // The index node always points to the inserted node dListIndex = btNode; // The output value cout <btNode-> value <endl;}/* traverses the binary sorting tree in the middle order, insert the value btNode binary tree root node */void goThroughBTree (Btn * btNode) to the two-way linked list at the same time. {// The node is empty and if (NULL = btNode) return is returned; // The left node is not empty. Continue to the left to drill into else if (NULL! = BtNode-> leftNode) goThroughBTree (btNode-> leftNode); // wait until the left node is empty and insertToDoubleList (btNode) is inserted. // The right node is not empty, go right to if (NULL! = BtNode-> rightNode) {goThroughBTree (btNode-> rightNode) ;}} void main () {BTreeNode * root = NULL; // binary tree root node dListIndex = NULL; dListHead = NULL; // create an insertToBTree (root, 10); insertToBTree (root, 6); insertToBTree (root, 12); insertToBTree (root, 14 ); insertToBTree (root, 8); insertToBTree (root, 4); insertToBTree (root, 16); // traverses the binary tree goThroughBTree (root); system ("pause ");}


This question is very basic and almost every programmer has seen it. But how many people can really understand the concept of binary tree without any reference? This is also the reason for Microsoft's interview questions.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.