Home Database Mysql Tutorial 二叉树(1)已知某2种排序方式,创建这个二叉树并按第3种方式排序

二叉树(1)已知某2种排序方式,创建这个二叉树并按第3种方式排序

Jun 07, 2016 pm 03:49 PM
about create sort Way this

1.关于指针和引用的说明 数据结构中 建立 二叉树子函数,根结点为什么用双重指针,即 指针的指针。 因为树的结点要用指针描述。 如果只用指针,作形参传给建立结点的函数,这个指针传给了函数栈中的内存,函数返回后,函数栈销毁,不能获得结点。 而用指针的

1.关于指针和引用的说明

   数据结构中建立二叉树子函数,根结点为什么用双重指针,即指针的指针。因为树的结点要用指针描述。如果只用指针,作形参传给建立结点的函数,这个指针值传给了函数栈中的内存,函数返回后,函数栈销毁,不能获得结点。而用指针的指针,函数内修改了这个双重指针指向的值(即结点指针),在函数外也能获得结点。这swap()函数要用指针而不能用值做参数一样。只是这里的值本身就是个指针,所以要用指针的指针。如下:

 typedef struct BinaryTreeNode
{
    char data;
    BinaryTreeNode * leftChild;
    BinaryTreeNode * rightChild;
}Node;
void Create(Node**T){......}
int main(){
	 Node* T;
	 Create(&T);
}
Copy after login

<span><span>   <span>如果Create的参数不是指针的引用(等同双指针),</span></span></span><span><span><span><span>main中 Create(T)是把指针T指向的地址传进去了。注意,只是地址.</span></span></span><span><span><span>然后你在Create函数内部申请内存时, 把这个地址给改变了, 但是因为你传的是一个地址, 这个地址本身跟T无关,T仅仅是指向了这个地址而以. 所以Create(T)之后, T还是指向原来的地址,并未改变, 后面的操作当然就是崩溃了(因为T未初始化,是一个野指针)。</span></span></span><span><span><br></span></span><span><span><span> 传值: 函数内部修改不会对原值内容进行改变.
</span></span></span><span><span><span>     传址: 函数内部修改会影响原值.</span></span></span><span><span><span>    可能我们认为值的指针就是传址了.</span></span></span><span><span><span>如果是传值, 函数入栈的时候就是把这个变量的值压栈,如果是传址, 就是把指针压栈,但压栈的时候,本身实际也是写一个数值而以.</span></span></span><span><span><span>你传的是指针的情况下, 如果修改指针指向的内容, 那么函数外部会同步修改.</span></span></span><span><span><span>但是你传入的是指针, 但是又修改的是指针本身, 而不是其内容, 那么你这就相当于传值.</span></span></span><span><span><span>当你要修改指针本身的时候, 你可以这样理解.</span></span></span></span>
Copy after login

//比如有:
void Creat(BTNode *pRoot);
//可以修改成以下形式
typedef BTNode* PNODE;
void Creat(PNODE pRoot);
//这时你可能就看出来了, 原来这是一个传值调用.想要修改pRoot的值, 那么就要
void Creat(PNODE &pRoot); ====还原就是 void Creat(BTNode* &pRoot);
//或者
void Creat(PNODE *pRoot); ====还原就是 void Creat(BTNode* *pRoot);
Copy after login
反正注意区分传址还是传值的区分不是看参数是否是指针, 而是要看你在函数内部是如何操作参数的. 如果你操作的是参数本身, 那么就是传值.如果你是把参数当成一个地址, 操作的是那个地址上的内容,那就是传址。

一个简单的例子:

  int a = 1;
  int b = 2;  
  int *tmp = &a;
  int *p = tmp;// 第二种情况:int *&p = tmp;(此即是指向指针的引用)
  p = &b;
  *p = 5;
Copy after login
<span>  第一种情况:a=1,b=5,*p=5,tmp=1
</span><pre class="brush:php;toolbar:false"><span>  第二种情况:a=1,b=,*p=5,<span>tmp=5.</span> 
</span><span><span>这是因为指向指针的引用,不仅改变了指针所指的对象,也改变了指针本身。</span></span>
Copy after login

2.前序,中序,后序遍历的简单说明

二叉树(1)已知某2种排序方式,创建这个二叉树并按第3种方式排序

<span><span><strong>   </strong></span></span><span><span><span><strong>先序遍历</strong></span><span>也叫做先根遍历,前序遍历</span></span><span>,<span>可记做</span><strong><span><span>根</span></span><span>左右</span></strong><span>(二叉树父结点向下先左后右)。</span></span><span>首先访问根结点然后遍历左子树,最后遍历右子树。在遍历左、右子树时,仍然先访问根结点,然后遍历左子树,最后遍历右子树,如果二叉树为空则返回。</span><span>上图所示二叉树的先序遍历结果是:ABDECF。
</span></span><span><span>    <span>已知中序遍历和后序遍历,可以确定唯一的前序遍历。</span></span></span>
Copy after login
<span><span>    </span><span><span><span>中序遍历</span><span><span>也叫</span><span>做</span><span>中根遍历</span><span>、</span><span>中序周游,可记做</span><span><strong><span>左</span><span>根</span><span>右</span></strong></span><span>。</span></span><span>首先遍历左子树,然后访问根结点,最后遍历右子树。在遍历左、右子树时,仍然先遍历左子树,再访问根结点,最后遍历右子树。</span></span></span></span><span><span>注意的是:遍历左右子树时</span><span>仍然采用中序遍历方法。</span><span>中<span>序遍历的</span></span><span>时间复杂度</span><span><span>为</span>:O(n)。</span><span>如果一棵</span><span>二叉排序树</span><span>的节点值是数值,中序遍历的结果为升序排列的</span><span>数组</span><span>。可以利用该性质检测一棵树是否为二叉排序数。<span>上图所示</span><span>二叉树</span><span>中</span><span>序遍历结果:DBEAFC</span></span></span>
<span><span>    </span><span><span>已知前序遍历和后序遍历,</span><span><span><strong>不能</strong></span></span><span>确定唯一的中序遍历。</span></span></span>
Copy after login
<p><span></span></p><span><span><span>    </span><span><span><strong>后序遍历</strong></span><span>(LRD)</span></span><span>也叫做</span><span>后根遍历</span><span>、后序周游,可记做</span><span><span>左右</span></span><span><span><strong>根</strong></span></span><span>。后序遍历有</span><span><span>递归算法</span></span><span>和非递归算法两种。</span></span></span><span><span>后序遍历首先遍历左子树,然后遍历右子树,最后遍历访问根结点,在遍历左、右子树时,仍然先遍历左子树,然后遍历右子树,最后遍历根结点。即:</span><span>若</span><span>二叉树</span><span>为空则结束返回,</span><span>否则:</span><span>(1)后序遍历左子树</span><span>(2)后序遍历右子树(3)访问根结点。</span><span>如右图所示</span><span>二叉树</span><span>后序遍历结果:DEBFCA。
</span></span><span><span>    </span><span><span>已知前序遍历和中序遍历,可以确定唯一的后序遍历。</span></span></span>
Copy after login

   解释:前序或后序能够确定根节点,结合中序能够唯一确定左子树和右子树元素。而仅仅知道前序和后序时,当左右子树存在空时,无法唯一确定究竟是哪一棵树为空。即仅仅知道前序和后序,无法唯一的还原2叉树,即中序无法唯一确定。


3.程序代码以及说明

   1.已知前序和中序排列,创建二叉树,并输出后序排列
   2.已知中序和后序排列,创建二叉树,并输出前序排列
   3.使用递归法遍历二叉树

#include "iostream"
using namespace std;
typedef struct BinaryTreeNode//二叉树节点
 {
	 char data;
	 BinaryTreeNode* leftchild;
	 BinaryTreeNode* rightchild;
 }Node,*NodePoint;
 bool MakeBinaryTree_PM(NodePoint* root,char* preOrder, char* midOrder,int length)//基于前序和中序遍历计算后序遍历
 {
	 if(length==0)
	    {*root=NULL;return true;}
	 else
	    {
	     *root=new Node;
	    (*root)->data=*preOrder;//前序第一个元素为根节点
             char * rootposition = strchr(midOrder,(*root)->data);//查找根节点在排序中的位置
	     if (rootposition == NULL)
		 {cout leftchild,preOrder+1,midOrder,LeftTreeSize);
	MakeBinaryTree_PM(&(*root)->rightchild,preOrder+LeftTreeSize+1,rootposition+1,length-LeftTreeSize-1);
	          return true;
		  }
	      }
 }
bool MakeBinaryTree_ML(NodePoint* root,char* midOrder,char* lastOrder,int length)//基于中序和后序遍历计算前序遍历
 {
	 if(length==0)
	    {*root=NULL;return true;}
	 else
	    {
	      *root=new Node;
	     (*root)->data=lastOrder[length-1];//后序最后一个元素为根节点
	      char * rootposition = strchr(midOrder,(*root)->data);//查找根节点在排序中的位置
	      if (rootposition == NULL)
		  {cout leftchild,leftSubTree_mid,leftSubTree_last,LeftSubTreeSize);
		   delete[]leftSubTree_mid;delete[]leftSubTree_last;
		   MakeBinaryTree_ML(&(*root)->rightchild,rootposition+1,rightSubTree_last,RightSubTreeSize);
                   delete []rightSubTree_last;
		   return true;
	         }
	    }
 }
 void PreTraverse(NodePoint Root)//嵌套前序遍历
 {
    if(Root==NULL);
    else
      {coutdata;
       PreTraverse(Root->leftchild);
       PreTraverse(Root->rightchild);}
 }
void MidTraverse(NodePoint Root)//嵌套中序遍历
 {
       if(Root==NULL);
       else
       {MidTraverse(Root->leftchild);
	   coutdata;
	MidTraverse(Root->rightchild);}
 }
void PostTraverse(Node* Root)//嵌套后序遍历
{
    if (Root == NULL)
        return;
    PostTraverse(Root->leftchild);
    PostTraverse(Root->rightchild);
    cout data;
}
int main(int argc, const char** argv)
{
    char pre[] = "abdeijcfg";//前序
    char mid[] = "dbiejafcg";//中序
    char last[]="dijebfgca";//后序
    Node* Root;
	
    MakeBinaryTree_PM(&Root, pre, mid, strlen(pre));//使用指针的引用
    cout//使用指针的引用
    cout<br>
<pre class="brush:php;toolbar:false"> <span><span><strong>注意 </strong></span>1.必须知道中序,知道任何其他两种中的一种倘若知道,可建唯一的二叉树进而得到另外一种排序。
</span><span>      2.创建二叉树的时候,使用指针的引用作为形参,即双重指针。</span>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to sort photos by date taken in Windows 11/10 How to sort photos by date taken in Windows 11/10 Feb 19, 2024 pm 08:45 PM

This article will introduce how to sort pictures according to shooting date in Windows 11/10, and also discuss what to do if Windows does not sort pictures by date. In Windows systems, organizing photos properly is crucial to making it easy to find image files. Users can manage folders containing photos based on different sorting methods such as date, size, and name. In addition, you can set ascending or descending order as needed to organize files more flexibly. How to Sort Photos by Date Taken in Windows 11/10 To sort photos by date taken in Windows, follow these steps: Open Pictures, Desktop, or any folder where you place photos In the Ribbon menu, click

How to sort emails by sender, subject, date, category, size in Outlook How to sort emails by sender, subject, date, category, size in Outlook Feb 19, 2024 am 10:48 AM

Outlook offers many settings and features to help you manage your work more efficiently. One of them is the sorting option that allows you to categorize your emails according to your needs. In this tutorial, we will learn how to use Outlook's sorting feature to organize emails based on criteria such as sender, subject, date, category, or size. This will make it easier for you to process and find important information, making you more productive. Microsoft Outlook is a powerful application that makes it easy to centrally manage your email and calendar schedules. You can easily send, receive, and organize email, while built-in calendar functionality makes it easy to keep track of your upcoming events and appointments. How to be in Outloo

How to create pixel art in GIMP How to create pixel art in GIMP Feb 19, 2024 pm 03:24 PM

This article will interest you if you are interested in using GIMP for pixel art creation on Windows. GIMP is a well-known graphics editing software that is not only free and open source, but also helps users create beautiful images and designs easily. In addition to being suitable for beginners and professional designers alike, GIMP can also be used to create pixel art, a form of digital art that utilizes pixels as the only building blocks for drawing and creating. How to Create Pixel Art in GIMP Here are the main steps to create pixel pictures using GIMP on a Windows PC: Download and install GIMP, then launch the application. Create a new image. Resize width and height. Select the pencil tool. Set the brush type to pixels. set up

How to create a folder on Realme Phone? How to create a folder on Realme Phone? Mar 23, 2024 pm 02:30 PM

Title: Realme Phone Beginner’s Guide: How to Create Folders on Realme Phone? In today's society, mobile phones have become an indispensable tool in people's lives. As a popular smartphone brand, Realme Phone is loved by users for its simple and practical operating system. In the process of using Realme phones, many people may encounter situations where they need to organize files and applications on their phones, and creating folders is an effective way. This article will introduce how to create folders on Realme phones to help users better manage their phone content. No.

How to create a family with Gree+ How to create a family with Gree+ Mar 01, 2024 pm 12:40 PM

Many friends expressed that they want to know how to create a family in Gree+ software. Here is the operation method for you. Friends who want to know more, come and take a look with me. First, open the Gree+ software on your mobile phone and log in. Then, in the options bar at the bottom of the page, click the "My" option on the far right to enter the personal account page. 2. After coming to my page, there is a "Create Family" option under "Family". After finding it, click on it to enter. 3. Next jump to the page to create a family, enter the family name to be set in the input box according to the prompts, and click the "Save" button in the upper right corner after entering it. 4. Finally, a "save successfully" prompt will pop up at the bottom of the page, indicating that the family has been successfully created.

How to Create a Contact Poster for Your iPhone How to Create a Contact Poster for Your iPhone Mar 02, 2024 am 11:30 AM

In iOS17, Apple has added a contact poster feature to its commonly used Phone and Contacts apps. This feature allows users to set personalized posters for each contact, making the address book more visual and personal. Contact posters can help users identify and locate specific contacts more quickly, improving user experience. Through this feature, users can add specific pictures or logos to each contact according to their preferences and needs, making the address book interface more vivid. Apple in iOS17 provides iPhone users with a novel way to express themselves, and added a personalizable contact poster. The Contact Poster feature allows you to display unique, personalized content when calling other iPhone users. you

How to sort WPS scores How to sort WPS scores Mar 20, 2024 am 11:28 AM

In our work, we often use wps software. There are many ways to process data in wps software, and the functions are also very powerful. We often use functions to find averages, summaries, etc. It can be said that as long as The methods that can be used for statistical data have been prepared for everyone in the WPS software library. Below we will introduce the steps of how to sort the scores in WPS. After reading this, you can learn from the experience. 1. First open the table that needs to be ranked. As shown below. 2. Then enter the formula =rank(B2, B2: B5, 0), and be sure to enter 0. As shown below. 3. After entering the formula, press the F4 key on the computer keyboard. This step is to change the relative reference into an absolute reference.

How to create a Gantt chart using Highcharts How to create a Gantt chart using Highcharts Dec 17, 2023 pm 07:23 PM

How to use Highcharts to create a Gantt chart requires specific code examples. Introduction: The Gantt chart is a chart form commonly used to display project progress and time management. It can visually display the start time, end time and progress of the task. Highcharts is a powerful JavaScript chart library that provides rich chart types and flexible configuration options. This article will introduce how to use Highcharts to create a Gantt chart and give specific code examples. 1. Highchart

See all articles