排序二叉树

排序二叉树

    二叉树:作为基本数据结构的一种,是红黑树,B树等树形结构的基础。而排序二叉树是按照二叉树的结构来组织的。在本文中采用链表结构来创建二叉树。排序二叉树的    基本原理:

    排序二叉树是将归并排序的基本思想构建二叉树结构,再用中序遍历的规则来遍历树的各个节点,输出的结果就是有序序列(本文是将一组无序的整型数值存储在二叉树中,通过中序遍历二叉树将数值输入,而得到一组按照由小到大排序的序列,从而间接实现了排序。所以下文的解释和说明都以此为例)。

    插入节点的过程:

    1.判断根节点是否为空?如果为空,插入节点作为根节点,结束;否则,转2;

    2.将要插入的数值和比较节点的数值进行比较。如果插入插入数值大于等于节点数值转3;否则转4;

    3.如果比较节点的右子树为空,将比较节点的右指针指向插入节点,结束;否则,将比较节点的右子树的根节点作为新的比较节点,转2;

    4.如果比较节点的左子树为空,将比较节点的左指针指向插入节点,结束;否则,将比较节点的左子树的根节点作为新的比较节点,转2;

    通过上述的插入过程实现的二叉树,可以得出:对于根节点,它左边的所有节点的值都小于它;而它右边的所有节点是数字都大于或等于它。中序遍历的过程是“左子节点->父节点->右子节点” ,这样数值的输出便具有了一定的顺序,这个规则的不断重复,就是归并排序思想的体现。同时该插入过程符合递归的特性。

  算法分析(时间复杂度):

    查找过程:二叉树查找过程的时间和二叉树的高度成正比。假设二叉树的高度为n,则查找最大值,最小值,前继结点后和后继节点的时间都可以在O(n)时间内实现。所以树的高度直接决定查找的效率。对于拥有n个节点的二叉树,形成完全二叉树时树的高度是最小的,即最小为lg(n);

    删除和插入过程:在二叉树删除和插入过程,查找过程是必须的前奏;所以插入和删除的最小为(lg(n)+O(1)),其中O(1)为插入和删除的时间。 

    

  1 #include "stdafx.h"
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 
  5 //非递归方法插入节点
  6 void InsertBtree(node *root,int value)
  7 {
  8     
  9     node *t=(node *)malloc(sizeof(node));
 10     t->left=t->right=NULL;
 11     t->number=value;
 12 
 13     node *temp=NULL;    
 14     temp=root;
 15 
 16     if(temp==NULL)
 17     {
 18         root=t;
 19     }
 20     else
 21     {
 22         while(temp!=NULL)
 23         {
 24             if(t->number>=temp->number)        
 25             {
 26                 if(temp->right==NULL)
 27                 {
 28                     temp->right=t;
 29                     break;
 30                 }
 31                 else
 32                 {
 33                     temp=temp->right;
 34                 }
 35 
 36             }
 37             else
 38             {
 39                 if(temp->left==NULL)
 40                 {
 41                     temp->left=t;
 42                     break;
 43                 }
 44                 else
 45                 {
 46                     temp=temp->left;
 47                 }
 48             }
 49         }
 50     }    
 51 }
 52 
 53 //采用递归的方法插入节点
 54 void InsertBtreeRe(node *root,int value)
 55 {
 56     if(root==NULL)
 57     {
 58         
 59         root->number=value;
 60         return;
 61     }
 62     else
 63     {
 64         if(value>=root->number)
 65         {
 66             if(root->right==NULL)
 67             {
 68                 node *roott=(node *)malloc(sizeof(node));
 69                 roott->left=roott->right=NULL;
 70                 roott->number=value;
 71                 root->right=roott;
 72                 return;
 73             }
 74             else
 75             {
 76                 InsertBtreeRe(root->right,value);
 77             }
 78         }
 79         else
 80         {
 81             if(root->right==NULL)
 82             {
 83                 node *roott=(node *)malloc(sizeof(node));
 84                 roott->left=roott->right=NULL;
 85                 roott->number=value;
 86                 root->left=roott;
 87                 return;
 88             }
 89             else
 90             {
 91                 InsertBtreeRe(root->left,value);
 92             }
 93         }
 94     }
 95 }
 96 
 97 
 98 //将一个整数数组中的数字按照归并排序的原理进行存储
 99 void buildBtree(int *p, int n,node *root)
100 {
101     int i;
102     node *vp=NULL;
103 
104     root->number=p[0];
105     root->left=root->right=NULL;
106 
107     for(i=1;i<n;i++)
108     {
109         
110         vp=root;
111         node *temp=(node *)malloc(sizeof(node));
112         temp->number=p[i];
113         temp->left=temp->right=NULL;
114 
115         while(vp!=NULL)
116         {
117             if(temp->number>=vp->number)
118             {
119                 if(vp->right==NULL)
120                 {
121                     vp->right=temp;
122                     break;
123                 }
124                 else
125                 {
126                     vp=vp->right;
127                 }
128             }
129             else
130             {
131                 if(vp->left==NULL)
132                 {
133                     vp->left=temp;
134                     break;
135                 }
136                 else
137                 {
138                     vp=vp->left;
139                 }
140             }
141         }
142         
143     }
144 
145 }
146 
147 //按照中序遍历二叉树
148 void midsearchBtree(node *proot)
149 {
150     if(proot!=NULL)
151     {
152         midsearchBtree(proot->left);
153         printf("%d ",proot->number);
154         midsearchBtree(proot->right);
155     }
156 }
157 
158 int main()
159 {
160     node *croot;
161     int input;
162     croot=(node *)malloc(sizeof(node));
163 
164     int a[]={7,5,3,2,4};
165     buildBtree(a,5,croot);
166     midsearchBtree(croot);
167     printf("\n");
168 
169     while(scanf_s("%d",&input)&& input!=1)
170     {
171         InsertBtree(croot,input);
172     }
173     midsearchBtree(croot);
174     printf("\n");
175 
176     InsertBtreeRe(croot,10);
177     midsearchBtree(croot);
178     printf("\n");
179     return 0;
180 }

 

              

  

转载于:https://www.cnblogs.com/zhengyou/p/3567759.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421178.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1020. Tree Traversals (25)

1020. Tree Traversals (25) 时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueSuppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to out…

leetcode 387 C++数组做法

leetcode 387 C数组做法 class Solution { public:int firstUniqChar(string s) {int ns.length();if(n0) return -1;int table[26]{0};for(int i0;i!n;i){table[s[i]-a];}for(int i0;i!n;i){if(table[s[i]-a]1)return i;}return -1;} };END

获取Class对象方式

在java中&#xff0c;每个class都有一个相应的Class对象&#xff0c;当编写好一个类&#xff0c;编译完成后&#xff0c;在生成的.class文件中&#xff0c;就产生一个Class对象&#xff0c;用来表示这个类的类型信息。获得Class实例的三种方式&#xff1a; 1). 利用对象调用get…

前端学习(1002):简洁版滑动下拉菜单问题解决

快速滑动 不停切换 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title><scrip…

js bom and dom

以下的代码只是一些小的例子。我画了一张图来总结js大的结构 <!DOCTYPE html> <html><head><meta charset"UTF-8"><title></title><script>//Point 1 delayer and timer (BOM browser Object Model)var delayer;var tim…

leetcode 383 赎金信 C++

自己想的&#xff0c;一个思路两个解法&#xff0c;从字符串中的第一个唯一字符的思路搬过来的 one class Solution { public:bool canConstruct(string ransomNote, string magazine) {int table2[26]{0};for(int i0;i!magazine.length();i){table2[magazine[i]-a];}for(int …

Win32下 Qt与Lua交互使用(二):在Lua脚本中使用Qt类

话接上篇。成功配置好QtLuatoLua后&#xff0c;我们可以实现在Lua脚本中使用各个Qt的类。直接看代码吧。 #include "include/lua.hpp" #include <QWidget> #include <QApplication> #include <QFile> #include <QDebug>static int tolua_new…

1099. Build A Binary Search Tree (30)

1099. Build A Binary Search Tree (30) 时间限制100 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueA Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains o…

mysql列属性auto(mysql笔记四)

常见的的是一个字段不为null存在默认值 没值得时候才去找默认值&#xff0c;可以插入一个null到 可以为null的行里 主键&#xff1a;可以唯一标识某条记录的字段或者字段的集合 主键设置 主键不可为null,声明时自动设置为not null 字段上设置 字段名 primary key定义完字段后 …

详解html结构之间的各个关系,层级关系(以列表为例)

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title>层级关系查找元素</title></head><body><div id "div">hello<ul id ""><li>li1</li><li>li2</…

leetcode 242 有效的字母异位词 C++

和赎金信的思路一样 我想我本科时光是找不到女朋友了&#xff0c;哪怕是一个异性的好朋友也不会有了&#xff0c;男女比例4&#xff1a;1&#xff0c;哼 class Solution { public:bool isAnagram(string s, string t) {int table2[26]{0};for(char a:t){table2[a-a];}for(char…

1058. A+B in Hogwarts (20)

1058. AB in Hogwarts (20) 时间限制50 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueIf you are a fan of Harry Potter, you would know the world of magic has its own currency system -- as Hagrid explained it to Harry, "Seventeen silve…

jquery post 同步异步总结

最近在测试,发现有些效果不对,最后发现是post的执行顺序问题,所以研究了下,写了以下总结 1.post被请求多次,解决方法: 连接加入随机数 rand""Math.random() $.post("/Control/webControl.ashx?rand "Math.random(), { Method: "LoginIn", Parem…

js对html节点的操作

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title></title><style></style><script>function copy(){//克隆节点//1&#xff0c;得到要克隆的divvar div document.body.firstElementChild;//2,复制…

leetcode 141 环形链表 C++

两种方法一个空间O(n)&#xff0c;另一个O(1)&#xff0c;时间都是O(n)。 one class Solution { public:bool hasCycle(ListNode *head) {unordered_set<ListNode*>set;while(head){if(set.count(head))return true;set.insert(head);headhead->next;}return false;}…

1056. Mice and Rice (25)

1056. Mice and Rice (25) 时间限制100 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueMice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map…

Java poi读取,写入Excel2003

Java poi读取,写入Excel2003 相关阅读&#xff1a;poi读写Excel2007:http://www.cnblogs.com/gavinYang/p/3576741.htmljxl读写excel2003/2007:http://www.cnblogs.com/gavinYang/p/3576819.html package com.gavin.operational.excle;import java.io.FileInputStream; import …

leetcode 21 合并两个有序链表 C++

因为迭代比较好理解 所以我们在这里用递归 class Solution { public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {if(!l1) return l2;else if(!l2) return l1;else if(l1->val<l2->val){l1->nextmergeTwoLists(l1->next,l2);return l1;}else{l2->…

1061. 判断题(15)

1061. 判断题(15) 时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue判断题的评判很简单&#xff0c;本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分。 输入格式&#xff1a; 输入在第一行给出两个不超过100的正整数N和M&…

css和 js 改变html里面的定位。

css和 js 改变html里面的定位。&#xff08;三种方式&#xff09; <style type"text/css">#div1{border: 1px aquamarine solid;/*固定定位&#xff1a;此元素在整个网页的位置不变,固定某处不动*/position : fixed;left: 20px;top: 10px;}#div2{/*相对定位&am…